@wp-operations/wp-ops 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,8 +18,9 @@ Requires Node.js 20.10 or later and git. Composer is needed for plugins. If you
18
18
  ## Commands
19
19
 
20
20
  ```bash
21
- wp-ops init my-platform --platform=path/to/wp-operations # the theme folder, for its platform.json
21
+ wp-ops init my-platform # the platform (wp-operations/), layout, workflows, git
22
22
  cd my-platform
23
+ wp-ops start # the platform at http://localhost:8881 (admin / password)
23
24
  wp-ops new plugin acme/forms
24
25
  wp-ops new app service-apps shop --server=prod-1 --app-path=/var/www/shop
25
26
  wp-ops composer add --apps=service-apps/shop --packages="acme/forms"
@@ -30,18 +31,34 @@ wp-ops doctor
30
31
 
31
32
  | Command | What it does |
32
33
  |---|---|
33
- | `init [dir] [--platform=]` | Creates `.github/platform.json`, the app manifests, `packages/`, the app folders, `.github/workflows/composer.yml` and `.gitignore`, then runs `git init`. Never overwrites a file. `--platform` copies the theme's `platform.json` to the root. |
34
+ | `init [dir]` | Creates the platform repository: the platform theme in `wp-operations/`, `platform.json`, `.github/platform.json`, the app manifests, `packages/`, the app folders, `.github/workflows/composer.yml` and `.gitignore`, then runs `git init`. Never overwrites a file. |
35
+ | `start` | Runs the platform (`wp-operations/`) locally. Site data stays in `.local/` (gitignored). `wp-ops start <dir>` runs one app instead. |
36
+ | `update` | Replaces `wp-operations/` with the platform shipped in your wp-ops version. Update wp-ops first (`npm install -g @wp-operations/wp-ops`), then run it and commit. |
34
37
  | `new app <kind> <slug>` | Creates the app (composer type `wp-app`) and adds it to the kind's manifest. |
35
38
  | `new plugin <vendor/name>` | Creates `packages/<vendor>/<name>` (composer type `psr4-plugin`) with `src/Plugin.php`. |
36
39
  | `apps`, `plugins` | Lists apps and plugins, which app uses which plugin, and any problems. |
37
40
  | `composer <add\|remove\|update> --apps= --packages=` | Edits the apps' composer.json the same way the platform does and refreshes `composer.lock`. This is what `composer.yml` runs. |
38
41
  | `sandbox` | Preview runtime for the platform. See below. |
39
42
  | `edit` | Opens wp-ide for this repository. |
40
- | `start`, `dev`, `build` | Runs one app with wp-app. |
43
+ | `dev`, `build` | Runs with file watching; builds packages with a build script. |
41
44
  | `doctor` | Checks the environment and the repository. |
42
45
 
43
46
  Running `wp-ops` with no arguments opens a menu.
44
47
 
48
+ ## Deploy
49
+
50
+ `init` (and `update`, for repositories made earlier) adds two workflows. They work the same way as the Elvion `deploy-apps.yml`:
51
+
52
+ - `deploy-apps.yml`: on push to `main`, deploys each changed app, and every app when `packages/` or a manifest changed. Running it from the platform deploys every app. Each app gets `composer install --no-dev`, is copied with rsync to `<app_path>/wp-content/themes/<theme_slug>/` on its server, and is activated with WP-CLI.
53
+ - `deploy-platform.yml`: deploys `wp-operations/` (with the root `platform.json`) to `platform.server` / `platform.app_path` from `.github/platform.json`, and activates it.
54
+
55
+ Repository secrets:
56
+
57
+ | Secret | Value |
58
+ |---|---|
59
+ | `SSH_PRIVATE_KEY` | The deploy key. |
60
+ | `SERVERS` | `label:ip:user label2:ip2:user2`, where each label is a `server` value in the manifests. |
61
+
45
62
  ## Sandbox
46
63
 
47
64
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wp-operations/wp-ops",
3
- "version": "0.3.1",
3
+ "version": "0.5.0",
4
4
  "description": "The wp-operations tool. One install gives the platform and wp-ops, wp-app, wp-ide and wp-code.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -8,6 +8,8 @@ import { UsageError } from '../args.js';
8
8
  import {
9
9
  APP_FUNCTIONS,
10
10
  APP_INDEX,
11
+ DEPLOY_APPS,
12
+ DEPLOY_PLATFORM,
11
13
  GITIGNORE,
12
14
  appStyle,
13
15
  composerWorkflow,
@@ -67,10 +69,15 @@ export async function init({ dir = process.cwd(), platform, version, log = conso
67
69
  writeNew(path.join(root, kind.folder, '.gitkeep'), '', created);
68
70
  }
69
71
  writeNew(path.join(root, 'packages', '.gitkeep'), '', created);
70
- writeNew(path.join(root, '.github', 'workflows', 'composer.yml'), composerWorkflow(version), created);
72
+ writeWorkflows(root, version, created);
71
73
  writeNew(path.join(root, '.gitignore'), GITIGNORE, created);
72
74
 
73
75
  platform = platform || bundledPlatform();
76
+ const themeDir = path.join(root, PLATFORM_DIR);
77
+ if (platform && fs.statSync(path.resolve(platform)).isDirectory() && !fs.existsSync(themeDir)) {
78
+ copyTheme(path.resolve(platform), themeDir);
79
+ created.push(themeDir);
80
+ }
74
81
  if (platform) {
75
82
  const source = path.resolve(platform);
76
83
  const file = fs.existsSync(source) && fs.statSync(source).isDirectory() ? path.join(source, 'platform.json') : source;
@@ -93,7 +100,7 @@ export async function init({ dir = process.cwd(), platform, version, log = conso
93
100
  if (!fs.existsSync(path.join(root, 'platform.json'))) {
94
101
  log(pc.yellow(`\n platform.json is missing at the root. Run: wp-ops init --platform=<path to the theme>`));
95
102
  }
96
- log(`\n Next: ${pc.bold(`cd ${path.relative(process.cwd(), root) || '.'}`)} and ${pc.bold('wp-ops platform')} to run the platform locally.`);
103
+ log(`\n Next: ${pc.bold(`cd ${path.relative(process.cwd(), root) || '.'}`)} then ${pc.bold('wp-ops start')} to run the platform locally.`);
97
104
  return { root, created, gitInit };
98
105
  }
99
106
 
@@ -109,6 +116,53 @@ export function bundledPlatform() {
109
116
  return '';
110
117
  }
111
118
 
119
+ export const PLATFORM_DIR = 'wp-operations';
120
+
121
+ function writeWorkflows(root, version, created) {
122
+ const dir = path.join(root, '.github', 'workflows');
123
+ writeNew(path.join(dir, 'composer.yml'), composerWorkflow(version), created);
124
+ writeNew(path.join(dir, 'deploy-apps.yml'), DEPLOY_APPS, created);
125
+ writeNew(path.join(dir, 'deploy-platform.yml'), DEPLOY_PLATFORM, created);
126
+ }
127
+
128
+ function themeVersion(dir) {
129
+ const css = path.join(dir, 'style.css');
130
+ return fs.existsSync(css) ? (fs.readFileSync(css, 'utf8').match(/^Version:\s*(\S+)/m)?.[1] ?? '') : '';
131
+ }
132
+
133
+ function copyTheme(source, target) {
134
+ fs.cpSync(source, target, {
135
+ recursive: true,
136
+ filter: (file) => {
137
+ const relative = path.relative(source, file).split(path.sep).join('/');
138
+ return relative !== 'ui' && !relative.startsWith('ui/');
139
+ },
140
+ });
141
+ }
142
+
143
+ /**
144
+ * Replaces the repository's platform theme (wp-operations/) with the one
145
+ * shipped in this wp-ops version. Commit the result to deploy it.
146
+ */
147
+ export async function update({ root, version, log = console.log }) {
148
+ const source = bundledPlatform();
149
+ if (!source) throw new UsageError('The platform theme is missing from this wp-ops install. Reinstall: npm install -g @wp-operations/wp-ops');
150
+ const created = [];
151
+ writeWorkflows(root, version, created);
152
+ for (const file of created) log(` ${pc.green('+')} ${path.relative(root, file).split(path.sep).join('/')}`);
153
+ const target = path.join(root, PLATFORM_DIR);
154
+ const from = themeVersion(target);
155
+ const to = themeVersion(source);
156
+ if (from === to) {
157
+ log(` Platform is up to date (${to}).`);
158
+ return { from, to, changed: created.length > 0 };
159
+ }
160
+ fs.rmSync(target, { recursive: true, force: true });
161
+ copyTheme(source, target);
162
+ log(` ${pc.green('✓')} Platform ${from || '(none)'} → ${to}. Review with git diff, then commit.`);
163
+ return { from, to, changed: true };
164
+ }
165
+
112
166
  function composerName(value) {
113
167
  return value.toLowerCase().replace(/[^a-z0-9_.-]+/g, '-').replace(/^[-_.]+|[-_.]+$/g, '');
114
168
  }
@@ -1,7 +1,7 @@
1
1
  import { spawn } from 'child_process';
2
2
  import { findRootDir, findWpAppCli } from '../project.js';
3
3
 
4
- export function startApp({ appDir, flags = [], dev = false }) {
4
+ export function startApp({ appDir, flags = [], dev = false, env = process.env }) {
5
5
  const rootDir = findRootDir(appDir);
6
6
  const wpAppCli = findWpAppCli(rootDir);
7
7
  if (!wpAppCli) {
@@ -11,7 +11,7 @@ export function startApp({ appDir, flags = [], dev = false }) {
11
11
  process.exit(1);
12
12
  }
13
13
  const args = [wpAppCli, dev ? 'dev' : 'start', `--path=${appDir}`, ...flags];
14
- const child = spawn(process.execPath, args, { stdio: 'inherit' });
14
+ const child = spawn(process.execPath, args, { stdio: 'inherit', env });
15
15
  child.on('exit', (code) => process.exit(code ?? 0));
16
16
  return child;
17
17
  }
package/src/main.js CHANGED
@@ -6,7 +6,7 @@ import { parseArgs, UsageError } from './args.js';
6
6
  import { startApp } from './commands/start.js';
7
7
  import { buildAll } from './commands/build.js';
8
8
  import { doctor } from './commands/doctor.js';
9
- import { apps, bundledPlatform, composer, init, newApp, newPlugin, platformRoot, plugins } from './commands/platform.js';
9
+ import { PLATFORM_DIR, apps, composer, init, newApp, newPlugin, platformRoot, plugins, update } from './commands/platform.js';
10
10
  import { sandbox } from './commands/sandbox.js';
11
11
  import { tui } from './tui.js';
12
12
 
@@ -19,9 +19,10 @@ Usage:
19
19
  wp-ops interactive menu
20
20
 
21
21
  Platform repository:
22
- wp-ops init [dir] lay out a platform repository and git init it
23
- wp-ops platform [--port=] run the wp-operations platform locally (admin / password)
24
- wp-ops apps list apps (<kind>/<slug>) and their deploy targets
22
+ wp-ops init [dir] create a platform repository: the platform (wp-operations/), layout, workflows, git
23
+ wp-ops start [--port=] run the platform locally (admin / password)
24
+ wp-ops update update wp-operations/ to the platform shipped with this wp-ops
25
+ wp-ops apps list apps (<kind>/<slug>) and their deploy targets
25
26
  wp-ops plugins list plugins and the apps that use them
26
27
  wp-ops new app <kind> <slug> [--server= --app-path= --theme-slug=]
27
28
  wp-ops new plugin <vendor/name> [--description=]
@@ -34,8 +35,8 @@ Platform repository:
34
35
  open the local browser editor (wp-ide)
35
36
 
36
37
  Single app:
37
- wp-ops start [dir] [--wp=… --php=… --port=…]
38
- wp-ops dev [dir] start + watch
38
+ wp-ops start <dir> [--wp=… --php=… --port=…]
39
+ wp-ops dev [dir] start + watch (the platform when no dir is given)
39
40
  wp-ops build build all packages/ with a build script
40
41
  wp-ops doctor environment and repository checks`;
41
42
 
@@ -49,8 +50,18 @@ async function run() {
49
50
  await tui(pkg.version);
50
51
  return;
51
52
  case 'start':
52
- case 'dev':
53
- startApp({ appDir: path.resolve(positional[0] ?? process.cwd()), flags, dev: command === 'dev' });
53
+ case 'dev': {
54
+ const root = await platformRoot().catch(() => null);
55
+ let appDir = positional[0] ? path.resolve(positional[0]) : process.cwd();
56
+ if (!positional[0] && root && fs.existsSync(path.join(root, PLATFORM_DIR, 'style.css'))) {
57
+ appDir = path.join(root, PLATFORM_DIR);
58
+ }
59
+ const env = root ? { ...process.env, WP_APP_HOME: process.env.WP_APP_HOME || path.join(root, '.local') } : process.env;
60
+ startApp({ appDir, flags, dev: command === 'dev', env });
61
+ return;
62
+ }
63
+ case 'update':
64
+ await update({ root: await platformRoot(), version: pkg.version });
54
65
  return;
55
66
  case 'build':
56
67
  buildAll();
@@ -61,12 +72,6 @@ async function run() {
61
72
  case 'init':
62
73
  await init({ dir: positional[0] ?? process.cwd(), platform: options.platform, version: pkg.version });
63
74
  return;
64
- case 'platform': {
65
- const theme = bundledPlatform();
66
- if (!theme) throw new UsageError('The platform theme is missing from this wp-ops install. Reinstall: npm install -g @wp-operations/wp-ops');
67
- startApp({ appDir: theme, flags });
68
- return;
69
- }
70
75
  case 'apps':
71
76
  await apps({ root: await platformRoot() });
72
77
  return;
package/src/templates.js CHANGED
@@ -74,6 +74,197 @@ jobs:
74
74
  `;
75
75
  }
76
76
 
77
+ const SSH_SETUP = ` - name: Setup SSH
78
+ run: |
79
+ mkdir -p ~/.ssh
80
+ echo "\${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
81
+ chmod 600 ~/.ssh/id_ed25519
82
+ `;
83
+
84
+ /*
85
+ * Secrets: SSH_PRIVATE_KEY, and SERVERS = "label:ip:user label2:ip2:user2"
86
+ * where each label is a "server" value from the manifests.
87
+ */
88
+ const SERVER_LOOKUP = ` SERVER_IP=$(echo "$SERVERS" | tr ' ' '\\n' | grep "^\${SERVER_LABEL}:" | cut -d: -f2)
89
+ SSH_USER=$(echo "$SERVERS" | tr ' ' '\\n' | grep "^\${SERVER_LABEL}:" | cut -d: -f3)
90
+ if [ -z "$SERVER_IP" ] || [ -z "$SSH_USER" ]; then echo "Server \\"$SERVER_LABEL\\" is not in the SERVERS secret."; exit 1; fi
91
+ ssh-keyscan -H "$SERVER_IP" >> ~/.ssh/known_hosts`;
92
+
93
+ export const DEPLOY_APPS = `name: deploy apps
94
+
95
+ on:
96
+ push:
97
+ branches: [main]
98
+ workflow_dispatch:
99
+
100
+ concurrency:
101
+ group: deploy-apps
102
+ cancel-in-progress: false
103
+
104
+ jobs:
105
+ detect:
106
+ runs-on: ubuntu-latest
107
+ outputs:
108
+ matrix: \${{ steps.set.outputs.matrix }}
109
+ steps:
110
+ - uses: actions/checkout@v4
111
+ with:
112
+ fetch-depth: 2
113
+
114
+ - name: Find the apps to deploy
115
+ id: set
116
+ env:
117
+ EVENT: \${{ github.event_name }}
118
+ run: |
119
+ KINDS=$(jq -c '.apps | to_entries | map({kind: .key, folder: (.value.folder // .key), manifest: (.value.manifest // (".github/" + .key + ".json"))})' .github/platform.json)
120
+ CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null || true)
121
+ ENTRIES="[]"
122
+ for ROW in $(echo "$KINDS" | jq -c '.[]'); do
123
+ FOLDER=$(echo "$ROW" | jq -r .folder)
124
+ MANIFEST=$(echo "$ROW" | jq -r .manifest)
125
+ [ -f "$MANIFEST" ] || continue
126
+ for APP in $(jq -r 'keys[]' "$MANIFEST"); do
127
+ if [ "$EVENT" = "workflow_dispatch" ] \\
128
+ || echo "$CHANGED" | grep -q "^$FOLDER/$APP/" \\
129
+ || echo "$CHANGED" | grep -q "^packages/" \\
130
+ || echo "$CHANGED" | grep -qx "$MANIFEST"; then
131
+ ENTRIES=$(echo "$ENTRIES" | jq -c --arg f "$FOLDER" --arg m "$MANIFEST" --arg a "$APP" '. + [{folder: $f, manifest: $m, app: $a}]')
132
+ fi
133
+ done
134
+ done
135
+ echo "matrix=$ENTRIES" >> "$GITHUB_OUTPUT"
136
+ echo "Deploying: $ENTRIES"
137
+
138
+ deploy:
139
+ needs: detect
140
+ if: \${{ needs.detect.outputs.matrix != '[]' }}
141
+ runs-on: ubuntu-latest
142
+ strategy:
143
+ fail-fast: false
144
+ max-parallel: 3
145
+ matrix:
146
+ include: \${{ fromJson(needs.detect.outputs.matrix) }}
147
+ steps:
148
+ - uses: actions/checkout@v4
149
+
150
+ - uses: shivammathur/setup-php@v2
151
+ with:
152
+ php-version: '8.2'
153
+ tools: composer:v2
154
+ coverage: none
155
+
156
+ ${SSH_SETUP}
157
+ - name: Load the deploy target
158
+ id: target
159
+ env:
160
+ SERVERS: \${{ secrets.SERVERS }}
161
+ APP: \${{ matrix.app }}
162
+ MANIFEST: \${{ matrix.manifest }}
163
+ run: |
164
+ SERVER_LABEL=$(jq -r --arg a "$APP" '.[$a].server' "$MANIFEST")
165
+ APP_PATH=$(jq -r --arg a "$APP" '.[$a].app_path' "$MANIFEST")
166
+ THEME_SLUG=$(jq -r --arg a "$APP" '.[$a].theme_slug // $a' "$MANIFEST")
167
+ ${SERVER_LOOKUP}
168
+ echo "target=$SSH_USER@$SERVER_IP" >> "$GITHUB_OUTPUT"
169
+ echo "app_path=$APP_PATH" >> "$GITHUB_OUTPUT"
170
+ echo "theme_slug=$THEME_SLUG" >> "$GITHUB_OUTPUT"
171
+
172
+ - name: Install plugins
173
+ working-directory: \${{ matrix.folder }}/\${{ matrix.app }}
174
+ run: |
175
+ if [ -f composer.json ]; then
176
+ composer install --no-dev --no-interaction --no-progress --optimize-autoloader
177
+ fi
178
+
179
+ - name: Deploy via rsync
180
+ uses: nick-fields/retry@v3
181
+ with:
182
+ timeout_minutes: 5
183
+ max_attempts: 3
184
+ command: |
185
+ rsync -rlz --omit-dir-times --delete \\
186
+ --exclude='.git' --exclude='node_modules' --exclude='.env' \\
187
+ -e "ssh -i ~/.ssh/id_ed25519" \\
188
+ \${{ matrix.folder }}/\${{ matrix.app }}/ \\
189
+ \${{ steps.target.outputs.target }}:\${{ steps.target.outputs.app_path }}/wp-content/themes/\${{ steps.target.outputs.theme_slug }}/
190
+
191
+ - name: Activate theme and flush cache
192
+ uses: nick-fields/retry@v3
193
+ with:
194
+ timeout_minutes: 5
195
+ max_attempts: 3
196
+ command: |
197
+ ssh -i ~/.ssh/id_ed25519 \${{ steps.target.outputs.target }} \\
198
+ "cd \${{ steps.target.outputs.app_path }} && \\
199
+ wp theme activate \${{ steps.target.outputs.theme_slug }} --allow-root && \\
200
+ wp rewrite flush --allow-root && \\
201
+ wp cache flush --allow-root && \\
202
+ (curl -X PURGE http://127.0.0.1/ || true)"
203
+ `;
204
+
205
+ export const DEPLOY_PLATFORM = `name: deploy platform
206
+
207
+ on:
208
+ push:
209
+ branches: [main]
210
+ paths:
211
+ - 'wp-operations/**'
212
+ - 'platform.json'
213
+ workflow_dispatch:
214
+
215
+ concurrency:
216
+ group: deploy-platform
217
+ cancel-in-progress: false
218
+
219
+ jobs:
220
+ deploy:
221
+ runs-on: ubuntu-latest
222
+ steps:
223
+ - uses: actions/checkout@v4
224
+
225
+ ${SSH_SETUP}
226
+ - name: Load the deploy target
227
+ id: target
228
+ env:
229
+ SERVERS: \${{ secrets.SERVERS }}
230
+ run: |
231
+ SERVER_LABEL=$(jq -r '.platform.server' .github/platform.json)
232
+ APP_PATH=$(jq -r '.platform.app_path' .github/platform.json)
233
+ THEME_SLUG=$(jq -r '.platform.theme_slug // "wp-operations"' .github/platform.json)
234
+ if [ -z "$SERVER_LABEL" ] || [ -z "$APP_PATH" ]; then echo "Set platform.server and platform.app_path in .github/platform.json."; exit 1; fi
235
+ ${SERVER_LOOKUP}
236
+ echo "target=$SSH_USER@$SERVER_IP" >> "$GITHUB_OUTPUT"
237
+ echo "app_path=$APP_PATH" >> "$GITHUB_OUTPUT"
238
+ echo "theme_slug=$THEME_SLUG" >> "$GITHUB_OUTPUT"
239
+
240
+ - name: Use this repository's platform.json
241
+ run: cp platform.json wp-operations/platform.json
242
+
243
+ - name: Deploy via rsync
244
+ uses: nick-fields/retry@v3
245
+ with:
246
+ timeout_minutes: 5
247
+ max_attempts: 3
248
+ command: |
249
+ rsync -rlz --omit-dir-times --delete \\
250
+ --exclude='.git' --exclude='node_modules' \\
251
+ -e "ssh -i ~/.ssh/id_ed25519" \\
252
+ wp-operations/ \\
253
+ \${{ steps.target.outputs.target }}:\${{ steps.target.outputs.app_path }}/wp-content/themes/\${{ steps.target.outputs.theme_slug }}/
254
+
255
+ - name: Activate the platform and flush cache
256
+ uses: nick-fields/retry@v3
257
+ with:
258
+ timeout_minutes: 5
259
+ max_attempts: 3
260
+ command: |
261
+ ssh -i ~/.ssh/id_ed25519 \${{ steps.target.outputs.target }} \\
262
+ "cd \${{ steps.target.outputs.app_path }} && \\
263
+ wp theme activate \${{ steps.target.outputs.theme_slug }} --allow-root && \\
264
+ wp cache flush --allow-root && \\
265
+ (curl -X PURGE http://127.0.0.1/ || true)"
266
+ `;
267
+
77
268
  export function appStyle(slug) {
78
269
  return `/*
79
270
  Theme Name: ${slug}