@loopress/cli 0.9.0 → 0.11.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.
Files changed (37) hide show
  1. package/README.md +41 -35
  2. package/dist/commands/init.js +24 -10
  3. package/dist/commands/project/config.js +1 -1
  4. package/dist/commands/project/remove.js +3 -2
  5. package/dist/commands/project/switch.d.ts +1 -2
  6. package/dist/commands/project/switch.js +28 -34
  7. package/dist/commands/project/sync.d.ts +13 -0
  8. package/dist/commands/project/sync.js +210 -0
  9. package/dist/commands/snippet/list.d.ts +0 -1
  10. package/dist/commands/snippet/list.js +4 -7
  11. package/dist/commands/snippet/pull.d.ts +1 -2
  12. package/dist/commands/snippet/pull.js +6 -10
  13. package/dist/commands/snippet/push.d.ts +1 -1
  14. package/dist/commands/snippet/push.js +37 -16
  15. package/dist/config/project-config.manager.d.ts +3 -1
  16. package/dist/config/project-config.manager.js +27 -3
  17. package/dist/help.js +1 -1
  18. package/dist/lib/api-client.d.ts +14 -0
  19. package/dist/lib/api-client.js +46 -0
  20. package/dist/lib/base.d.ts +0 -1
  21. package/dist/lib/base.js +0 -5
  22. package/dist/lib/push-command.js +1 -1
  23. package/dist/lib/wp-client.d.ts +1 -0
  24. package/dist/lib/wp-client.js +4 -0
  25. package/dist/types/global-config.generated.d.ts +8 -0
  26. package/dist/types/project-config.generated.d.ts +0 -4
  27. package/dist/types/snippet.d.ts +1 -1
  28. package/dist/utils/{snippet-plugin.d.ts → snippet-format.d.ts} +3 -19
  29. package/dist/utils/snippet-format.js +57 -0
  30. package/oclif.manifest.json +28 -43
  31. package/package.json +7 -1
  32. package/schemas/global-config.schema.json +91 -0
  33. package/schemas/project-config.schema.json +42 -0
  34. package/schemas/snippet.schema.json +61 -0
  35. package/dist/utils/snippet-plugin-flag.d.ts +0 -3
  36. package/dist/utils/snippet-plugin-flag.js +0 -8
  37. package/dist/utils/snippet-plugin.js +0 -232
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@loopress/cli",
3
3
  "description": "CLI tool for syncing WordPress code snippets, plugins, and Composer dependencies via the REST API",
4
- "version": "0.9.0",
4
+ "version": "0.11.0",
5
5
  "author": "jean-smaug",
6
6
  "bin": {
7
7
  "loopress": "bin/run.js",
@@ -26,6 +26,8 @@
26
26
  "@eslint/compat": "2.1.0",
27
27
  "@oclif/prettier-config": "^0.2.1",
28
28
  "@oclif/test": "^4.1.20",
29
+ "@stryker-mutator/core": "^9.6.1",
30
+ "@stryker-mutator/vitest-runner": "^9.6.1",
29
31
  "@types/node": "26.0.1",
30
32
  "@types/write-file-atomic": "^4.0.3",
31
33
  "@vitest/coverage-v8": "4.1.9",
@@ -36,6 +38,7 @@
36
38
  "oclif": "^4.23.24",
37
39
  "prettier": "3.9.4",
38
40
  "shx": "^0.4.0",
41
+ "ts-node": "10.9.2",
39
42
  "tsx": "4.22.4",
40
43
  "typescript": "^6.0.3",
41
44
  "vitest": "^4.1.9"
@@ -46,6 +49,7 @@
46
49
  "files": [
47
50
  "./bin",
48
51
  "./dist",
52
+ "./schemas",
49
53
  "./oclif.manifest.json"
50
54
  ],
51
55
  "homepage": "https://github.com/loopress/loopress",
@@ -110,6 +114,8 @@
110
114
  "posttest": "pnpm run lint",
111
115
  "schema:types": "tsx scripts/generate-config-types.ts",
112
116
  "test": "vitest run",
117
+ "pretest:mutation": "pnpm run schema:types",
118
+ "test:mutation": "stryker run",
113
119
  "version": "oclif readme && git add README.md",
114
120
  "format": "prettier . --write"
115
121
  }
@@ -0,0 +1,91 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://loopress.dev/schema/global-config.json",
4
+ "title": "Loopress global configuration",
5
+ "description": "Global CLI state stored at ~/.loopress/config.json: known projects, their environments, and which one is currently active.",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": ["currentProject", "projects"],
9
+ "properties": {
10
+ "currentProject": {
11
+ "description": "Pointer to the currently active project and environment, or null if none is selected.",
12
+ "oneOf": [{"type": "null"}, {"$ref": "#/definitions/CurrentProjectPointer"}]
13
+ },
14
+ "projects": {
15
+ "type": "object",
16
+ "description": "Known projects, keyed by project id.",
17
+ "additionalProperties": {"$ref": "#/definitions/ProjectConfig"}
18
+ }
19
+ },
20
+ "definitions": {
21
+ "CurrentProjectPointer": {
22
+ "title": "CurrentProjectPointer",
23
+ "type": "object",
24
+ "additionalProperties": false,
25
+ "required": ["env", "id"],
26
+ "properties": {
27
+ "env": {
28
+ "type": "string",
29
+ "description": "Name of the currently active environment."
30
+ },
31
+ "id": {
32
+ "type": "string",
33
+ "description": "Id of the currently active project."
34
+ }
35
+ }
36
+ },
37
+ "ProjectConfig": {
38
+ "title": "ProjectConfig",
39
+ "type": "object",
40
+ "additionalProperties": false,
41
+ "required": ["addedAt", "environments", "name"],
42
+ "properties": {
43
+ "addedAt": {
44
+ "type": "string",
45
+ "description": "ISO timestamp of when the project was added."
46
+ },
47
+ "apiProjectId": {
48
+ "type": "string",
49
+ "description": "Id of the matching project on the Loopress API, once synced via `lps project sync`."
50
+ },
51
+ "environments": {
52
+ "type": "object",
53
+ "description": "Environments for this project, keyed by environment name.",
54
+ "additionalProperties": {"$ref": "#/definitions/EnvironmentConfig"}
55
+ },
56
+ "name": {
57
+ "type": "string",
58
+ "description": "Human-readable project name."
59
+ }
60
+ }
61
+ },
62
+ "EnvironmentConfig": {
63
+ "title": "EnvironmentConfig",
64
+ "type": "object",
65
+ "additionalProperties": false,
66
+ "required": ["addedAt", "name", "url"],
67
+ "properties": {
68
+ "addedAt": {
69
+ "type": "string",
70
+ "description": "ISO timestamp of when the environment was added."
71
+ },
72
+ "apiEnvironmentId": {
73
+ "type": "string",
74
+ "description": "Id of the matching environment on the Loopress API, once synced via `lps project sync`."
75
+ },
76
+ "name": {
77
+ "type": "string",
78
+ "description": "Environment name (e.g. \"production\", \"staging\")."
79
+ },
80
+ "token": {
81
+ "type": "string",
82
+ "description": "API token used to authenticate against this environment."
83
+ },
84
+ "url": {
85
+ "type": "string",
86
+ "description": "Base URL of the WordPress site for this environment."
87
+ }
88
+ }
89
+ }
90
+ }
91
+ }
@@ -0,0 +1,42 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://loopress.dev/schema/project-config.json",
4
+ "title": "Loopress project configuration",
5
+ "description": "Project-level config for the Loopress CLI (loopress.json).",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "properties": {
9
+ "$schema": {
10
+ "type": "string"
11
+ },
12
+ "rootDir": {
13
+ "type": "string",
14
+ "description": "Base directory for all Loopress paths. All other path options are resolved relative to this directory.",
15
+ "default": "."
16
+ },
17
+ "snippetsDir": {
18
+ "type": "string",
19
+ "description": "Directory where code snippets are read from and written to, relative to rootDir.",
20
+ "default": "snippets"
21
+ },
22
+ "projectId": {
23
+ "type": "string",
24
+ "description": "Project identifier from the global Loopress config. When set, takes precedence over the currently active project in the global config."
25
+ },
26
+ "plugins": {
27
+ "type": "object",
28
+ "description": "Pinned plugin versions. Keys are WordPress.org plugin slugs, values are version constraints.",
29
+ "additionalProperties": {
30
+ "type": "string",
31
+ "description": "Version to pin. Use an exact version (e.g. \"8.9.1\") or \"latest\".",
32
+ "examples": ["8.9.1", "latest"]
33
+ },
34
+ "examples": [
35
+ {
36
+ "woocommerce": "8.9.1",
37
+ "contact-form-7": "latest"
38
+ }
39
+ ]
40
+ }
41
+ }
42
+ }
@@ -0,0 +1,61 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://loopress.dev/schema/snippet.json",
4
+ "title": "Loopress snippet metadata",
5
+ "description": "Sidecar .json file paired with a snippet's code file in the snippets directory (e.g. `123-my-snippet.php` + `123-my-snippet.json`).",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "properties": {
9
+ "$schema": {
10
+ "type": "string"
11
+ },
12
+ "id": {
13
+ "type": "integer",
14
+ "description": "Remote snippet id. Omitted until the snippet has been pushed for the first time."
15
+ },
16
+ "name": {
17
+ "type": "string",
18
+ "description": "Snippet name/title. Defaults to the code file's name when omitted."
19
+ },
20
+ "type": {
21
+ "type": "string",
22
+ "description": "Snippet language. Defaults to the type inferred from the code file's extension when omitted.",
23
+ "enum": ["css", "html", "js", "php", "text"]
24
+ },
25
+ "active": {
26
+ "type": "boolean",
27
+ "description": "Whether the snippet is enabled.",
28
+ "default": false
29
+ },
30
+ "location": {
31
+ "type": "string",
32
+ "description": "Where the snippet runs. Supported values depend on the target plugin and snippet type.",
33
+ "enum": ["admin", "body", "everywhere", "footer", "frontend", "header", "once"]
34
+ },
35
+ "description": {
36
+ "type": "string",
37
+ "description": "Free-text note about the snippet."
38
+ },
39
+ "tags": {
40
+ "type": "array",
41
+ "description": "Tags for organizing snippets.",
42
+ "items": {"type": "string"}
43
+ },
44
+ "insertMethod": {
45
+ "type": "string",
46
+ "description": "How the snippet is inserted into the page.",
47
+ "enum": ["auto", "shortcode"],
48
+ "default": "auto"
49
+ },
50
+ "priority": {
51
+ "type": "integer",
52
+ "description": "Execution priority. Lower runs earlier.",
53
+ "default": 10
54
+ },
55
+ "shortcodeAttributes": {
56
+ "type": "array",
57
+ "description": "Attribute names accepted by the snippet's shortcode, used when insertMethod is \"shortcode\".",
58
+ "items": {"type": "string"}
59
+ }
60
+ }
61
+ }
@@ -1,3 +0,0 @@
1
- export declare const snippetPluginFlag: {
2
- plugin: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
3
- };
@@ -1,8 +0,0 @@
1
- import { Flags } from '@oclif/core';
2
- export const snippetPluginFlag = {
3
- plugin: Flags.string({
4
- char: 'p',
5
- description: 'WordPress snippet plugin to target (overrides loopress.json)',
6
- options: ['code-snippets', 'wpcode'],
7
- }),
8
- };
@@ -1,232 +0,0 @@
1
- export function parseType(raw) {
2
- const valid = ['css', 'html', 'js', 'php', 'text'];
3
- const value = String(raw ?? '').toLowerCase();
4
- return valid.includes(value) ? value : null;
5
- }
6
- const VALID_LOCATIONS = new Set(['admin', 'body', 'everywhere', 'footer', 'frontend', 'header', 'once']);
7
- export function parseLocation(raw) {
8
- const value = String(raw ?? '').toLowerCase();
9
- return VALID_LOCATIONS.has(value) ? value : null;
10
- }
11
- export function parseInsertMethod(raw) {
12
- return raw === 'auto' || raw === 'shortcode' ? raw : null;
13
- }
14
- // The sensible default placement for a freshly pushed snippet that doesn't specify a location.
15
- export function defaultLocationForType(type) {
16
- switch (type) {
17
- case 'css': {
18
- return 'header';
19
- }
20
- case 'html':
21
- case 'js':
22
- case 'text': {
23
- return 'footer';
24
- }
25
- case 'php': {
26
- return 'everywhere';
27
- }
28
- }
29
- }
30
- function inferTypeFromCode(code) {
31
- const firstLine = code.trimStart().split('\n')[0].trimStart();
32
- if (firstLine.startsWith('<?'))
33
- return 'php';
34
- if (firstLine.startsWith('<'))
35
- return 'html';
36
- return 'php';
37
- }
38
- function resolveType(raw, code) {
39
- return parseType(raw) ?? inferTypeFromCode(code);
40
- }
41
- function resolvePriority(raw) {
42
- const value = Number(raw);
43
- return Number.isFinite(value) ? value : 10;
44
- }
45
- // Snippet files on disk keep the <?php opening tag so they're valid, syntax-highlighted PHP files.
46
- // Both plugins store just the executable body, so it's stripped before push and restored on pull
47
- // (see buildSnippetFile in commands/snippet/pull.ts).
48
- function stripPhpOpeningTag(code) {
49
- return code.replace(/^<\?php\s*/i, '');
50
- }
51
- // The real Code Snippets plugin has no independent "type" field: its REST API only has `scope`,
52
- // and the snippet type is derived from it (see WPCode_Snippet::get_type_from_scope() upstream).
53
- // Sending a `type` key (as this adapter used to) is silently ignored by that plugin.
54
- const CODE_SNIPPETS_SCOPE_TO_LOCATION = {
55
- admin: 'admin',
56
- 'admin-css': 'admin',
57
- content: 'everywhere',
58
- 'footer-content': 'footer',
59
- 'front-end': 'frontend',
60
- global: 'everywhere',
61
- 'head-content': 'header',
62
- 'single-use': 'once',
63
- 'site-css': 'frontend',
64
- 'site-footer-js': 'footer',
65
- 'site-head-js': 'header',
66
- };
67
- function typeFromScope(scope) {
68
- if (scope.endsWith('-css'))
69
- return 'css';
70
- if (scope.endsWith('-js'))
71
- return 'js';
72
- if (scope.endsWith('content'))
73
- return 'html';
74
- return 'php';
75
- }
76
- function scopeFromTypeAndLocation(type, location) {
77
- switch (type) {
78
- case 'css': {
79
- if (location === 'frontend')
80
- return 'site-css';
81
- if (location === 'admin')
82
- return 'admin-css';
83
- throw new Error(`Code Snippets does not support the "${location}" location for CSS snippets. Use one of: frontend, admin.`);
84
- }
85
- case 'html': {
86
- if (location === 'header')
87
- return 'head-content';
88
- if (location === 'footer')
89
- return 'footer-content';
90
- if (location === 'everywhere')
91
- return 'content';
92
- throw new Error(`Code Snippets does not support the "${location}" location for HTML snippets. Use one of: header, footer, everywhere.`);
93
- }
94
- case 'js': {
95
- if (location === 'header')
96
- return 'site-head-js';
97
- if (location === 'footer')
98
- return 'site-footer-js';
99
- throw new Error(`Code Snippets does not support the "${location}" location for JS snippets. Use one of: header, footer.`);
100
- }
101
- case 'php': {
102
- if (location === 'everywhere')
103
- return 'global';
104
- if (location === 'frontend')
105
- return 'front-end';
106
- if (location === 'admin')
107
- return 'admin';
108
- if (location === 'once')
109
- return 'single-use';
110
- throw new Error(`Code Snippets does not support the "${location}" location for PHP snippets. Use one of: everywhere, frontend, admin, once.`);
111
- }
112
- case 'text': {
113
- throw new Error('Code Snippets has no "text" snippet type. Change the sidecar "type", or push this snippet to "wpcode" instead.');
114
- }
115
- }
116
- }
117
- class CodeSnippetsPlugin {
118
- endpointPath() {
119
- return 'code-snippets/v1/snippets';
120
- }
121
- fromRemote(data) {
122
- const scope = String(data.scope ?? 'global');
123
- const type = typeFromScope(scope);
124
- return {
125
- active: Boolean(data.active),
126
- code: String(data.code ?? ''),
127
- description: String(data.desc ?? ''),
128
- id: Number(data.id),
129
- insertMethod: 'auto',
130
- location: CODE_SNIPPETS_SCOPE_TO_LOCATION[scope] ?? defaultLocationForType(type),
131
- name: String(data.name ?? ''),
132
- priority: resolvePriority(data.priority),
133
- shortcodeAttributes: [],
134
- tags: Array.isArray(data.tags) ? data.tags : [],
135
- type,
136
- };
137
- }
138
- toPayload({ active, code, location, name, path, priority, tags, type }) {
139
- return {
140
- active,
141
- code: stripPhpOpeningTag(code),
142
- desc: `Imported from ${path}`,
143
- name,
144
- priority,
145
- scope: scopeFromTypeAndLocation(type, location),
146
- tags,
147
- };
148
- }
149
- }
150
- // WPCode taxonomy term slugs (see wpcode_register_taxonomies() and the auto-insert location
151
- // classes upstream). 'everywhere' | 'frontend_only' | 'admin_only' | 'on_demand' only apply to
152
- // PHP snippets; 'site_wide_header' | 'site_wide_body' | 'site_wide_footer' apply to any type.
153
- const WPCODE_PHP_ONLY_LOCATIONS = {
154
- admin: 'admin_only',
155
- everywhere: 'everywhere',
156
- frontend: 'frontend_only',
157
- once: 'on_demand',
158
- };
159
- const WPCODE_UNIVERSAL_LOCATIONS = {
160
- body: 'site_wide_body',
161
- footer: 'site_wide_footer',
162
- header: 'site_wide_header',
163
- };
164
- const WPCODE_LOCATION_TO_CANONICAL = {
165
- 'admin_only': 'admin',
166
- everywhere: 'everywhere',
167
- 'frontend_only': 'frontend',
168
- 'on_demand': 'once',
169
- 'site_wide_body': 'body',
170
- 'site_wide_footer': 'footer',
171
- 'site_wide_header': 'header',
172
- };
173
- function wpcodeLocationTerm(type, location) {
174
- const universal = WPCODE_UNIVERSAL_LOCATIONS[location];
175
- if (universal)
176
- return universal;
177
- if (type === 'php') {
178
- const phpOnly = WPCODE_PHP_ONLY_LOCATIONS[location];
179
- if (phpOnly)
180
- return phpOnly;
181
- }
182
- const allowed = type === 'php' ? 'header, body, footer, everywhere, frontend, admin, once' : 'header, body, footer';
183
- throw new Error(`WPCode does not support the "${location}" location for ${type} snippets. Use one of: ${allowed}.`);
184
- }
185
- class WPCodePlugin {
186
- endpointPath() {
187
- return 'loopress/v1/wpcode/snippets';
188
- }
189
- fromRemote(data) {
190
- const type = resolveType(data.type, String(data.code ?? ''));
191
- return {
192
- active: Boolean(data.active),
193
- code: String(data.code ?? ''),
194
- description: String(data.note ?? ''),
195
- id: Number(data.id),
196
- insertMethod: data.insert_method === 'shortcode' ? 'shortcode' : 'auto',
197
- location: WPCODE_LOCATION_TO_CANONICAL[String(data.location)] ?? defaultLocationForType(type),
198
- name: String(data.title ?? ''),
199
- priority: resolvePriority(data.priority),
200
- shortcodeAttributes: Array.isArray(data.shortcode_attributes) ? data.shortcode_attributes.map(String) : [],
201
- tags: Array.isArray(data.tags) ? data.tags : [],
202
- type,
203
- };
204
- }
205
- toPayload({ active, code, insertMethod, location, name, path, priority, shortcodeAttributes, tags, type, }) {
206
- const placement = insertMethod === 'shortcode'
207
- ? { 'shortcode_attributes': shortcodeAttributes }
208
- : { location: wpcodeLocationTerm(type, location) };
209
- return {
210
- active,
211
- code: stripPhpOpeningTag(code),
212
- note: `Imported from ${path}`,
213
- priority,
214
- tags,
215
- title: name,
216
- type,
217
- ...placement,
218
- 'insert_method': insertMethod,
219
- };
220
- }
221
- }
222
- export function getSnippetPlugin(name) {
223
- switch (name) {
224
- case 'wpcode': {
225
- return new WPCodePlugin();
226
- }
227
- case 'code-snippets':
228
- default: {
229
- return new CodeSnippetsPlugin();
230
- }
231
- }
232
- }