diff --git a/apps/cli/ai/tools/wp-cli.ts b/apps/cli/ai/tools/wp-cli.ts index 80e8e10e12..121ba1bbea 100644 --- a/apps/cli/ai/tools/wp-cli.ts +++ b/apps/cli/ai/tools/wp-cli.ts @@ -1,10 +1,7 @@ import { Type } from 'typebox'; import { connectToDaemon, disconnectFromDaemon } from 'cli/lib/daemon-client'; import { runWpCliCommand } from 'cli/lib/run-wp-cli-command'; -import { - getUnsupportedWpCliPostContentMessage, - routePostContentFile, -} from 'cli/lib/wp-cli-post-content'; +import { getUnsupportedWpCliPostContentMessage } from 'cli/lib/wp-cli-post-content'; import { defineTool, REMOTE_OVERWRITE } from './define-tool'; import { resolveSite } from './utils'; @@ -167,9 +164,7 @@ export const runWpCliTool = defineTool( try { await connectToDaemon(); - const splitArgs = splitCommandArgs( args.command ); - const routed = routePostContentFile( splitArgs, site.path ); - const wpCliArgs = routed?.args ?? splitArgs; + const wpCliArgs = splitCommandArgs( args.command ); const unsupportedOptionMessage = getUnsupportedWpCliOptionMessage( wpCliArgs ); if ( unsupportedOptionMessage ) { throw new Error( unsupportedOptionMessage ); @@ -184,9 +179,7 @@ export const runWpCliTool = defineTool( const stdout = await command.response.stdoutText; const stderr = await command.response.stderrText; - let output = routed - ? `Read the post content from ${ routed.file }, the file \`--post_content\` named.\n` - : ''; + let output = ''; if ( stdout ) { output += stdout; } diff --git a/apps/cli/lib/tests/wp-cli-post-content.test.ts b/apps/cli/lib/tests/wp-cli-post-content.test.ts index 747adce0c1..f6fa5f5e5d 100644 --- a/apps/cli/lib/tests/wp-cli-post-content.test.ts +++ b/apps/cli/lib/tests/wp-cli-post-content.test.ts @@ -1,10 +1,4 @@ -import { mkdirSync, mkdtempSync, writeFileSync } from 'fs'; -import os from 'os'; -import path from 'path'; -import { - getUnsupportedWpCliPostContentMessage, - routePostContentFile, -} from 'cli/lib/wp-cli-post-content'; +import { getUnsupportedWpCliPostContentMessage } from 'cli/lib/wp-cli-post-content'; describe( 'getUnsupportedWpCliPostContentMessage', () => { it( 'ignores commands other than post create and update', () => { @@ -65,62 +59,31 @@ describe( 'getUnsupportedWpCliPostContentMessage', () => { ] ) ).toContain( 'does not run in a shell' ); } ); -} ); - -describe( 'routePostContentFile', () => { - const sitePath = mkdtempSync( path.join( os.tmpdir(), 'post-content-path-' ) ); - mkdirSync( path.join( sitePath, 'tmp' ) ); - writeFileSync( path.join( sitePath, 'tmp', 'header.html' ), '' ); - - it( 'passes an absolute file path as the file WP-CLI reads content from', () => { - const file = path.join( sitePath, 'tmp', 'header.html' ); - expect( - routePostContentFile( [ 'post', 'update', '5', `--post_content=${ file }` ], sitePath ) - ).toEqual( { args: [ 'post', 'update', '5', file ], file } ); - } ); - it( 'keeps the flags around a relative path', () => { - expect( - routePostContentFile( - [ 'post', 'create', '--post_type=page', '--post_content=tmp/header.html --porcelain' ], - sitePath - ) - ).toEqual( { - args: [ 'post', 'create', '--post_type=page', '--porcelain', 'tmp/header.html' ], - file: 'tmp/header.html', - } ); - } ); - - it( 'leaves content that only looks like a path, and other commands, alone', () => { + it( 'refuses a file path, which WP-CLI would save as the content', () => { for ( const content of [ - 'tmp/missing.html', - 'tmp', - 'See tmp/header.html', - 'tmp/header.html and more', - '
tmp/header.html
', + '/Users/me/Studio/site/tmp/header.html', + 'tmp/header.html --porcelain', + 'tmp/styles.json', ] ) { expect( - routePostContentFile( [ 'post', 'create', `--post_content=${ content }` ], sitePath ) - ).toBeUndefined(); + getUnsupportedWpCliPostContentMessage( [ + 'post', + 'update', + '5', + `--post_content=${ content }`, + ] ) + ).toContain( 'not a file path' ); } - expect( - routePostContentFile( - [ 'option', 'update', 'x', '--post_content=tmp/header.html' ], - sitePath - ) - ).toBeUndefined(); - } ); - - it( 'leaves a file outside the site folder alone', () => { - const outside = mkdtempSync( path.join( os.tmpdir(), 'post-content-outside-' ) ); - writeFileSync( path.join( outside, 'notes.html' ), 'x' ); - for ( const file of [ - path.join( outside, 'notes.html' ), - `../${ path.basename( outside ) }/notes.html`, + for ( const content of [ + 'tmp', + 'See tmp/header.html', + 'tmp/header.html
', + 'Hi
', ] ) { expect( - routePostContentFile( [ 'post', 'create', `--post_content=${ file }` ], sitePath ) - ).toBeUndefined(); + getUnsupportedWpCliPostContentMessage( [ 'post', 'create', `--post_content=${ content }` ] ) + ).toBeNull(); } } ); } ); diff --git a/apps/cli/lib/wp-cli-post-content.ts b/apps/cli/lib/wp-cli-post-content.ts index 592afba3fa..c84d7c780a 100644 --- a/apps/cli/lib/wp-cli-post-content.ts +++ b/apps/cli/lib/wp-cli-post-content.ts @@ -1,6 +1,3 @@ -import { existsSync, statSync } from 'fs'; -import path from 'path'; - const UNSUPPORTED_WP_CLI_POST_CONTENT_MESSAGE = 'Unsupported `--post_content` value: `wp_cli` does not run in a shell. Do not use `$(cat file)` or backticks. Pass the literal post content directly, or write large content to a scratch file inside the site directory and pass its path as a positional argument (`wp post create