Skip to content

Commit

Permalink
wp-now: create first version of the command (#188)
Browse files Browse the repository at this point in the history
* wp-now: create dummy commands and new package

* wp-now: re-create wp-now folders using nx

* wp-now: execute php code example

* wp-now: add connect node proxy to php runtime

* wp-now: download latest wp zip in .wp-now dir

* wp-now: download wordpress and proxy requests fixing location

* wp-now: remove unused run method

* wp-now: clean unused imports

* wp-now: css fixed, redirect fixed

* wp-now: setup sqlite

* wp-now: use express and change the response to bytes

* wp-now: rename download lib

* wp-now: add sqlite funciton

* wp-now: move sqlite logic to nodejs and unify paths

* wp-now: split create-server run-cli logic

* wp-now: update yarn lock

* wp-now: import HTTPMethod from abstract

* wp-now: avoid importing abstract

* wp-now: remove async from mountSqlite

* wp-now: clean unnecesary cli

* wp-now: fix imports after merge using NodePHP and universal

* wp-now: change build phase to esbuild module

* wp-now: remove clear console lines

* wp-now: use portFinder for open port source of truth

* wp-now: receive path as paramter

* wp-now: accept path and mode, enable plugin mode

* wp-now: infer mode plugin, theme, index or core

* wp-now: infer mode static directory

* wp-now: rename patchFile to updateFile

* wp-now: add comments to constants

* wp-now: reduce amount of child folders for sqlite

* wp-now: run always wp-now cli mode

Exmaple:
nx preview wp-now start --path=/Users/macbookpro/gutenberg

* wp-now: use path as a parameter instead of cwd()

* wp-now: fix code style

* wp-now: remove unnecessary log port number

* wp-now: update SQLite plugin name under quotes

* wp-now: remove node env suggestion from run-cli

* wp-now: project.json run commands sequentially

* wp-now: rename wordpress versions path

* wp-now: fix code style

* wp-now: rename requestBodyToMultipartFormData

* wp-now: reuse defineSiteUrl

* wp-now: include a WP-NOW Readme and copy it to dist

* wp-now: improve the readme redaction

* wp-now: improve the readme redaction

* wp-now: add phpVersion parameter

Closes: #232

* wp-now: fix code style

* wp-now: default mode is index

* wp-now: fix code style

* wp-now: use dashes for wordpress-versions folder

* wp-now: update readme

* wp-now: remove wp-content argument to use only home directory

* wp-now: rename phpVersion argument name to php

* wp-now: pass php version value from php argument
  • Loading branch information
sejas authored May 3, 2023
1 parent dce32bf commit 6c532ef
Show file tree
Hide file tree
Showing 20 changed files with 1,336 additions and 13 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@
"dependencies": {
"classnames": "^2.3.2",
"comlink": "^4.4.1",
"express-fileupload": "1.4.0",
"file-saver": "^2.0.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-modal": "^3.16.1"
"react-modal": "^3.16.1",
"request": "2.88.2",
"unzipper": "0.10.11"
},
"devDependencies": {
"@convex-dev/typedoc-plugin-markdown": "3.14.0",
Expand Down Expand Up @@ -76,6 +79,7 @@
"@vitejs/plugin-react": "^3.1.0",
"@vitest/coverage-c8": "~0.25.8",
"@vitest/ui": "^0.25.8",
"chalk": "5.2.0",
"dts-bundle-generator": "^7.2.0",
"esbuild": "^0.17.5",
"esbuild-plugin-copy": "^2.1.0",
Expand All @@ -95,6 +99,7 @@
"jsonc-eslint-parser": "^2.1.0",
"lerna": "6.6.1",
"nx": "15.8.6",
"ora": "6.3.0",
"prettier": "^2.6.2",
"react-test-renderer": "18.2.0",
"rimraf": "^4.4.0",
Expand Down
18 changes: 18 additions & 0 deletions packages/wp-now/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
31 changes: 31 additions & 0 deletions packages/wp-now/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# WP-NOW

`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.

## Getting Started

Follow these steps to build and run `wp-now` locally:

### Building

To build the project, use the following commands in your terminal:

```bash
nvm use
yarn install
yarn build
```

### Running

To start the web server and execute your WordPress plugin or theme, use the following command:

```bash
nx preview wp-now start --path=/path/to/wordpress-plugin-or-theme
```

Replace `/path/to/wordpress-plugin-or-theme` with the actual path to your plugin or theme folder.

## Contributing

We welcome contributions from the community! Please refer to the main [README.md](../../README.md) file for instructions on how to contribute to this project.
10 changes: 10 additions & 0 deletions packages/wp-now/esbuild.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as esbuild from 'esbuild';

await esbuild.build({
entryPoints: ['packages/wp-now/src/main.ts'],
outdir: 'dist/packages/wp-now',
bundle: true,
packages: 'external',
format: 'esm',
platform: 'node',
});
14 changes: 14 additions & 0 deletions packages/wp-now/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable */
export default {
displayName: 'wp-now',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': [
'ts-jest',
{ tsconfig: '<rootDir>/tsconfig.spec.json' },
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/wp-now',
};
27 changes: 27 additions & 0 deletions packages/wp-now/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "wp-now",
"version": "0.0.0",
"description": "WordPress Playground CLI",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/wordpress-playground"
},
"homepage": "https://developer.wordpress.org/playground",
"author": "The WordPress contributors",
"contributors": [
{
"name": "Antonio Sejas",
"email": "[email protected]",
"url": "https://github.com/sejas"
}
],
"publishConfig": {
"access": "public",
"directory": "../../../dist/packages/wp-now"
},
"license": "(GPL-2.0-or-later OR MPL-2.0)",
"type": "module",
"main": "main.js",
"bin": "main.js",
"gitHead": "e6a7ed1b4e3e804590a5d9c40d95dfb9e6281e1a"
}
76 changes: 76 additions & 0 deletions packages/wp-now/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "wp-now",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/wp-now/src",
"projectType": "application",
"targets": {
"build": {
"executor": "nx:noop",
"dependsOn": ["build:package-json", "build:bundle"]
},
"build:package-json": {
"executor": "@wp-playground/nx-extensions:package-json",
"options": {
"tsConfig": "packages/wp-now/tsconfig.lib.json",
"outputPath": "dist/packages/wp-now",
"buildTarget": "wp-now:build:bundle"
},
"dependsOn": ["build:bundle"]
},
"build:bundle": {
"executor": "nx:run-commands",
"options": {
"commands": [
"node packages/wp-now/esbuild.mjs",
"cp packages/wp-now/package.json dist/packages/wp-now",
"cp packages/wp-now/README.md dist/packages/wp-now"
],
"parallel": false
}
},
"preview": {
"executor": "@wp-playground/nx-extensions:built-script",
"options": {
"scriptPath": "dist/packages/wp-now/main.js"
},
"dependsOn": ["build"]
},
"serve": {
"executor": "@nrwl/js:node",
"defaultConfiguration": "development",
"options": {
"buildTarget": "wp-now:build"
},
"configurations": {
"development": {
"buildTarget": "wp-now:build:development"
},
"production": {
"buildTarget": "wp-now:build:production"
}
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/wp-now/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/wp-now/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}
Empty file.
51 changes: 51 additions & 0 deletions packages/wp-now/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os from 'os';
import path from 'path';

/**
* The hidden folder name for storing WP Now related files.
*/
export const WP_NOW_HIDDEN_FOLDER = '.wp-now';

/**
* The full path to the hidden WP Now folder in the user's home directory.
*/
export const WP_NOW_PATH = path.join(os.homedir(), WP_NOW_HIDDEN_FOLDER);

/**
* The path where WordPress zip files will be unzipped and stored within the WP Now folder.
*/
export const WORDPRESS_VERSIONS_PATH = path.join(
WP_NOW_PATH,
'wordpress-versions'
);

/**
* The file name for the SQLite plugin name.
*/
export const SQLITE_FILENAME = 'sqlite-database-integration';

/**
* The full path to the "SQLite database integration" folder.
*/
export const SQLITE_PATH = path.join(WP_NOW_PATH, `${SQLITE_FILENAME}-main`);

/**
* The URL for downloading the latest version of WordPress.
*/
export const WP_DOWNLOAD_URL = 'https://wordpress.org/latest.zip';

/**
* The URL for downloading the "SQLite database integration" WordPress Plugin.
*/
export const SQLITE_URL =
'https://github.com/WordPress/sqlite-database-integration/archive/refs/heads/main.zip';

/**
* The default starting port for running the WP Now server.
*/
export const DEFAULT_PORT = 8881;

/**
* The default PHP version to use when running the WP Now server.
*/
export const DEFAULT_PHP_VERSION = '8.0';
75 changes: 75 additions & 0 deletions packages/wp-now/src/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import request from 'request';
import fs from 'fs-extra';
import unzipper from 'unzipper';
import path from 'path';
import {
SQLITE_FILENAME,
SQLITE_PATH,
SQLITE_URL,
WORDPRESS_VERSIONS_PATH,
WP_DOWNLOAD_URL,
WP_NOW_PATH,
} from './constants';

async function downloadFile(url: string, dest: string): Promise<void> {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest);
request(url)
.pipe(file)
.on('finish', () => {
file.close(() => resolve());
})
.on('error', (err: Error) => {
fs.unlink(dest);
reject(err);
});
});
}

async function downloadFileAndUnzip({
url,
zipPath,
unzipPath,
checkFinalPath,
itemName,
}) {
if (!fs.existsSync(checkFinalPath)) {
try {
fs.ensureDirSync(path.dirname(zipPath));
console.log(`Downloading ${itemName}...`);
await downloadFile(url, zipPath);
// unzip the file
console.log(`Unzipping ${itemName}...`);
await fs
.createReadStream(zipPath)
.pipe(unzipper.Extract({ path: unzipPath }))
.promise();
console.log('Removing Zip.');
await fs.remove(zipPath);
} catch (err) {
console.error(`Error downloading or unzipping ${itemName}:`, err);
}
} else {
console.log(`${itemName} folder already exists. Skipping download.`);
}
}

export async function downloadWordPress(fileName = 'latest') {
return downloadFileAndUnzip({
url: WP_DOWNLOAD_URL,
zipPath: path.join(WORDPRESS_VERSIONS_PATH, `${fileName}.zip`),
unzipPath: path.join(WORDPRESS_VERSIONS_PATH, fileName),
checkFinalPath: path.join(WORDPRESS_VERSIONS_PATH, fileName),
itemName: `WordPress ${fileName}`,
});
}

export async function downloadSqlite() {
return downloadFileAndUnzip({
url: SQLITE_URL,
zipPath: path.join(WP_NOW_PATH, `${SQLITE_FILENAME}.zip`),
unzipPath: WP_NOW_PATH,
checkFinalPath: SQLITE_PATH,
itemName: 'Sqlite',
});
}
3 changes: 3 additions & 0 deletions packages/wp-now/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { runCli } from './run-cli';

runCli();
Loading

0 comments on commit 6c532ef

Please sign in to comment.