-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wp-now: create php
command to execute PHP files
#336
wp-now: create php
command to execute PHP files
#336
Conversation
Example: npx nx preview wp-now php ~/Desktop/example.php --path=/Users/macbookpro/demo-wp-now/gutenberg
test('php file execution for each PHP Version', async () => { | ||
const resultFilePath = path.join(exampleDir, 'php-version-result.txt'); | ||
|
||
for (const phpVersion of SupportedPHPVersions) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add explicit tests here? Assertions inside of for
always give me concern because they can accidentally stop working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the for loop here:
fe410c8
Thanks for the advice.
Hello @sejas ! Great job!
I know it's not related to the current issue, as it was something I have mentioned in the past, however I'd find it awesome if:
UPDATE:
|
This is now fixed, thanks!
|
…to add/wp-now-php-file-execution
@kozer, I'm glad that the first issue is fixed 🥳
|
Cool! I'll approve it then, as I don't find any other problems. |
// check if filePath exists | ||
const absoluteFilePath = path.resolve(filePath); | ||
if (!fs.existsSync(absoluteFilePath)) { | ||
throw new Error(`File not found: ${absoluteFilePath}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's match PHP's behavior:
$ php foo.php
Could not open input file: foo.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed cd4afb8
packages/wp-now/src/run-cli.ts
Outdated
} catch (error) { | ||
console.error(error); | ||
spinner.fail( | ||
`Failed to start the server: ${ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this output is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed 5bd0f53
packages/wp-now/src/run-cli.ts
Outdated
}); | ||
}, | ||
async (argv) => { | ||
const spinner = startSpinner('Running the php command...'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the caller might expect to use the output for something else, I don't think wp-now php
should provide any additional output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the wp-now logs:
5157e9e
describe: "WordPress version to use: e.g. '--wp=6.2'", | ||
type: 'string', | ||
default: DEFAULT_WORDPRESS_VERSION, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One issue is that I can't pass arbitrary parameters to my script:
$ nx preview wp-now php example.php --php=8.1 --foo=ba
wp-now php <filePath>
Run the php command passing the arguments for php cli
Positionals:
filePath Path to the PHP file to run [string] [required]
Options:
--version Show version number [boolean]
--path Path to the PHP or WordPress project. Defaults to the current w
orking directory.
[string] [default: "/Users/danielbachhuber/projects/wordpress-playground"]
--php PHP version to use. [string] [default: "8.0"]
--wp WordPress version to use: e.g. '--wp=6.2'
[string] [default: "latest"]
-h, --help Show help [boolean]
Unknown argument: foo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For wp-cli
we will have a different command that will accept any parameters and pass them to wp-cli
.
I could do the same for this command, but the trade-off is that the user will not be able to change the php
version pr the wp
version or any other wp-now
parameter.
); | ||
process.exit(result.status); | ||
} catch (error) { | ||
console.error(error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recently we've merged the output wrapper. Could we use output?.error()
here too?
Should we modify shouldOutput
to return false when executed from the PHP CLI context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as this function is not a exported from the wp-now
package, I think we're good here. There should be no way to trigger it from the outside. As for the internal use, printing too many errors sounds better than printing too few.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we modify shouldOutput to return false when executed from the PHP CLI context?
So no output when it's running in PHP CLI? When should it output, then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you meant wp-now php
for shouldOutput=false
. Your call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamziel yes, I think it should output whatever the PHP script outputs and nothing more. My "Hello World" script executed using PHP binary built-in with macOS outputs following:
& php /path/to/test.php
Hello World!
And our new PHP command:
$ npx nx preview wp-now php /path/to/test.php
[...nx build output here...]
(node:30521) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Running the php command......
directory: /Volumes/Sites/wordpress-playground
mode: index
php: 8.0
wp: latest
Hello World!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, that makes sense! FYI the Custom ESM Loaders thing won't be there in the built npm package. I'd love to get rid of it from the repo version as well somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for confirming that. As far as I saw how npm-installed wp-now works, the nx build output is also gone there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the wp-now logs but I'm keeping the error trace in case the execution fails.
5157e9e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should update the README in this PR too.
…to add/wp-now-php-file-execution
…/wordpress-playground into add/wp-now-php-file-execution
…to add/wp-now-php-file-execution
Updated: 10c3458 |
fs.renameSync(path.join(tempFolder, 'wordpress'), finalFolder); | ||
fs.moveSync(path.join(tempFolder, 'wordpress'), finalFolder, { | ||
overwrite: true, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This solves @kozer issue #336 (comment)
Follow up from #336 (comment)
See #242
Proposed changes
php
command to execute php filesexecutePHPFile
wp-cli
will have its separate command in a different PR.Testing instructions
example.php
:<?php echo "Hello World!";
npx nx preview wp-now php ~/path-to/example.php
example.php
to add WordPress functions: e.g.<?php echo get_bloginfo();
npx nx preview wp-now php ~/path-to/example.php --path=/path-to-your-plugin/gutenberg
// To load WordPress needs to be executed in a mode different thanindex
.