@stallforge/cli 0.0.0-stage → 0.2.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 +39 -2
- package/bin/cli.mjs +4 -0
- package/package.json +58 -4
- package/src/commands/create.mjs +117 -0
- package/src/commands/dev.mjs +106 -0
- package/src/commands/doctor.mjs +48 -0
- package/src/commands/publish.mjs +43 -0
- package/src/commands/test.mjs +83 -0
- package/src/commands/validate.mjs +35 -0
- package/src/index.mjs +9 -0
- package/src/lib/build.mjs +35 -0
- package/src/lib/bundle.mjs +132 -0
- package/src/lib/check.mjs +105 -0
- package/src/lib/inspect.mjs +38 -0
- package/src/lib/names.mjs +6 -0
- package/src/lib/package-info.mjs +19 -0
- package/src/lib/report.mjs +4 -0
- package/src/lib/templates.mjs +5 -0
- package/src/lib/usage.mjs +1 -0
- package/src/main.mjs +61 -0
- package/template/src/index.mjs +69 -0
- package/template/templates/_common/README.md.tpl +11 -0
- package/template/templates/_common/gitignore.tpl +2 -0
- package/template/templates/_common/package.json.tpl +18 -0
- package/template/templates/_common/test/manifest.test.ts.tpl +15 -0
- package/template/templates/_common/tsconfig.json.tpl +15 -0
- package/template/templates/example/plugin.json.tpl +13 -0
- package/template/templates/example/src/index.ts.tpl +15 -0
- package/template/templates/example/test/plugin.test.ts.tpl +23 -0
- package/template/templates/payment/plugin.json.tpl +136 -0
- package/template/templates/payment/src/index.ts.tpl +248 -0
- package/types/bundle.d.ts +39 -0
- package/types/index.d.ts +24 -0
- package/types/shims/brand.d.ts +8 -0
- package/types/shims/conformance.d.ts +11 -0
- package/types/shims/contracts.d.ts +22 -0
- package/types/shims/sdk-range.d.ts +2 -0
- package/types/shims/sdk-scan.d.ts +8 -0
- package/types/shims/sdk.d.ts +1 -0
- package/types/shims/slot-shapes.d.ts +4 -0
- package/types/shims/slot-suites.d.ts +9 -0
- package/types/shims/validate.d.ts +14 -0
package/README.md
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @stallforge/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Author tooling for creating, testing, validating and publishing plugins.
|
|
4
|
+
|
|
5
|
+
The SDK scan used by validate is a guardrail and not a sandbox.
|
|
6
|
+
|
|
7
|
+
## Commands
|
|
8
|
+
|
|
9
|
+
- `create NAME [--type example|payment] [--slot SLOT] [--scope-value VALUE] [--license SPDX] [--dir FOLDER]` — make a plugin from a template
|
|
10
|
+
- `dev [DIR] [--once] [--call HANDLER] [--input JSON]` — load the plugin in the test host and reload on change
|
|
11
|
+
- `test [DIR]` — run the unit tests and the slot suite of every claimed slot
|
|
12
|
+
- `validate [DIR_OR_BUNDLE_FOLDER]` — check the manifest, the SDK range, the bundle scan and capability use
|
|
13
|
+
- `publish [DIR] [--out FOLDER] [--source-tag TAG]` — build the bundle twice and write the bundle folder and registry entry
|
|
14
|
+
- `doctor [DIR]` — check this machine and the plugin in the folder
|
|
15
|
+
|
|
16
|
+
## Bundle layout
|
|
17
|
+
|
|
18
|
+
`publish` writes one folder per plugin version under the output root (default `dist/`):
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
dist/NAME-VERSION/
|
|
22
|
+
index.mjs single-file ES module, minified, no source map, nothing external except node: built-ins
|
|
23
|
+
plugin.json the source manifest with entry set to index.mjs; every other key and the key order unchanged;
|
|
24
|
+
serialized as JSON.stringify(value, null, 2) plus one newline (UTF-8, LF)
|
|
25
|
+
registry-entry.json serialized the same way
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`bundle.sha256` is the SHA-256 of the exact bytes of `index.mjs`; `manifest.sha256` is the SHA-256 of the exact bytes of `plugin.json` as written; both are lower-case hex. The same source builds to the same bundle bytes in any folder (two builds in different temporary folders are compared on every publish).
|
|
29
|
+
|
|
30
|
+
## Checks
|
|
31
|
+
|
|
32
|
+
- `npm test`
|
|
33
|
+
- `npm run lint`
|
|
34
|
+
- `npm run typecheck`
|
|
35
|
+
- `npm run brand:check`
|
|
36
|
+
- `npm run plan-ids:check`
|
|
37
|
+
|
|
38
|
+
## About the name
|
|
39
|
+
|
|
40
|
+
Stallforge is a placeholder product name until the owner confirms it is final.
|
package/bin/cli.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stallforge/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Author tooling: create, dev, test, validate, publish and doctor for plugins.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=24.15.0"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"bin",
|
|
12
|
+
"src",
|
|
13
|
+
"template",
|
|
14
|
+
"template/templates/_common/test",
|
|
15
|
+
"template/templates/example/test",
|
|
16
|
+
"types"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./types/index.d.ts",
|
|
21
|
+
"default": "./src/index.mjs"
|
|
22
|
+
},
|
|
23
|
+
"./bundle": {
|
|
24
|
+
"types": "./types/bundle.d.ts",
|
|
25
|
+
"default": "./src/lib/bundle.mjs"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"bin": {
|
|
30
|
+
"stallforge": "./bin/cli.mjs"
|
|
31
|
+
},
|
|
32
|
+
"imports": {
|
|
33
|
+
"#contracts/*": "@stallforge/contracts/*",
|
|
34
|
+
"#sdk": "@stallforge/plugin-sdk",
|
|
35
|
+
"#sdk/*": "@stallforge/plugin-sdk/*"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public",
|
|
39
|
+
"provenance": false
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"lint": "biome check --error-on-warnings .",
|
|
43
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
44
|
+
"templates:check": "node tools/sync-template.mjs --check",
|
|
45
|
+
"templates:write": "node tools/sync-template.mjs --write",
|
|
46
|
+
"test": "node --test \"test/**/*.test.mjs\"",
|
|
47
|
+
"brand:check": "node ../../scripts/brand-check.mjs --root ../..",
|
|
48
|
+
"plan-ids:check": "node ../../scripts/no-plan-ids.mjs --root ../.."
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@stallforge/contracts": "0.4.0",
|
|
52
|
+
"@stallforge/plugin-sdk": "0.4.0",
|
|
53
|
+
"rolldown": "1.3.4"
|
|
54
|
+
},
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "git+https://github.com/stallforge/stallforge-monorepo.git",
|
|
58
|
+
"directory": "packages/cli"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { SLOTS } from '#contracts/src/manifest/validate.mjs';
|
|
5
|
+
import { exampleOf, getShape } from '#contracts/src/slot-shapes/index.mjs';
|
|
6
|
+
import { SDK_VERSION } from '#sdk';
|
|
7
|
+
import own from '../../package.json' with { type: 'json' };
|
|
8
|
+
import { CLI_NAME, CLI_PACKAGE, SDK_PACKAGE } from '../lib/names.mjs';
|
|
9
|
+
import { loadTemplates } from '../lib/templates.mjs';
|
|
10
|
+
import { UsageError } from '../lib/usage.mjs';
|
|
11
|
+
|
|
12
|
+
const NAME = /^[a-z][a-z0-9-]{0,62}$/;
|
|
13
|
+
const LICENSE = /^[A-Za-z0-9.+() -]{1,100}$/;
|
|
14
|
+
const GENERATED = ['plugin.json', 'src/index.ts', 'test/plugin.test.ts'];
|
|
15
|
+
|
|
16
|
+
// A plugin that claims a registry slot: the manifest claim uses the registry mode, and each registry handler gets a stub
|
|
17
|
+
// that answers with the shape's example output, so the slot suite tells the author what is left to implement.
|
|
18
|
+
function slotFiles(slot, name, values, scopeValue) {
|
|
19
|
+
const claim = { slot: slot.name, mode: slot.mode, ...(slot.scope === null ? {} : { scope_value: scopeValue }) };
|
|
20
|
+
const manifest = {
|
|
21
|
+
manifest_version: 0,
|
|
22
|
+
name,
|
|
23
|
+
version: '0.1.0',
|
|
24
|
+
license: values.LICENSE,
|
|
25
|
+
type: slot.name,
|
|
26
|
+
sdk: values.SDK_RANGE,
|
|
27
|
+
entry: 'src/index.ts',
|
|
28
|
+
capabilities: [],
|
|
29
|
+
slots: [claim],
|
|
30
|
+
};
|
|
31
|
+
const shape = getShape(slot.name);
|
|
32
|
+
const stubs = (slot.handlers ?? []).map(
|
|
33
|
+
(handler) =>
|
|
34
|
+
` async ${handler}(_input, _ctx) {\n return ${JSON.stringify(exampleOf(shape, handler)?.output ?? null)};\n },`,
|
|
35
|
+
);
|
|
36
|
+
const source = [
|
|
37
|
+
`import type { Handlers, PluginContext, PluginModule } from '${SDK_PACKAGE}';`,
|
|
38
|
+
'',
|
|
39
|
+
'export default {',
|
|
40
|
+
' async activate(_ctx: PluginContext) {},',
|
|
41
|
+
' async dispose() {},',
|
|
42
|
+
'} satisfies PluginModule;',
|
|
43
|
+
'',
|
|
44
|
+
'export const handlers: Handlers = {',
|
|
45
|
+
...stubs,
|
|
46
|
+
'};',
|
|
47
|
+
'',
|
|
48
|
+
].join('\n');
|
|
49
|
+
return [
|
|
50
|
+
{ path: 'plugin.json', text: `${JSON.stringify(manifest, null, 2)}\n` },
|
|
51
|
+
{ path: 'src/index.ts', text: source },
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function create(argv, ctx) {
|
|
56
|
+
const { values, positionals } = parseArgs({
|
|
57
|
+
args: argv,
|
|
58
|
+
allowPositionals: true,
|
|
59
|
+
options: {
|
|
60
|
+
type: { type: 'string', default: 'example' },
|
|
61
|
+
slot: { type: 'string' },
|
|
62
|
+
'scope-value': { type: 'string' },
|
|
63
|
+
license: { type: 'string', default: 'MIT-0' },
|
|
64
|
+
dir: { type: 'string' },
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
const [name] = positionals;
|
|
68
|
+
if (!name)
|
|
69
|
+
throw new UsageError(
|
|
70
|
+
`usage: ${CLI_NAME} create NAME [--type TEMPLATE] [--slot SLOT] [--scope-value VALUE] [--license SPDX] [--dir FOLDER]`,
|
|
71
|
+
);
|
|
72
|
+
if (!NAME.test(name))
|
|
73
|
+
throw new UsageError(
|
|
74
|
+
`"${name}" is not a valid plugin name; use lower case letters, digits and hyphens, starting with a letter (at most 63 characters)`,
|
|
75
|
+
);
|
|
76
|
+
if (!LICENSE.test(values.license)) throw new UsageError(`"${values.license}" is not a valid license identifier`);
|
|
77
|
+
const templates = await loadTemplates();
|
|
78
|
+
const types = templates.listTemplates();
|
|
79
|
+
if (!types.includes(values.type))
|
|
80
|
+
throw new UsageError(`unknown template "${values.type}"; available: ${types.join(', ')}`);
|
|
81
|
+
/** @type {{ name: string, mode: string, handlers?: string[] | null, scope?: string | null } | null} */
|
|
82
|
+
let slot = null;
|
|
83
|
+
if (values.slot !== undefined) {
|
|
84
|
+
slot = SLOTS.slots.find((item) => item.name === values.slot) ?? null;
|
|
85
|
+
if (!slot)
|
|
86
|
+
throw new UsageError(
|
|
87
|
+
`unknown slot "${values.slot}"; slots are: ${SLOTS.slots.map((item) => item.name).join(', ')}`,
|
|
88
|
+
);
|
|
89
|
+
if (values.type !== 'example')
|
|
90
|
+
throw new UsageError('--slot starts from the example template; the payment template already claims its slot');
|
|
91
|
+
if (slot.scope !== null && values['scope-value'] === undefined)
|
|
92
|
+
throw new UsageError(`slot ${slot.name} is scoped to ${slot.scope}; pass --scope-value VALUE`);
|
|
93
|
+
}
|
|
94
|
+
const target = path.resolve(ctx.cwd, values.dir ?? name);
|
|
95
|
+
if (fs.existsSync(target) && fs.readdirSync(target).length > 0)
|
|
96
|
+
throw new Error(`${target} already exists and is not empty`);
|
|
97
|
+
const vars = {
|
|
98
|
+
PLUGIN_NAME: name,
|
|
99
|
+
LICENSE: values.license,
|
|
100
|
+
SDK_PACKAGE,
|
|
101
|
+
SDK_RANGE: `^${SDK_VERSION}`,
|
|
102
|
+
CLI_PACKAGE,
|
|
103
|
+
CLI_RANGE: `^${own.version}`,
|
|
104
|
+
CLI_NAME,
|
|
105
|
+
};
|
|
106
|
+
const files = templates.templateFiles(values.type).filter((file) => slot === null || !GENERATED.includes(file.path));
|
|
107
|
+
const written = files.map((file) => ({ path: file.path, text: templates.renderTemplate(file.text, vars) }));
|
|
108
|
+
if (slot) written.push(...slotFiles(slot, name, vars, values['scope-value']));
|
|
109
|
+
for (const file of written) {
|
|
110
|
+
const out = path.join(target, file.path);
|
|
111
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
112
|
+
fs.writeFileSync(out, file.text);
|
|
113
|
+
}
|
|
114
|
+
ctx.out(`${name}: created ${written.length} files in ${target}`);
|
|
115
|
+
ctx.out(`next: cd ${path.relative(ctx.cwd, target) || '.'} && npm install && ${CLI_NAME} test`);
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { createTestkitAdapter } from '#sdk/conformance';
|
|
5
|
+
import { readManifest } from '../lib/inspect.mjs';
|
|
6
|
+
|
|
7
|
+
const IGNORED = /(^|[\\/])(node_modules|dist|\.git)([\\/]|$)/;
|
|
8
|
+
|
|
9
|
+
// One plugin in the SDK testkit host. load() replaces the running copy; the loader gives every load a fresh module.
|
|
10
|
+
export function createSession(dir, ctx) {
|
|
11
|
+
const adapter = createTestkitAdapter();
|
|
12
|
+
/** @type {import("../../types/shims/conformance.d.ts").HostHandle | null} */
|
|
13
|
+
let host = null;
|
|
14
|
+
let name = path.basename(dir);
|
|
15
|
+
async function stop() {
|
|
16
|
+
if (!host) return;
|
|
17
|
+
const current = host;
|
|
18
|
+
host = null;
|
|
19
|
+
try {
|
|
20
|
+
await current.unload(name);
|
|
21
|
+
} catch {
|
|
22
|
+
// the host is closed next
|
|
23
|
+
}
|
|
24
|
+
await current.close();
|
|
25
|
+
}
|
|
26
|
+
async function load() {
|
|
27
|
+
await stop();
|
|
28
|
+
const read = readManifest(dir);
|
|
29
|
+
name = read.name;
|
|
30
|
+
for (const error of read.errors) ctx.err(`error: ${error}`);
|
|
31
|
+
if (!read.manifest) return false;
|
|
32
|
+
host = await adapter.create();
|
|
33
|
+
const result = await host.load(dir);
|
|
34
|
+
if (result.state !== 'active') {
|
|
35
|
+
for (const error of result.errors) ctx.err(`error: ${name}: ${error}`);
|
|
36
|
+
await stop();
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
ctx.out(`${name}: active (${read.manifest.capabilities.join(', ') || 'no capabilities'})`);
|
|
40
|
+
return read.errors.length === 0;
|
|
41
|
+
}
|
|
42
|
+
async function call(handler, input) {
|
|
43
|
+
if (!host) throw new Error(`${name} is not loaded`);
|
|
44
|
+
try {
|
|
45
|
+
ctx.out(JSON.stringify(await host.callHandler(name, handler, input)));
|
|
46
|
+
return true;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
const err = /** @type {{ code?: string, message?: string }} */ (error);
|
|
49
|
+
ctx.err(`error: ${name}: ${handler} failed with ${err?.code ?? 'error'}: ${err?.message ?? ''}`);
|
|
50
|
+
return false;
|
|
51
|
+
} finally {
|
|
52
|
+
const current = host;
|
|
53
|
+
if (current) {
|
|
54
|
+
for (const row of await current.audit()) {
|
|
55
|
+
if (row.outcome === 'denied') ctx.err(`denied: ${row.plugin}: ${row.action} ${JSON.stringify(row.details)}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return { load, call, stop };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function dev(argv, ctx) {
|
|
64
|
+
const { values, positionals } = parseArgs({
|
|
65
|
+
args: argv,
|
|
66
|
+
allowPositionals: true,
|
|
67
|
+
options: { once: { type: 'boolean' }, call: { type: 'string' }, input: { type: 'string' } },
|
|
68
|
+
});
|
|
69
|
+
const dir = path.resolve(ctx.cwd, positionals[0] ?? '.');
|
|
70
|
+
const session = createSession(dir, ctx);
|
|
71
|
+
let ok = await session.load();
|
|
72
|
+
if (ok && values.call !== undefined)
|
|
73
|
+
ok = await session.call(values.call, values.input === undefined ? {} : JSON.parse(values.input));
|
|
74
|
+
if (values.once || values.call !== undefined) {
|
|
75
|
+
await session.stop();
|
|
76
|
+
return ok ? 0 : 1;
|
|
77
|
+
}
|
|
78
|
+
const controller = new AbortController();
|
|
79
|
+
const signal = ctx.signal ?? controller.signal;
|
|
80
|
+
if (!ctx.signal) process.once('SIGINT', () => controller.abort());
|
|
81
|
+
/** @type {ReturnType<typeof setTimeout> | undefined} */
|
|
82
|
+
let timer;
|
|
83
|
+
let busy = Promise.resolve();
|
|
84
|
+
const watcher = fs.watch(dir, { recursive: true }, (_event, file) => {
|
|
85
|
+
if (file && IGNORED.test(String(file))) return;
|
|
86
|
+
clearTimeout(timer);
|
|
87
|
+
timer = setTimeout(() => {
|
|
88
|
+
busy = busy.then(async () => {
|
|
89
|
+
ctx.out(`${path.basename(dir)}: change detected, reloading`);
|
|
90
|
+
await session.load();
|
|
91
|
+
ctx.out('reload finished');
|
|
92
|
+
});
|
|
93
|
+
}, 150);
|
|
94
|
+
});
|
|
95
|
+
watcher.on('error', () => {});
|
|
96
|
+
ctx.out(`watching ${dir}; press Ctrl+C to stop`);
|
|
97
|
+
await new Promise((resolve) => {
|
|
98
|
+
if (signal.aborted) resolve(undefined);
|
|
99
|
+
else signal.addEventListener('abort', () => resolve(undefined), { once: true });
|
|
100
|
+
});
|
|
101
|
+
clearTimeout(timer);
|
|
102
|
+
watcher.close();
|
|
103
|
+
await busy;
|
|
104
|
+
await session.stop();
|
|
105
|
+
return 0;
|
|
106
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { sdkSatisfies } from '#contracts/src/manifest/sdk-range.mjs';
|
|
4
|
+
import { SLOTS } from '#contracts/src/manifest/validate.mjs';
|
|
5
|
+
import { SDK_VERSION } from '#sdk';
|
|
6
|
+
import own from '../../package.json' with { type: 'json' };
|
|
7
|
+
import { BUNDLER, BUNDLER_PIN, BUNDLER_VERSION } from '../lib/build.mjs';
|
|
8
|
+
import { readManifest } from '../lib/inspect.mjs';
|
|
9
|
+
import { SDK_PACKAGE } from '../lib/names.mjs';
|
|
10
|
+
import { loadTemplates } from '../lib/templates.mjs';
|
|
11
|
+
|
|
12
|
+
// The author side of the health check: this machine, the pinned tools and the plugin in the folder.
|
|
13
|
+
export async function doctor(argv, ctx) {
|
|
14
|
+
const dir = path.resolve(ctx.cwd, argv[0] ?? '.');
|
|
15
|
+
const types = (await loadTemplates()).listTemplates();
|
|
16
|
+
const rows = [
|
|
17
|
+
{
|
|
18
|
+
id: 'node',
|
|
19
|
+
ok: sdkSatisfies(own.engines.node, process.versions.node),
|
|
20
|
+
detail: `${process.version}, needs ${own.engines.node}`,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: 'type-stripping',
|
|
24
|
+
ok: Boolean(process.features.typescript),
|
|
25
|
+
detail: `process.features.typescript is ${JSON.stringify(process.features.typescript ?? null)}`,
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: 'bundler',
|
|
29
|
+
ok: BUNDLER_VERSION === BUNDLER_PIN,
|
|
30
|
+
detail: `${BUNDLER} ${BUNDLER_VERSION} installed, ${BUNDLER_PIN} pinned`,
|
|
31
|
+
},
|
|
32
|
+
{ id: 'sdk', ok: true, detail: `${SDK_PACKAGE} ${SDK_VERSION}` },
|
|
33
|
+
{ id: 'templates', ok: types.length >= 2, detail: types.join(', ') },
|
|
34
|
+
{
|
|
35
|
+
id: 'slots',
|
|
36
|
+
ok: SLOTS.slots.every((slot) => Array.isArray(slot.handlers)),
|
|
37
|
+
detail: `${SLOTS.slots.length} slots in the registry, each with a handler list`,
|
|
38
|
+
},
|
|
39
|
+
];
|
|
40
|
+
if (fs.existsSync(path.join(dir, 'plugin.json'))) {
|
|
41
|
+
const read = readManifest(dir);
|
|
42
|
+
rows.push({ id: 'plugin', ok: read.errors.length === 0, detail: read.errors[0] ?? `${read.name} reads cleanly` });
|
|
43
|
+
} else {
|
|
44
|
+
rows.push({ id: 'plugin', ok: true, detail: 'no plugin.json here, plugin checks skipped' });
|
|
45
|
+
}
|
|
46
|
+
for (const row of rows) ctx.out(`${row.ok ? 'ok ' : 'FAIL'} ${row.id}: ${row.detail}`);
|
|
47
|
+
return rows.every((row) => row.ok) ? 0 : 1;
|
|
48
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { parseArgs } from 'node:util';
|
|
3
|
+
import { writeBundle } from '../lib/bundle.mjs';
|
|
4
|
+
import { checkPlugin } from '../lib/check.mjs';
|
|
5
|
+
import { report } from '../lib/report.mjs';
|
|
6
|
+
import { UsageError } from '../lib/usage.mjs';
|
|
7
|
+
|
|
8
|
+
const TAG = /^[A-Za-z0-9._/-]{1,100}$/;
|
|
9
|
+
|
|
10
|
+
// Builds twice, refuses on any problem, then writes the bundle folder. Nothing is pushed or sent anywhere.
|
|
11
|
+
export async function publish(argv, ctx) {
|
|
12
|
+
const { values, positionals } = parseArgs({
|
|
13
|
+
args: argv,
|
|
14
|
+
allowPositionals: true,
|
|
15
|
+
options: { out: { type: 'string' }, 'source-tag': { type: 'string' } },
|
|
16
|
+
});
|
|
17
|
+
if (values['source-tag'] !== undefined && !TAG.test(values['source-tag']))
|
|
18
|
+
throw new UsageError(`"${values['source-tag']}" is not a valid source tag`);
|
|
19
|
+
const dir = path.resolve(ctx.cwd, positionals[0] ?? '.');
|
|
20
|
+
const result = await checkPlugin(dir, { builds: 2 });
|
|
21
|
+
report(ctx, result);
|
|
22
|
+
if (result.errors.length > 0 || !result.bundle) {
|
|
23
|
+
ctx.err(`${result.name}: publish stopped, nothing was written`);
|
|
24
|
+
return 1;
|
|
25
|
+
}
|
|
26
|
+
const written = writeBundle({
|
|
27
|
+
outRoot: path.resolve(dir, values.out ?? 'dist'),
|
|
28
|
+
raw: result.raw,
|
|
29
|
+
manifest: result.manifest,
|
|
30
|
+
bundle: result.bundle,
|
|
31
|
+
sourceTag: values['source-tag'] ?? null,
|
|
32
|
+
sourceDir: dir,
|
|
33
|
+
});
|
|
34
|
+
const { entry } = written;
|
|
35
|
+
ctx.out(`${result.name}: wrote ${written.dir}`);
|
|
36
|
+
ctx.out(
|
|
37
|
+
`${result.name}: bundle sha256 ${entry.bundle.sha256} (${entry.bundle.bytes} bytes), manifest sha256 ${entry.manifest.sha256}`,
|
|
38
|
+
);
|
|
39
|
+
ctx.out(
|
|
40
|
+
`${result.name}: two clean builds matched byte for byte (${entry.toolchain.bundler} ${entry.toolchain.version})`,
|
|
41
|
+
);
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { parseArgs } from 'node:util';
|
|
6
|
+
import { runSlotSuite } from '#sdk/slot-suites';
|
|
7
|
+
import { buildBundle } from '../lib/build.mjs';
|
|
8
|
+
import { BUNDLE_FILES, manifestText } from '../lib/bundle.mjs';
|
|
9
|
+
import { readManifest } from '../lib/inspect.mjs';
|
|
10
|
+
|
|
11
|
+
// The author's own tests run in a child process with the plain Node test runner: no mocks, no extra framework.
|
|
12
|
+
// NODE_TEST_CONTEXT is removed so a run started from inside another test run reports on its own.
|
|
13
|
+
function runUnitTests(dir, ctx) {
|
|
14
|
+
return new Promise((resolve) => {
|
|
15
|
+
const env = { ...process.env };
|
|
16
|
+
delete env.NODE_TEST_CONTEXT;
|
|
17
|
+
const child = spawn(process.execPath, ['--test', 'test/**/*.test.*'], {
|
|
18
|
+
cwd: dir,
|
|
19
|
+
env,
|
|
20
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
21
|
+
});
|
|
22
|
+
let stdout = '';
|
|
23
|
+
let stderr = '';
|
|
24
|
+
child.stdout.on('data', (chunk) => {
|
|
25
|
+
stdout += chunk;
|
|
26
|
+
});
|
|
27
|
+
child.stderr.on('data', (chunk) => {
|
|
28
|
+
stderr += chunk;
|
|
29
|
+
});
|
|
30
|
+
child.on('close', (code) => {
|
|
31
|
+
if (stdout) ctx.out(stdout.trimEnd());
|
|
32
|
+
if (stderr) ctx.err(stderr.trimEnd());
|
|
33
|
+
resolve(code ?? 1);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function test(argv, ctx) {
|
|
39
|
+
const { positionals } = parseArgs({ args: argv, allowPositionals: true, options: {} });
|
|
40
|
+
const dir = path.resolve(ctx.cwd, positionals[0] ?? '.');
|
|
41
|
+
const read = readManifest(dir);
|
|
42
|
+
for (const error of read.errors) ctx.err(`error: ${error}`);
|
|
43
|
+
if (!read.manifest) return 1;
|
|
44
|
+
let code = read.errors.length > 0 ? 1 : 0;
|
|
45
|
+
|
|
46
|
+
if (fs.existsSync(path.join(dir, 'test'))) {
|
|
47
|
+
if ((await runUnitTests(dir, ctx)) !== 0) {
|
|
48
|
+
ctx.err(`error: ${read.name}: the unit tests failed`);
|
|
49
|
+
code = 1;
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
ctx.out(`${read.name}: no test folder, unit tests not run`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const claims = read.manifest.slots;
|
|
56
|
+
if (claims.length === 0) {
|
|
57
|
+
ctx.out(`${read.name}: no slot claimed, slot suite not run`);
|
|
58
|
+
return code;
|
|
59
|
+
}
|
|
60
|
+
if (read.manifest.entry === undefined) return 1;
|
|
61
|
+
// The suite runs against the built bundle in the shipped layout, so what passes is what ships.
|
|
62
|
+
const work = fs.mkdtempSync(path.join(os.tmpdir(), 'suite-'));
|
|
63
|
+
try {
|
|
64
|
+
fs.writeFileSync(path.join(work, BUNDLE_FILES.bundle), await buildBundle(dir, read.manifest.entry));
|
|
65
|
+
fs.writeFileSync(path.join(work, BUNDLE_FILES.manifest), manifestText(read.raw));
|
|
66
|
+
for (const claim of claims) {
|
|
67
|
+
const result = await runSlotSuite(claim.slot, work);
|
|
68
|
+
const passed = result.results.filter((item) => item.status === 'pass').length;
|
|
69
|
+
for (const item of result.results.filter((entry) => entry.status !== 'pass')) {
|
|
70
|
+
ctx.err(`error: ${read.name}: slot ${claim.slot}: ${item.part}/${item.id} ${item.status}: ${item.message}`);
|
|
71
|
+
}
|
|
72
|
+
ctx.out(`${read.name}: slot ${claim.slot}: ${passed} of ${result.results.length} checks passed`);
|
|
73
|
+
if (!result.ok) code = 1;
|
|
74
|
+
}
|
|
75
|
+
} catch (error) {
|
|
76
|
+
const err = /** @type {{ message?: string }} */ (error);
|
|
77
|
+
ctx.err(`error: ${read.name}: build failed: ${String(err?.message ?? error).split('\n')[0]}`);
|
|
78
|
+
code = 1;
|
|
79
|
+
} finally {
|
|
80
|
+
fs.rmSync(work, { recursive: true, force: true });
|
|
81
|
+
}
|
|
82
|
+
return code;
|
|
83
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { BUNDLE_FILES, verifyBundle } from '../lib/bundle.mjs';
|
|
5
|
+
import { checkPlugin, scanBundle } from '../lib/check.mjs';
|
|
6
|
+
import { readManifest } from '../lib/inspect.mjs';
|
|
7
|
+
import { report } from '../lib/report.mjs';
|
|
8
|
+
|
|
9
|
+
// A published bundle folder: integrity, manifest, SDK range and scan of the shipped file.
|
|
10
|
+
function checkBundleFolder(dir) {
|
|
11
|
+
const verified = verifyBundle(dir);
|
|
12
|
+
const read = readManifest(dir);
|
|
13
|
+
const errors = [...new Set([...verified.problems, ...read.errors])];
|
|
14
|
+
const warnings = [];
|
|
15
|
+
const file = path.join(dir, BUNDLE_FILES.bundle);
|
|
16
|
+
if (read.manifest && fs.existsSync(file)) {
|
|
17
|
+
const scanned = scanBundle({ name: read.name, manifest: read.manifest, bundleText: fs.readFileSync(file, 'utf8') });
|
|
18
|
+
errors.push(...scanned.errors);
|
|
19
|
+
warnings.push(...scanned.warnings);
|
|
20
|
+
}
|
|
21
|
+
return { name: read.name, errors, warnings };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function validate(argv, ctx) {
|
|
25
|
+
const { positionals } = parseArgs({ args: argv, allowPositionals: true, options: {} });
|
|
26
|
+
const dir = path.resolve(ctx.cwd, positionals[0] ?? '.');
|
|
27
|
+
const result = fs.existsSync(path.join(dir, BUNDLE_FILES.entry)) ? checkBundleFolder(dir) : await checkPlugin(dir);
|
|
28
|
+
report(ctx, result);
|
|
29
|
+
if (result.errors.length > 0) {
|
|
30
|
+
ctx.err(`${result.name}: ${result.errors.length} problem(s), not valid`);
|
|
31
|
+
return 1;
|
|
32
|
+
}
|
|
33
|
+
ctx.out(`${result.name}: valid (${result.warnings.length} warning(s))`);
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { rolldown } from 'rolldown';
|
|
5
|
+
import own from '../../package.json' with { type: 'json' };
|
|
6
|
+
import { SDK_PACKAGE } from './names.mjs';
|
|
7
|
+
import { packageInfo } from './package-info.mjs';
|
|
8
|
+
|
|
9
|
+
export const BUNDLER = 'rolldown';
|
|
10
|
+
export const BUNDLER_PIN = own.dependencies.rolldown;
|
|
11
|
+
export const BUNDLER_VERSION = packageInfo(BUNDLER).version;
|
|
12
|
+
|
|
13
|
+
// One clean build to a temporary folder; returns the bytes of the single file. The second build of a publish uses
|
|
14
|
+
// a different, longer folder name so the folder cannot leak into the output unnoticed. Settings are fixed here
|
|
15
|
+
// (single-file ES module, minified, no source map, node platform); a plugin has no bundler setting of its own.
|
|
16
|
+
export async function buildBundle(dir, entry, index = 0) {
|
|
17
|
+
const out = fs.mkdtempSync(
|
|
18
|
+
path.join(os.tmpdir(), index === 0 ? 'bundle-' : 'bundle-second-build-in-a-longer-folder-name-'),
|
|
19
|
+
);
|
|
20
|
+
const file = path.join(out, 'index.mjs');
|
|
21
|
+
let bundle;
|
|
22
|
+
try {
|
|
23
|
+
bundle = await rolldown({
|
|
24
|
+
input: path.resolve(dir, entry),
|
|
25
|
+
platform: 'node',
|
|
26
|
+
external: [SDK_PACKAGE],
|
|
27
|
+
logLevel: 'silent',
|
|
28
|
+
});
|
|
29
|
+
await bundle.write({ file, format: 'esm', minify: true, sourcemap: false });
|
|
30
|
+
return fs.readFileSync(file);
|
|
31
|
+
} finally {
|
|
32
|
+
await bundle?.close();
|
|
33
|
+
fs.rmSync(out, { recursive: true, force: true });
|
|
34
|
+
}
|
|
35
|
+
}
|