Skip to content
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 first version of the command #188

Merged
merged 62 commits into from
May 3, 2023

Conversation

sejas
Copy link
Collaborator

@sejas sejas commented Apr 10, 2023

Proposed changes

  • Create an initial version of wp-now start

Testing instructions

  • Create a wordpress theme or plugin. Or just a index.php.
  • Run nx preview wp-now start --path=/Users/path-to-your-theme-or-plugin
  • Open the browser in the displayed URL. By default http://127.0.0.1:8881
  • wp-now will detect if the given folder, or current directory correspond to any of these modes:
  • index: Simple php file
  • theme : loads WordPress with your theme included
  • plugin : loads WordPress with your plugin included

Screencast

wp-now-run-source-code-peq.mp4

@adamziel
Copy link
Collaborator

Nx has a command that will set up a new "project" for you and add all the necessary boilerplate:

nx g @nrwl/node:application

If you use it to create a wp-now project in the packages/playground directory, it will create and wire the tsconfig.json files correctly, give you a correct package.json, and generate the project.json file that would then enable you to build this project using nx, e.g. nx build wp-now.

multipartData += `${json[key]}${eol}`;
}

multipartData += `--${boundary}--${eol}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this not interfere with other multipart segments?
I would imagine that if we have more than one call to generateMultipartFormDataString and then concatenate the strings we'll get empty parts because the boundary will be duplicated with no content in between

Copy link
Collaborator

@adamziel adamziel Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's one-off per request and not meant to be concatenated. Eventually I'd love to somehow get rid of this entirely

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not add the final boundary outside of the function then? or create a kind of multipartJoinSegments function so we don't do this?

I guess I was just a little confused because the function name suggests to me that it's a generic function to create multiple multi-part chunks.

define('WP_HOME', "${this.options.absoluteUrl}");
define('WP_SITEURL', "${this.options.absoluteUrl}");
?>${contents}`
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like @adamziel has been abstracting this into setupSiteUrl()

if we're concatenating <?php multiple times we'll run into trouble, plus by defining WP_HOME twice we might run into PHP crashes

Copy link
Collaborator

@adamziel adamziel Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference: defineSiteUrl

Copy link
Collaborator Author

@sejas sejas Apr 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition! 🥳

In order to use defineSiteUrl I would like to accept a custom documentRoot.

Another option would be changing the wp-now documentRoot to /wordpress

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tricky question: How could we handle wp-now run script.php where script.php needs files outside of the current working directory? e.g. include "../../vendor/something.phar";.

Calling php.useHostFilesystem() would make it just work, but you couldn't mount WordPress anymore. Since it's all JavaScript, we could add support for mounting inside already–mounted paths, but first I'd like to explore what use-cases we're dealing with.

Would I need to ever go one level up from the WordPress path:

  • If I'm a core developer? Probably no
  • If I'm a plugin developer? Maybe...? But hopefully unlikely
  • If I want to use wp-now just for its PHP and without WordPress? Surely! But that's out of scope for this project.


mount() {
this.mountWordpress()
this.mountSqlite()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mountSqlite is async but this function isn't. we might want to make this async and await this.mountSqlite() or acknowledge that mount() might return before mountSqlite has completed (or technically, before it has even begun to process)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!
mountSqlite shouldn't be async 👍
Fixed here: 684fb86

@sejas sejas changed the title WIP: wp-now: create first version of the command wp-now: create first version of the command May 3, 2023
Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked out this branch, ran yarn install, and then nx preview crashed with this error:

nx preview wp-now start --path=/Users/danielbachhuber/projects/vanilla

> nx run wp-now:"build:bundle"


> nx run wp-now:"build:package-json"


> nx run wp-now:build


> nx run wp-now:preview start --path=/Users/danielbachhuber/projects/vanilla

(node:55204) 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)
/Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:286
  throw new ERR_MODULE_NOT_FOUND(
        ^
CustomError: Cannot find package '/Users/danielbachhuber/projects/wordpress-playground/node_modules/@php-wasm/node/' imported from /Users/danielbachhuber/projects/wordpress-playground/dist/packages/wp-now/main.js
    at legacyMainResolve (/Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:286:9)
    at packageResolve (/Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:750:14)
    at moduleResolve (/Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:798:18)
    at Object.defaultResolve (/Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:912:11)
    at /Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/src/esm.ts:218:35
    at entrypointFallback (/Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/src/esm.ts:168:34)
    at /Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/src/esm.ts:217:14
    at addShortCircuitFlag (/Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/src/esm.ts:409:21)
    at resolve (/Users/danielbachhuber/projects/wordpress-playground/node_modules/ts-node/src/esm.ts:197:12)
    at resolve (file:///Users/danielbachhuber/projects/wordpress-playground/packages/nx-extensions/src/executors/built-script/loader.mjs:37:15)

 —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Ran target preview for project wp-now and 3 task(s) they depend on (1s)

         With additional flags:
           start
           --path=/Users/danielbachhuber/projects/vanilla

    ✖    1/4 failed
    ✔    3/4 succeeded [0 read from cache]

Is there another setup step I'm missing?

@danielbachhuber
Copy link
Member

Is there another setup step I'm missing?

I see the README now. Might be worth mentioning that in the PR description.

Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of minor nits. I think this is good to land otherwise

@@ -0,0 +1,31 @@
# WP-NOW

`wp-now` is an intuitive Command Line Interface (CLI) tool designed to streamline the process of setting up a local WordPress environment by using only Node.js. This powerful tool is an ideal choice for developers working on WordPress themes and plugins.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`wp-now` is an intuitive Command Line Interface (CLI) tool designed to streamline the process of setting up a local WordPress environment by using only Node.js. This powerful tool is an ideal choice for developers working on WordPress themes and plugins.
`wp-now` is a Command Line Interface (CLI) tool designed to streamline the process of setting up a local WordPress environment by using only Node.js. This powerful tool is optimized for developers working on WordPress themes and plugins.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here: dec0385

Thanks for the suggestion.

type: 'string',
default: process.cwd(),
});
yargs.option('wp-content', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this argument is confusing. Can we remove it for now, and re-think it at a later point?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed! 55189e4

The goal of the parameter was to map the wp-content folder to a diferent path.
Currently we are mapping it to: ~/.wp-now/wp-content/${projectName}-${sha1(projectPath)}

packages/wp-now/src/run-cli.ts Outdated Show resolved Hide resolved
@danielbachhuber danielbachhuber self-requested a review May 3, 2023 12:21
Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my approval shouldn't matter, but I'm giving it to say I see no reason to block this. thanks for creating associated issues!

@danielbachhuber danielbachhuber merged commit 6c532ef into WordPress:trunk May 3, 2023
@sejas sejas deleted the add/wp-now branch May 3, 2023 14:13
@sejas
Copy link
Collaborator Author

sejas commented May 3, 2023

@dmsnell, thank you for your invaluable code reviews 🙏. Your help has increased the quality of the code. Much appreciated!

Now that we've successfully merged the first prototype, I believe it will be easier to iterate and improve wp-now through smaller PRs.

@dmsnell
Copy link
Member

dmsnell commented May 3, 2023

nice work on the code. thanks for your effort and time on this!

if (mode === 'plugin' || mode === 'theme') {
const folderName = path.basename(this.options.projectPath);
const partialPath = mode === 'plugin' ? 'plugins' : 'themes';
fs.ensureDirSync(path.join(wpContentPath, partialPath, folderName));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put ensureDirSync directly in NodePHP

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 , Not sure about this change. I'll take a deeper look tomorrow.

Copy link
Collaborator

@adamziel adamziel May 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rationale: I keep seeing confused developers "why is my mount() not working?" Everyone is going to get stuck here for a moment, make a few checks, and realize they need this to ensure the directory exists – we can remove the speed bump and just do it for them.

@adamziel
Copy link
Collaborator

adamziel commented May 4, 2023

I just caught up with everything here. I agree with everything @dmsnell noted and added a few nits of my own, but other than that this is great! Thank you so much for working on this @sejas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Local Environment: Prototype initial implementation
4 participants