@wp-operations/wp-ops 0.6.0 → 0.7.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
@@ -34,6 +34,7 @@ wp-ops doctor
34
34
  | Command | What it does |
35
35
  |---|---|
36
36
  | `init [dir]` | Asks for each section of the project's `platform.json` (name, author, website, languages and default language, content types and their fields, taxonomies, modules, brand color, app categories) and writes it. Start from the default content types or from empty. Then creates the platform theme in `wp-operations/`, `.github/platform.json`, the app manifests, `packages/`, the app folders, the workflows and `.gitignore`, and runs `git init`. `--yes` takes the default `platform.json`; `--template=empty` starts with no content types. Never overwrites a file. |
37
+ | `github <owner/name>` | Creates the GitHub repository if it does not exist (private unless `--public`), sets it as `origin`, commits and pushes `main`. `init` asks for it at the end (or `--github=owner/name`). Uses `GITHUB_TOKEN`, or the token git already uses for github.com. |
37
38
  | `start` | Runs the platform (`wp-operations/`) locally. Site data stays in `.local/` (gitignored). `wp-ops start <dir>` runs one app instead. |
38
39
  | `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. |
39
40
  | `new app <kind> <slug>` | Creates the app (composer type `wp-app`) and adds it to the kind's manifest. |
@@ -60,6 +61,7 @@ Repository secrets:
60
61
  |---|---|
61
62
  | `SSH_PRIVATE_KEY` | The deploy key. |
62
63
  | `SERVERS` | `label:ip:user label2:ip2:user2`, where each label is a `server` value in the manifests. |
64
+ | `PLATFORM_GITHUB_TOKEN` | The token the platform uses for this repository. `deploy-platform.yml` writes it into the server's `wp-config.php` and sets the platform's repository and branch (Settings → Operations) to this repository, so they are never filled in by hand. |
63
65
 
64
66
  ## Sandbox
65
67
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wp-operations/wp-ops",
3
- "version": "0.6.0",
3
+ "version": "0.7.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",
@@ -0,0 +1,74 @@
1
+ import { execFileSync, spawnSync } from 'child_process';
2
+ import pc from 'picocolors';
3
+ import { UsageError } from '../args.js';
4
+
5
+ const API = 'https://api.github.com';
6
+
7
+ export function parseRepo(value) {
8
+ const match = String(value || '').trim().replace(/\.git$/, '').match(/(?:github\.com[/:])?([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)$/);
9
+ return match ? { owner: match[1], name: match[2] } : null;
10
+ }
11
+
12
+ /**
13
+ * GITHUB_TOKEN / GH_TOKEN, else the token git already uses for github.com.
14
+ */
15
+ export function findToken() {
16
+ const env = process.env.GITHUB_TOKEN || process.env.GH_TOKEN;
17
+ if (env) return env.trim();
18
+ const result = spawnSync('git', ['credential', 'fill'], { input: 'protocol=https\nhost=github.com\n\n', encoding: 'utf8', env: { ...process.env, GIT_TERMINAL_PROMPT: '0' } });
19
+ return (result.stdout || '').match(/^password=(.+)$/m)?.[1]?.trim() || '';
20
+ }
21
+
22
+ async function api(token, method, path, body) {
23
+ const response = await fetch(API + path, {
24
+ method,
25
+ headers: { Authorization: `Bearer ${token}`, Accept: 'application/vnd.github+json', 'User-Agent': 'wp-ops', ...(body ? { 'Content-Type': 'application/json' } : {}) },
26
+ body: body ? JSON.stringify(body) : undefined,
27
+ });
28
+ const data = await response.json().catch(() => ({}));
29
+ return { status: response.status, data };
30
+ }
31
+
32
+ /**
33
+ * Creates the GitHub repository when it does not exist, sets it as origin,
34
+ * commits everything and pushes main.
35
+ */
36
+ export async function connectGithub({ root, repo, isPrivate = true, token = findToken(), log = console.log }) {
37
+ const target = parseRepo(repo);
38
+ if (!target) throw new UsageError(`"${repo}" is not a GitHub repository. Use owner/name.`);
39
+ if (!token) throw new UsageError('No GitHub token found. Sign in to GitHub with git once, or set GITHUB_TOKEN, then run: wp-ops github owner/name');
40
+
41
+ const user = await api(token, 'GET', '/user');
42
+ if (user.status !== 200) throw new UsageError(`GitHub did not accept the token (${user.status}).`);
43
+
44
+ const existing = await api(token, 'GET', `/repos/${target.owner}/${target.name}`);
45
+ if (existing.status === 200) {
46
+ log(` ${pc.green('✓')} github.com/${target.owner}/${target.name} exists`);
47
+ } else {
48
+ const path = user.data.login?.toLowerCase() === target.owner.toLowerCase() ? '/user/repos' : `/orgs/${target.owner}/repos`;
49
+ const created = await api(token, 'POST', path, { name: target.name, private: isPrivate, auto_init: false });
50
+ if (created.status !== 201) throw new UsageError(`Could not create ${target.owner}/${target.name}: ${created.data.message || created.status}`);
51
+ log(` ${pc.green('+')} github.com/${target.owner}/${target.name} (${isPrivate ? 'private' : 'public'})`);
52
+ }
53
+
54
+ const url = `https://github.com/${target.owner}/${target.name}.git`;
55
+ const git = (...args) => execFileSync('git', args, { cwd: root, stdio: 'pipe', encoding: 'utf8' });
56
+ const remote = spawnSync('git', ['remote', 'get-url', 'origin'], { cwd: root, encoding: 'utf8' });
57
+ if (remote.status !== 0) git('remote', 'add', 'origin', url);
58
+ else if (remote.stdout.trim() !== url) git('remote', 'set-url', 'origin', url);
59
+
60
+ git('add', '-A');
61
+ if (spawnSync('git', ['diff', '--cached', '--quiet'], { cwd: root }).status !== 0) {
62
+ git('commit', '-m', 'Platform repository (wp-ops init)');
63
+ }
64
+ const push = spawnSync('git', ['push', '-u', 'origin', 'main'], { cwd: root, encoding: 'utf8' });
65
+ if (push.status !== 0) throw new UsageError(`git push failed:\n${push.stderr}`);
66
+ log(` ${pc.green('✓')} pushed main to origin`);
67
+ log(`\n Add these secrets once (Settings → Secrets and variables → Actions):`);
68
+ log(` ${pc.bold(`https://github.com/${target.owner}/${target.name}/settings/secrets/actions`)}`);
69
+ log(' SSH_PRIVATE_KEY the deploy key');
70
+ log(' SERVERS label:ip:user label2:ip2:user2');
71
+ log(' PLATFORM_GITHUB_TOKEN the token the platform uses for this repository');
72
+ log(pc.dim(' The deploy sets the platform\'s GitHub settings (repository, branch, token) itself.'));
73
+ return target;
74
+ }
package/src/main.js CHANGED
@@ -9,6 +9,7 @@ import { doctor } from './commands/doctor.js';
9
9
  import * as p from '@clack/prompts';
10
10
  import { PLATFORM_DIR, apps, bundledPlatform, composer, init, newApp, newPlugin, platformRoot, plugins, update } from './commands/platform.js';
11
11
  import { askPlatform } from './commands/setup.js';
12
+ import { connectGithub } from './commands/github.js';
12
13
  import { sandbox } from './commands/sandbox.js';
13
14
  import { tui } from './tui.js';
14
15
 
@@ -24,7 +25,9 @@ Platform repository:
24
25
  wp-ops init [dir] create a platform repository: asks for each section of platform.json
25
26
  (name, languages, content types, taxonomies, modules, app), then
26
27
  writes the platform (wp-operations/), layout, workflows and git
27
- [--yes: default platform.json] [--template=empty]
28
+ [--yes: default platform.json] [--template=empty] [--github=owner/name] [--public]
29
+ wp-ops github <owner/name> [--public]
30
+ create the GitHub repository (if needed), set it as origin, commit and push main
28
31
  wp-ops start [--port=] run the platform locally (admin / password)
29
32
  wp-ops update update wp-operations/ to the platform shipped with this wp-ops
30
33
  wp-ops apps list apps (<kind>/<slug>) and their deploy targets
@@ -87,11 +90,33 @@ async function run() {
87
90
  p.cancel('Nothing created.');
88
91
  return;
89
92
  }
93
+ p.log.step('github');
94
+ const repo = await p.text({ message: 'GitHub repository to create and push to (owner/name, empty to skip)', placeholder: `your-account/${path.basename(dir)}`, defaultValue: '' });
95
+ if (p.isCancel(repo)) {
96
+ p.cancel('Nothing created.');
97
+ return;
98
+ }
99
+ if (repo.trim()) {
100
+ const isPrivate = await p.confirm({ message: 'Private repository?', initialValue: true });
101
+ if (p.isCancel(isPrivate)) {
102
+ p.cancel('Nothing created.');
103
+ return;
104
+ }
105
+ options.github = repo.trim();
106
+ options.public = !isPrivate;
107
+ }
90
108
  p.outro('Creating the platform repository');
91
109
  }
92
110
  await init({ dir, platform: options.platform, version: pkg.version, answers });
111
+ if (options.github && options.github !== true) {
112
+ console.log('');
113
+ await connectGithub({ root: dir, repo: options.github, isPrivate: options.public !== true });
114
+ }
93
115
  }
94
116
  return;
117
+ case 'github':
118
+ await connectGithub({ root: await platformRoot(), repo: positional[0], isPrivate: options.public !== true });
119
+ return;
95
120
  case 'apps':
96
121
  await apps({ root: await platformRoot() });
97
122
  return;
package/src/templates.js CHANGED
@@ -278,6 +278,41 @@ ${SERVER_LOOKUP}
278
278
  wp theme activate \${{ steps.target.outputs.theme_slug }} --allow-root && \\
279
279
  wp cache flush --allow-root && \\
280
280
  (curl -X PURGE http://127.0.0.1/ || true)"
281
+
282
+ # The platform's GitHub settings come from here: this repository, this
283
+ # branch, and the PLATFORM_GITHUB_TOKEN secret. Nobody fills them in.
284
+ - name: Connect the platform to this repository
285
+ if: steps.target.outputs.skip != 'true'
286
+ env:
287
+ TARGET: \${{ steps.target.outputs.target }}
288
+ APP_PATH: \${{ steps.target.outputs.app_path }}
289
+ PLATFORM_GITHUB_TOKEN: \${{ secrets.PLATFORM_GITHUB_TOKEN }}
290
+ run: |
291
+ if [ -n "$PLATFORM_GITHUB_TOKEN" ]; then
292
+ printf '%s\\n' "$PLATFORM_GITHUB_TOKEN" | ssh -i ~/.ssh/id_ed25519 "$TARGET" \\
293
+ "cd $APP_PATH && read -r T && wp config set PLATFORM_GITHUB_TOKEN \\"\\$T\\" --type=constant --allow-root --quiet"
294
+ else
295
+ echo "::notice::Add the PLATFORM_GITHUB_TOKEN secret so the platform can reach this repository."
296
+ fi
297
+ cat > connect.php <<'PHP'
298
+ <?php
299
+ if ( ! class_exists( 'Platform\\App\\Ops' ) ) {
300
+ WP_CLI::error( 'The wp-operations platform is not the active theme.' );
301
+ }
302
+ $saved = Platform\\App\\Ops::save( [ 'url' => 'https://github.com/' . getenv( 'PLATFORM_REPO' ), 'branch' => getenv( 'PLATFORM_BRANCH' ) ] );
303
+ if ( is_wp_error( $saved ) ) {
304
+ WP_CLI::error( $saved->get_error_message() );
305
+ }
306
+ if ( defined( 'PLATFORM_GITHUB_TOKEN' ) && '' !== PLATFORM_GITHUB_TOKEN ) {
307
+ $imported = Platform\\App\\Ops::import();
308
+ if ( is_wp_error( $imported ) ) {
309
+ WP_CLI::warning( $imported->get_error_message() );
310
+ }
311
+ }
312
+ WP_CLI::success( 'Platform connected to ' . getenv( 'PLATFORM_REPO' ) . ' (' . getenv( 'PLATFORM_BRANCH' ) . ').' );
313
+ PHP
314
+ ssh -i ~/.ssh/id_ed25519 "$TARGET" \\
315
+ "cd $APP_PATH && PLATFORM_REPO='\${{ github.repository }}' PLATFORM_BRANCH='\${{ github.ref_name }}' wp eval-file - --allow-root" < connect.php
281
316
  `;
282
317
 
283
318
  export function appStyle(slug) {
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.3
4
+
5
+ - App settings → Details renders every field from `platform.json` (`apps.metadata`) with its type, options and width, using the same fields as the CMS.
6
+ - The AI (Build, Plan, Chat) gets the app's configured details and links, labelled as `platform.json` defines them.
7
+ - Chat ideas and the new-app hint use the platform's own content types instead of fixed examples.
8
+
3
9
  ## 0.1.2
4
10
 
5
11
  - changed logo size from mine to my father's (tried to fool boss)