WordPress / playground-tools

GNU General Public License v2.0
138 stars 39 forks source link

Allow express app to be (optionally) passed to start-server #284

Open Shelob9 opened 4 months ago

Shelob9 commented 4 months ago

What?

Why?

I wanted to see if I could integrate this with an existing express app. I was able to make it work with one small change that I think is a useful change. Would allow using WordPress for part of a node application and or easily plug this package into existing application

This is what I am thinking:

import {
    getWpNowConfig,
    startServer
} from '@wp-now/wp-now';

const options = await getWpNowConfig({});
const app = express();
app.get('/api', (req, res) => {
    res.json({message:"Hi Roy"})
});
const {  url } = await startServer({...options,app});
console.log({url})

How?

Testing Instructions

No Changes test

  1. Check out the branch.
  2. npx nx preview wp-now start
  3. Should work as-is, no changes.

Works this way test

  1. Check out the branch.
  2. Link npm link @wp-now/wp-now https://docs.npmjs.com/cli/v9/commands/npm-link
  3. Make a new project
  4. Install this wp-now locally
  5. Test it with above example code
bgrgicak commented 3 months ago

Hey @Shelob9, thanks for working on this. While this could work there is a much better way to do it with Playground instead of WP-NOW. Playground has a CLI server and it would be great if there was an Express middleware that could run Playground CLI requests. Would you be up for working on this in the Playground repository?

FYI @juanmaguitar

Shelob9 commented 3 months ago

While this could work there is a much better way to do it with Playground instead of WP-NOW.

Why is this better? The reason I chose to do it this way was:

bgrgicak commented 3 months ago

Why is this better?

WP-now is a tool that uses Playground and is built for local development. Playground CLI is the standard way of running Playground servers. By contributing to the Playground CLI everyone gets the benefits.

adamziel commented 3 months ago

A middleware could be used in any express server, while hooking into wp-now makes it specific to the way wp-now is booted and configured.