obsidian-plugin-dev-kit 0.0.0-stage → 0.3.3
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/LICENSE +21 -0
- package/README.md +223 -2
- package/bin/opdk.js +34 -0
- package/dist/build/args.d.ts +10 -0
- package/dist/build/args.js +45 -0
- package/dist/build/bundle-check.d.ts +17 -0
- package/dist/build/bundle-check.js +42 -0
- package/dist/build/bundle-file.d.ts +4 -0
- package/dist/build/bundle-file.js +29 -0
- package/dist/build/commands.d.ts +5 -0
- package/dist/build/commands.js +109 -0
- package/dist/build/config.d.ts +3 -0
- package/dist/build/config.js +103 -0
- package/dist/build/css-imports.d.ts +23 -0
- package/dist/build/css-imports.js +50 -0
- package/dist/build/css-step.d.ts +10 -0
- package/dist/build/css-step.js +27 -0
- package/dist/build/css.d.ts +24 -0
- package/dist/build/css.js +47 -0
- package/dist/build/options.d.ts +55 -0
- package/dist/build/options.js +70 -0
- package/dist/build/release.d.ts +5 -0
- package/dist/build/release.js +34 -0
- package/dist/build/report.d.ts +7 -0
- package/dist/build/report.js +16 -0
- package/dist/build/vault.d.ts +26 -0
- package/dist/build/vault.js +290 -0
- package/dist/build-info.json +1 -0
- package/dist/cli/argv.d.ts +14 -0
- package/dist/cli/argv.js +18 -0
- package/dist/cli/commands.d.ts +3 -0
- package/dist/cli/commands.js +7 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +23 -0
- package/dist/cli/process-reporter.d.ts +5 -0
- package/dist/cli/process-reporter.js +15 -0
- package/dist/cli/registry.d.ts +4 -0
- package/dist/cli/registry.js +15 -0
- package/dist/cli/reporter.d.ts +53 -0
- package/dist/cli/reporter.js +100 -0
- package/dist/cli/run.d.ts +9 -0
- package/dist/cli/run.js +61 -0
- package/dist/cli/types.d.ts +27 -0
- package/dist/cli/types.js +1 -0
- package/dist/cli/version.d.ts +13 -0
- package/dist/cli/version.js +61 -0
- package/dist/config/define-config.d.ts +3 -0
- package/dist/config/define-config.js +4 -0
- package/dist/config/load-config.d.ts +6 -0
- package/dist/config/load-config.js +70 -0
- package/dist/config/manifest.d.ts +3 -0
- package/dist/config/manifest.js +72 -0
- package/dist/config/types.d.ts +146 -0
- package/dist/config/types.js +23 -0
- package/dist/demo/args.d.ts +4 -0
- package/dist/demo/args.js +13 -0
- package/dist/demo/checks.d.ts +56 -0
- package/dist/demo/checks.js +124 -0
- package/dist/demo/commands.d.ts +3 -0
- package/dist/demo/commands.js +65 -0
- package/dist/demo/config.d.ts +57 -0
- package/dist/demo/config.js +337 -0
- package/dist/demo/ffmpeg-run.d.ts +18 -0
- package/dist/demo/ffmpeg-run.js +59 -0
- package/dist/demo/ffmpeg.d.ts +63 -0
- package/dist/demo/ffmpeg.js +111 -0
- package/dist/demo/films.d.ts +46 -0
- package/dist/demo/films.js +134 -0
- package/dist/demo/phone-layout.d.ts +38 -0
- package/dist/demo/phone-layout.js +98 -0
- package/dist/demo/phone-render.d.ts +28 -0
- package/dist/demo/phone-render.js +124 -0
- package/dist/demo/published.d.ts +16 -0
- package/dist/demo/published.js +72 -0
- package/dist/demo/run.d.ts +31 -0
- package/dist/demo/run.js +397 -0
- package/dist/demo/stills.d.ts +21 -0
- package/dist/demo/stills.js +62 -0
- package/dist/e2e/artifacts.d.ts +27 -0
- package/dist/e2e/artifacts.js +90 -0
- package/dist/e2e/cleanup.d.ts +54 -0
- package/dist/e2e/cleanup.js +109 -0
- package/dist/e2e/config.d.ts +9 -0
- package/dist/e2e/config.js +42 -0
- package/dist/e2e/create-test.d.ts +37 -0
- package/dist/e2e/create-test.js +109 -0
- package/dist/e2e/demo/clip.d.ts +21 -0
- package/dist/e2e/demo/clip.js +44 -0
- package/dist/e2e/demo/create-demo.d.ts +42 -0
- package/dist/e2e/demo/create-demo.js +439 -0
- package/dist/e2e/demo/manifest.d.ts +76 -0
- package/dist/e2e/demo/manifest.js +168 -0
- package/dist/e2e/demo/names.d.ts +18 -0
- package/dist/e2e/demo/names.js +44 -0
- package/dist/e2e/demo/options.d.ts +22 -0
- package/dist/e2e/demo/options.js +59 -0
- package/dist/e2e/demo/ownership.d.ts +27 -0
- package/dist/e2e/demo/ownership.js +65 -0
- package/dist/e2e/demo/page-scripts.d.ts +82 -0
- package/dist/e2e/demo/page-scripts.js +250 -0
- package/dist/e2e/demo/png.d.ts +8 -0
- package/dist/e2e/demo/png.js +23 -0
- package/dist/e2e/demo/presets.d.ts +43 -0
- package/dist/e2e/demo/presets.js +20 -0
- package/dist/e2e/demo/privacy.d.ts +18 -0
- package/dist/e2e/demo/privacy.js +30 -0
- package/dist/e2e/demo/still-options.d.ts +22 -0
- package/dist/e2e/demo/still-options.js +68 -0
- package/dist/e2e/demo/types.d.ts +184 -0
- package/dist/e2e/demo/types.js +1 -0
- package/dist/e2e/electron-binary.d.ts +15 -0
- package/dist/e2e/electron-binary.js +30 -0
- package/dist/e2e/fixtures.d.ts +62 -0
- package/dist/e2e/fixtures.js +68 -0
- package/dist/e2e/gate.d.ts +4 -0
- package/dist/e2e/gate.js +8 -0
- package/dist/e2e/global-setup.d.ts +11 -0
- package/dist/e2e/global-setup.js +17 -0
- package/dist/e2e/helpers/commands.d.ts +14 -0
- package/dist/e2e/helpers/commands.js +56 -0
- package/dist/e2e/helpers/create-helpers.d.ts +24 -0
- package/dist/e2e/helpers/create-helpers.js +582 -0
- package/dist/e2e/helpers/device-metrics.d.ts +21 -0
- package/dist/e2e/helpers/device-metrics.js +25 -0
- package/dist/e2e/helpers/ime.d.ts +13 -0
- package/dist/e2e/helpers/ime.js +19 -0
- package/dist/e2e/helpers/layout-page-scripts.d.ts +103 -0
- package/dist/e2e/helpers/layout-page-scripts.js +224 -0
- package/dist/e2e/helpers/layout.d.ts +83 -0
- package/dist/e2e/helpers/layout.js +148 -0
- package/dist/e2e/helpers/mobile.d.ts +31 -0
- package/dist/e2e/helpers/mobile.js +26 -0
- package/dist/e2e/helpers/page-scripts.d.ts +194 -0
- package/dist/e2e/helpers/page-scripts.js +467 -0
- package/dist/e2e/helpers/settings.d.ts +8 -0
- package/dist/e2e/helpers/settings.js +16 -0
- package/dist/e2e/helpers/settle.d.ts +28 -0
- package/dist/e2e/helpers/settle.js +33 -0
- package/dist/e2e/helpers/types.d.ts +193 -0
- package/dist/e2e/helpers/types.js +1 -0
- package/dist/e2e/helpers/view-mode.d.ts +23 -0
- package/dist/e2e/helpers/view-mode.js +35 -0
- package/dist/e2e/index.d.ts +14 -0
- package/dist/e2e/index.js +4 -0
- package/dist/e2e/internals.d.ts +155 -0
- package/dist/e2e/internals.js +8 -0
- package/dist/e2e/obsidian-path.d.ts +17 -0
- package/dist/e2e/obsidian-path.js +31 -0
- package/dist/e2e/options.d.ts +123 -0
- package/dist/e2e/options.js +244 -0
- package/dist/e2e/page-scripts.d.ts +45 -0
- package/dist/e2e/page-scripts.js +53 -0
- package/dist/e2e/peers.d.ts +9 -0
- package/dist/e2e/peers.js +19 -0
- package/dist/e2e/plugins.d.ts +32 -0
- package/dist/e2e/plugins.js +119 -0
- package/dist/e2e/process-tree.d.ts +33 -0
- package/dist/e2e/process-tree.js +133 -0
- package/dist/e2e/readiness.d.ts +23 -0
- package/dist/e2e/readiness.js +63 -0
- package/dist/e2e/seed.d.ts +34 -0
- package/dist/e2e/seed.js +122 -0
- package/dist/e2e/session.d.ts +71 -0
- package/dist/e2e/session.js +266 -0
- package/dist/e2e/vault.d.ts +52 -0
- package/dist/e2e/vault.js +146 -0
- package/dist/e2e/version.d.ts +25 -0
- package/dist/e2e/version.js +40 -0
- package/dist/eslint/index.d.ts +2 -0
- package/dist/eslint/index.js +1 -0
- package/dist/eslint/obsidian-plugin.d.ts +49 -0
- package/dist/eslint/obsidian-plugin.js +115 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/dist/kit/args.d.ts +7 -0
- package/dist/kit/args.js +21 -0
- package/dist/kit/commands.d.ts +3 -0
- package/dist/kit/commands.js +52 -0
- package/dist/kit/files.d.ts +3 -0
- package/dist/kit/files.js +24 -0
- package/dist/kit/pins.d.ts +89 -0
- package/dist/kit/pins.js +178 -0
- package/dist/kit/remote.d.ts +13 -0
- package/dist/kit/remote.js +33 -0
- package/dist/kit/runners.d.ts +11 -0
- package/dist/kit/runners.js +53 -0
- package/dist/kit/terminal-text.d.ts +6 -0
- package/dist/kit/terminal-text.js +20 -0
- package/dist/kit/upgrade.d.ts +26 -0
- package/dist/kit/upgrade.js +162 -0
- package/dist/peers/resolve.d.ts +8 -0
- package/dist/peers/resolve.js +28 -0
- package/dist/peers/versions.d.ts +30 -0
- package/dist/peers/versions.js +32 -0
- package/dist/release/api-check.d.ts +27 -0
- package/dist/release/api-check.js +119 -0
- package/dist/release/args.d.ts +40 -0
- package/dist/release/args.js +120 -0
- package/dist/release/assets.d.ts +21 -0
- package/dist/release/assets.js +35 -0
- package/dist/release/build-check.d.ts +43 -0
- package/dist/release/build-check.js +184 -0
- package/dist/release/build-script.d.ts +12 -0
- package/dist/release/build-script.js +30 -0
- package/dist/release/build-tagged.d.ts +26 -0
- package/dist/release/build-tagged.js +52 -0
- package/dist/release/bump.d.ts +24 -0
- package/dist/release/bump.js +58 -0
- package/dist/release/changelog.d.ts +25 -0
- package/dist/release/changelog.js +148 -0
- package/dist/release/check.d.ts +38 -0
- package/dist/release/check.js +150 -0
- package/dist/release/commands.d.ts +3 -0
- package/dist/release/commands.js +154 -0
- package/dist/release/committed-tree.d.ts +63 -0
- package/dist/release/committed-tree.js +118 -0
- package/dist/release/config.d.ts +3 -0
- package/dist/release/config.js +28 -0
- package/dist/release/gh-access.d.ts +14 -0
- package/dist/release/gh-access.js +45 -0
- package/dist/release/gh.d.ts +10 -0
- package/dist/release/gh.js +33 -0
- package/dist/release/git.d.ts +49 -0
- package/dist/release/git.js +111 -0
- package/dist/release/interrupt.d.ts +30 -0
- package/dist/release/interrupt.js +60 -0
- package/dist/release/json.d.ts +33 -0
- package/dist/release/json.js +81 -0
- package/dist/release/kit-check.d.ts +8 -0
- package/dist/release/kit-check.js +51 -0
- package/dist/release/manifest-rules.d.ts +23 -0
- package/dist/release/manifest-rules.js +255 -0
- package/dist/release/preflight.d.ts +10 -0
- package/dist/release/preflight.js +65 -0
- package/dist/release/problems.d.ts +24 -0
- package/dist/release/problems.js +11 -0
- package/dist/release/project-files.d.ts +14 -0
- package/dist/release/project-files.js +16 -0
- package/dist/release/publish-command.d.ts +21 -0
- package/dist/release/publish-command.js +83 -0
- package/dist/release/publish-git.d.ts +37 -0
- package/dist/release/publish-git.js +108 -0
- package/dist/release/publish-state.d.ts +29 -0
- package/dist/release/publish-state.js +70 -0
- package/dist/release/publish-verify.d.ts +41 -0
- package/dist/release/publish-verify.js +112 -0
- package/dist/release/publish-wait.d.ts +25 -0
- package/dist/release/publish-wait.js +56 -0
- package/dist/release/publish.d.ts +35 -0
- package/dist/release/publish.js +181 -0
- package/dist/release/release-assets.d.ts +39 -0
- package/dist/release/release-assets.js +68 -0
- package/dist/release/release.d.ts +37 -0
- package/dist/release/release.js +179 -0
- package/dist/release/remote.d.ts +12 -0
- package/dist/release/remote.js +15 -0
- package/dist/release/reproducible.d.ts +16 -0
- package/dist/release/reproducible.js +34 -0
- package/dist/release/run-tests.d.ts +5 -0
- package/dist/release/run-tests.js +17 -0
- package/dist/release/semver.d.ts +8 -0
- package/dist/release/semver.js +60 -0
- package/dist/release/version-rules.d.ts +28 -0
- package/dist/release/version-rules.js +63 -0
- package/dist/release/versions-json.d.ts +15 -0
- package/dist/release/versions-json.js +88 -0
- package/dist/release/workflow-run.d.ts +44 -0
- package/dist/release/workflow-run.js +71 -0
- package/dist/scanner/code-checks.d.ts +41 -0
- package/dist/scanner/code-checks.js +82 -0
- package/dist/scanner/commands.d.ts +3 -0
- package/dist/scanner/commands.js +43 -0
- package/dist/scanner/config.d.ts +7 -0
- package/dist/scanner/config.js +25 -0
- package/dist/scanner/css-check.d.ts +26 -0
- package/dist/scanner/css-check.js +63 -0
- package/dist/scanner/eslint-nesting.d.ts +16 -0
- package/dist/scanner/eslint-nesting.js +45 -0
- package/dist/scanner/findings.d.ts +23 -0
- package/dist/scanner/findings.js +21 -0
- package/dist/scanner/git-files.d.ts +16 -0
- package/dist/scanner/git-files.js +73 -0
- package/dist/scanner/lint.d.ts +47 -0
- package/dist/scanner/lint.js +163 -0
- package/dist/scanner/meta-checks.d.ts +8 -0
- package/dist/scanner/meta-checks.js +22 -0
- package/dist/scanner/outside-project.d.ts +19 -0
- package/dist/scanner/outside-project.js +20 -0
- package/dist/scanner/run.d.ts +19 -0
- package/dist/scanner/run.js +78 -0
- package/dist/scanner/scanner-config.d.ts +114 -0
- package/dist/scanner/scanner-config.js +207 -0
- package/dist/scanner/typescript-gate.d.ts +30 -0
- package/dist/scanner/typescript-gate.js +73 -0
- package/dist/vitest/build-config.d.ts +17 -0
- package/dist/vitest/build-config.js +106 -0
- package/dist/vitest/define-unit-config.d.ts +27 -0
- package/dist/vitest/define-unit-config.js +45 -0
- package/dist/vitest/dom/elements.d.ts +87 -0
- package/dist/vitest/dom/elements.js +291 -0
- package/dist/vitest/dom/index.d.ts +1 -0
- package/dist/vitest/dom/index.js +1 -0
- package/dist/vitest/dom/install.d.ts +8 -0
- package/dist/vitest/dom/install.js +261 -0
- package/dist/vitest/dom/setup.d.ts +1 -0
- package/dist/vitest/dom/setup.js +3 -0
- package/dist/vitest/index.d.ts +3 -0
- package/dist/vitest/index.js +2 -0
- package/dist/vitest/options.d.ts +92 -0
- package/dist/vitest/options.js +136 -0
- package/dist/vitest/peers.d.ts +10 -0
- package/dist/vitest/peers.js +51 -0
- package/dist/vitest/workspace-root.d.ts +7 -0
- package/dist/vitest/workspace-root.js +39 -0
- package/docs/build.md +232 -0
- package/docs/ci.md +182 -0
- package/docs/demo.md +364 -0
- package/docs/e2e.md +563 -0
- package/docs/release.md +374 -0
- package/docs/scanner.md +204 -0
- package/docs/unit.md +291 -0
- package/package.json +100 -4
- package/plugins/opdk/skills/audit/SKILL.md +81 -0
- package/plugins/opdk/skills/build/SKILL.md +79 -0
- package/plugins/opdk/skills/demo/SKILL.md +102 -0
- package/plugins/opdk/skills/e2e/SKILL.md +132 -0
- package/plugins/opdk/skills/release/SKILL.md +84 -0
- package/plugins/opdk/skills/scanner/SKILL.md +60 -0
- package/plugins/opdk/skills/unit/SKILL.md +90 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, unwatchFile, watchFile } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { context } from "esbuild";
|
|
4
|
+
import { fileStamp } from "./css.js";
|
|
5
|
+
import { CssImportError } from "./css-imports.js";
|
|
6
|
+
import { runCssStep } from "./css-step.js";
|
|
7
|
+
import { toEsbuildOptions } from "./options.js";
|
|
8
|
+
import { isBuildFailure, printMessages } from "./report.js";
|
|
9
|
+
const POLL_INTERVAL_MS = 300;
|
|
10
|
+
/** `opdk build --vault`: unminified bundle plus styles.css and manifest.json in the dev vault. Returns the exit code. */
|
|
11
|
+
export async function runVaultBuild(options) {
|
|
12
|
+
const { cwd, manifest, build, watch: watching } = options;
|
|
13
|
+
const id = manifest.id;
|
|
14
|
+
const vaultDir = path.resolve(cwd, options.dir);
|
|
15
|
+
// The stylesheet the vault gets: the bundled one, or the hand-written styles.css of a plugin without src/styles.
|
|
16
|
+
const stylesSource = path.resolve(cwd, build.css === false ? "styles.css" : build.css.outfile);
|
|
17
|
+
const state = { assetsFailed: false, hookFailed: false, watching: false };
|
|
18
|
+
// The CSS files the last successful CSS step read; polled in watch mode so a new or removed import is tracked.
|
|
19
|
+
const cssEntry = build.css === false ? undefined : path.resolve(cwd, build.css.entry);
|
|
20
|
+
let cssInputs = [];
|
|
21
|
+
// The import target the last failed CSS step could not find. Polled too, so creating it triggers a sync.
|
|
22
|
+
let cssMissing;
|
|
23
|
+
// A plugin with a hand-written stylesheet has no CSS entry on every sync; say so on the first one only.
|
|
24
|
+
let noEntryAnnounced = false;
|
|
25
|
+
const polled = new Set();
|
|
26
|
+
// What each CSS file looked like when the last CSS step read it (undefined: it was missing). Lets a poller that
|
|
27
|
+
// registers later notice an edit made in between, see `recheckBaseline`.
|
|
28
|
+
const stamps = new Map();
|
|
29
|
+
const recheckTimers = new Set();
|
|
30
|
+
const rejection = checkVaultDirArgument(options.dir, vaultDir, cwd);
|
|
31
|
+
if (rejection !== undefined) {
|
|
32
|
+
options.reporter.failure("build", `${id}: ${rejection}`);
|
|
33
|
+
return 1;
|
|
34
|
+
}
|
|
35
|
+
if (options.force !== true) {
|
|
36
|
+
const message = checkVaultTarget(vaultDir, id);
|
|
37
|
+
if (message !== undefined) {
|
|
38
|
+
options.reporter.failure("build", `${id}: ${message}`);
|
|
39
|
+
return 1;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (watching && options.signal === undefined) {
|
|
43
|
+
throw new Error("runVaultBuild: watch mode needs an AbortSignal to know when to stop.");
|
|
44
|
+
}
|
|
45
|
+
const syncAssets = () => {
|
|
46
|
+
state.assetsFailed = false;
|
|
47
|
+
const reads = new Map();
|
|
48
|
+
try {
|
|
49
|
+
// Rebuild the stylesheet rather than copying whatever is lying around.
|
|
50
|
+
// Copying it stale is silent and shipped a vault missing the styles of two
|
|
51
|
+
// whole blocks: the TypeScript was fresh, the CSS was three commits old.
|
|
52
|
+
if (build.css !== false) {
|
|
53
|
+
const result = runCssStep(cwd, build.css, options, !noEntryAnnounced, (file, stamp) => {
|
|
54
|
+
// The first lookup of a file is the oldest stamp, the safest one to compare against.
|
|
55
|
+
if (!reads.has(file)) {
|
|
56
|
+
reads.set(file, stamp);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
if (result.status === "built") {
|
|
60
|
+
cssInputs = result.inputs;
|
|
61
|
+
cssMissing = undefined;
|
|
62
|
+
noEntryAnnounced = false;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
noEntryAnnounced = true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
mkdirSync(vaultDir, { recursive: true });
|
|
69
|
+
if (existsSync(stylesSource)) {
|
|
70
|
+
copyFileSync(stylesSource, path.join(vaultDir, "styles.css"));
|
|
71
|
+
}
|
|
72
|
+
copyFileSync(path.join(cwd, "manifest.json"), path.join(vaultDir, "manifest.json"));
|
|
73
|
+
options.reporter.success("build", `${id}: assets copied to ${vaultDir}`);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
state.assetsFailed = true;
|
|
77
|
+
if (error instanceof CssImportError) {
|
|
78
|
+
// Poll what the step read before it failed, or fixing a broken nested file would never trigger a sync.
|
|
79
|
+
// Keep the previous inputs too: a file that was polled stays polled while a step fails.
|
|
80
|
+
cssInputs = [...new Set([...cssInputs, ...error.files])];
|
|
81
|
+
cssMissing = error.missing;
|
|
82
|
+
}
|
|
83
|
+
options.reporter.failure("build", `${id}: CSS build or copy failed: ${errorMessage(error)}`);
|
|
84
|
+
}
|
|
85
|
+
for (const [file, stamp] of reads) {
|
|
86
|
+
stamps.set(file, stamp);
|
|
87
|
+
}
|
|
88
|
+
if (state.watching) {
|
|
89
|
+
pollCssFiles();
|
|
90
|
+
}
|
|
91
|
+
options.onAssetsSynced?.();
|
|
92
|
+
};
|
|
93
|
+
// fs.watchFile on a missing path calls the listener once, immediately, with zeroed stats for both curr and prev.
|
|
94
|
+
// That is not a change: syncing on it would run the CSS step again and print the same import error twice.
|
|
95
|
+
const onCssPoll = (curr, prev) => {
|
|
96
|
+
if (curr.mtimeMs === 0 && prev.mtimeMs === 0) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
syncAssets();
|
|
100
|
+
};
|
|
101
|
+
// fs.watchFile takes its baseline stat asynchronously on libuv's thread pool. An edit that lands between the CSS
|
|
102
|
+
// step reading a file and that stat (an import seen for the first time, a missing target that appears) becomes
|
|
103
|
+
// the baseline and is never reported, so it would be lost until the next edit. One poll interval after
|
|
104
|
+
// registering, the baseline is settled: compare the file with what the CSS step read, and sync if it differs.
|
|
105
|
+
const recheckBaseline = (file) => {
|
|
106
|
+
if (!stamps.has(file)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const timer = setTimeout(() => {
|
|
110
|
+
recheckTimers.delete(timer);
|
|
111
|
+
if (!state.watching || !polled.has(file)) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
if (fileStamp(file) !== stamps.get(file)) {
|
|
116
|
+
syncAssets();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
options.reporter.failure("build", `${id}: could not check ${file} for changes: ${errorMessage(error)}`);
|
|
121
|
+
}
|
|
122
|
+
}, POLL_INTERVAL_MS);
|
|
123
|
+
recheckTimers.add(timer);
|
|
124
|
+
};
|
|
125
|
+
// Poll the exact files the CSS step reads. A recursive fs.watch on the folder delivered nothing for 4-6 s
|
|
126
|
+
// on macOS (FSEvents latency), and an outfile inside that folder would have retriggered it forever.
|
|
127
|
+
// The entry stays polled even when the last CSS step failed, so fixing it triggers the next sync.
|
|
128
|
+
const pollCssFiles = () => {
|
|
129
|
+
const wanted = new Set(cssInputs);
|
|
130
|
+
if (cssMissing !== undefined) {
|
|
131
|
+
wanted.add(cssMissing);
|
|
132
|
+
}
|
|
133
|
+
if (cssEntry !== undefined && existsSync(cssEntry)) {
|
|
134
|
+
wanted.add(cssEntry);
|
|
135
|
+
}
|
|
136
|
+
for (const file of wanted) {
|
|
137
|
+
if (!polled.has(file)) {
|
|
138
|
+
polled.add(file);
|
|
139
|
+
watchFile(file, { interval: POLL_INTERVAL_MS }, onCssPoll);
|
|
140
|
+
recheckBaseline(file);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
for (const file of polled) {
|
|
144
|
+
if (!wanted.has(file)) {
|
|
145
|
+
polled.delete(file);
|
|
146
|
+
unwatchFile(file, onCssPoll);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
// ctx.watch() resolves before its first build reaches onEnd; the Watching line waits for that build.
|
|
151
|
+
let firstBuildDone = () => undefined;
|
|
152
|
+
const firstBuild = new Promise((resolve) => {
|
|
153
|
+
firstBuildDone = resolve;
|
|
154
|
+
});
|
|
155
|
+
const ctx = await context({
|
|
156
|
+
...toEsbuildOptions(build, cwd, { kind: "vault", dir: vaultDir }),
|
|
157
|
+
plugins: [
|
|
158
|
+
{
|
|
159
|
+
name: "opdk-vault",
|
|
160
|
+
setup(esbuild) {
|
|
161
|
+
esbuild.onEnd(async (result) => {
|
|
162
|
+
try {
|
|
163
|
+
await printMessages(result.warnings, "warning", options);
|
|
164
|
+
await printMessages(result.errors, "error", options);
|
|
165
|
+
if (result.errors.length === 0) {
|
|
166
|
+
options.reporter.success("build", `${id}: built ${path.join(vaultDir, "main.js")}`);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
options.reporter.failure("build", `${id}: build had ${String(result.errors.length)} error(s)`);
|
|
170
|
+
}
|
|
171
|
+
syncAssets();
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
// A throw inside an esbuild hook would surface as a bare esbuild error; say what failed.
|
|
175
|
+
state.hookFailed = true;
|
|
176
|
+
options.reporter.failure("build", `${id}: reporting the build failed: ${errorMessage(error)}`);
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
firstBuildDone();
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
});
|
|
186
|
+
if (!watching) {
|
|
187
|
+
let buildFailed = false;
|
|
188
|
+
try {
|
|
189
|
+
await ctx.rebuild();
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (!isBuildFailure(error)) {
|
|
193
|
+
await ctx.dispose();
|
|
194
|
+
throw error;
|
|
195
|
+
}
|
|
196
|
+
// The errors were printed by the onEnd hook.
|
|
197
|
+
buildFailed = true;
|
|
198
|
+
}
|
|
199
|
+
await ctx.dispose();
|
|
200
|
+
return buildFailed || state.assetsFailed || state.hookFailed ? 1 : 0;
|
|
201
|
+
}
|
|
202
|
+
const manifestFile = path.join(cwd, "manifest.json");
|
|
203
|
+
try {
|
|
204
|
+
state.watching = true;
|
|
205
|
+
// Register before ctx.watch(): its first build, the only one at start, then comes after the pollers took
|
|
206
|
+
// their baseline. A failed first build keeps the watch alive: fixing the file triggers the next build.
|
|
207
|
+
pollCssFiles();
|
|
208
|
+
watchFile(manifestFile, { interval: POLL_INTERVAL_MS }, syncAssets);
|
|
209
|
+
if (build.css !== false && cssEntry !== undefined && !existsSync(cssEntry)) {
|
|
210
|
+
// Same rule as the CSS step: a hand-written stylesheet is a normal setup, no stylesheet at all is not.
|
|
211
|
+
const message = `${id}: no ${build.css.entry}, CSS not watched`;
|
|
212
|
+
if (existsSync(path.resolve(cwd, build.css.outfile))) {
|
|
213
|
+
options.reporter.info("build", message);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
options.reporter.warn("build", message);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
await ctx.watch();
|
|
220
|
+
await firstBuild;
|
|
221
|
+
options.reporter.info("build", `${id}: Watching for changes (Ctrl+C to stop)`);
|
|
222
|
+
await aborted(options.signal);
|
|
223
|
+
}
|
|
224
|
+
finally {
|
|
225
|
+
state.watching = false;
|
|
226
|
+
for (const timer of recheckTimers) {
|
|
227
|
+
clearTimeout(timer);
|
|
228
|
+
}
|
|
229
|
+
recheckTimers.clear();
|
|
230
|
+
for (const file of polled) {
|
|
231
|
+
unwatchFile(file, onCssPoll);
|
|
232
|
+
}
|
|
233
|
+
polled.clear();
|
|
234
|
+
unwatchFile(manifestFile, syncAssets);
|
|
235
|
+
await ctx.dispose();
|
|
236
|
+
}
|
|
237
|
+
options.reporter.info("build", `${id}: watch stopped`);
|
|
238
|
+
return 0;
|
|
239
|
+
}
|
|
240
|
+
/** Paths that cannot be a vault plugin folder, even with `--force`. */
|
|
241
|
+
function checkVaultDirArgument(dir, vaultDir, cwd) {
|
|
242
|
+
if (dir.startsWith("~")) {
|
|
243
|
+
return `"${dir}" starts with "~", which is not expanded here. Pass an absolute path.`;
|
|
244
|
+
}
|
|
245
|
+
if (vaultDir === path.resolve(cwd)) {
|
|
246
|
+
return "the vault folder cannot be the plugin root, the build would overwrite main.js and manifest.json in place. Pass the plugin folder inside your dev vault.";
|
|
247
|
+
}
|
|
248
|
+
return undefined;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Refuses to write into a folder that holds a different plugin. `VAULT_PLUGIN` stays exported in a shell,
|
|
252
|
+
* so a build of plugin B would silently overwrite plugin A's main.js and manifest.json in the dev vault.
|
|
253
|
+
* A folder without a readable manifest is treated as fresh.
|
|
254
|
+
*/
|
|
255
|
+
export function checkVaultTarget(vaultDir, pluginId) {
|
|
256
|
+
const other = readTargetPluginId(path.join(vaultDir, "manifest.json"));
|
|
257
|
+
if (other === undefined || other === pluginId) {
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
return `${vaultDir} holds plugin ${other}, not ${pluginId}. Check VAULT_PLUGIN / --vault, or pass --force to overwrite it.`;
|
|
261
|
+
}
|
|
262
|
+
function readTargetPluginId(manifestFile) {
|
|
263
|
+
try {
|
|
264
|
+
const parsed = JSON.parse(readFileSync(manifestFile, "utf8"));
|
|
265
|
+
if (typeof parsed === "object" && parsed !== null && "id" in parsed && typeof parsed.id === "string") {
|
|
266
|
+
return parsed.id;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
// No manifest, or one that is not JSON: nothing there to protect.
|
|
271
|
+
}
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
function errorMessage(error) {
|
|
275
|
+
return error instanceof Error ? error.message : String(error);
|
|
276
|
+
}
|
|
277
|
+
function aborted(signal) {
|
|
278
|
+
return new Promise((resolve) => {
|
|
279
|
+
if (signal === undefined) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (signal.aborted) {
|
|
283
|
+
resolve();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
signal.addEventListener("abort", () => {
|
|
287
|
+
resolve();
|
|
288
|
+
}, { once: true });
|
|
289
|
+
});
|
|
290
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"commit":"f3267c5"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type ParsedArgv = {
|
|
2
|
+
readonly kind: "help";
|
|
3
|
+
} | {
|
|
4
|
+
readonly kind: "version";
|
|
5
|
+
} | {
|
|
6
|
+
readonly kind: "command-help";
|
|
7
|
+
readonly name: string;
|
|
8
|
+
} | {
|
|
9
|
+
readonly kind: "run";
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly args: readonly string[];
|
|
12
|
+
};
|
|
13
|
+
/** Pure argv parsing (argv without the node and script entries). Does not know which commands exist. */
|
|
14
|
+
export declare function parseArgv(argv: readonly string[]): ParsedArgv;
|
package/dist/cli/argv.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const isHelpFlag = (arg) => arg === "--help" || arg === "-h";
|
|
2
|
+
/** Pure argv parsing (argv without the node and script entries). Does not know which commands exist. */
|
|
3
|
+
export function parseArgv(argv) {
|
|
4
|
+
const [first, ...rest] = argv;
|
|
5
|
+
if (first === undefined || isHelpFlag(first)) {
|
|
6
|
+
return { kind: "help" };
|
|
7
|
+
}
|
|
8
|
+
if (first === "--version") {
|
|
9
|
+
return { kind: "version" };
|
|
10
|
+
}
|
|
11
|
+
// Everything after a bare "--" belongs to the command, including a literal -h.
|
|
12
|
+
const separator = rest.indexOf("--");
|
|
13
|
+
const ownArgs = separator === -1 ? rest : rest.slice(0, separator);
|
|
14
|
+
if (ownArgs.some(isHelpFlag)) {
|
|
15
|
+
return { kind: "command-help", name: first };
|
|
16
|
+
}
|
|
17
|
+
return { kind: "run", name: first, args: rest };
|
|
18
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { buildCommands } from "../build/commands.js";
|
|
2
|
+
import { demoCommands } from "../demo/commands.js";
|
|
3
|
+
import { kitCommands } from "../kit/commands.js";
|
|
4
|
+
import { releaseCommands } from "../release/commands.js";
|
|
5
|
+
import { scanCommands } from "../scanner/commands.js";
|
|
6
|
+
/** Commands shipped with the kit. Each module adds its own entry here when it lands. */
|
|
7
|
+
export const builtinCommands = [...buildCommands, ...scanCommands, ...demoCommands, ...releaseCommands, ...kitCommands];
|
package/dist/cli/main.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { builtinCommands } from "./commands.js";
|
|
2
|
+
import { createProcessReporter } from "./process-reporter.js";
|
|
3
|
+
import { createRegistry } from "./registry.js";
|
|
4
|
+
import { runCli } from "./run.js";
|
|
5
|
+
import { describeKitVersion } from "./version.js";
|
|
6
|
+
/** Binds the CLI to the real process. Called by bin/opdk.js; returns the exit code. */
|
|
7
|
+
export function main(argv) {
|
|
8
|
+
return runCli(argv, {
|
|
9
|
+
registry: createRegistry(builtinCommands),
|
|
10
|
+
version: describeKitVersion(import.meta.url),
|
|
11
|
+
ctx: {
|
|
12
|
+
cwd: process.cwd(),
|
|
13
|
+
env: process.env,
|
|
14
|
+
stdout: (text) => process.stdout.write(text),
|
|
15
|
+
stderr: (text) => process.stderr.write(text),
|
|
16
|
+
reporter: createProcessReporter(),
|
|
17
|
+
onInterrupt: (listener) => {
|
|
18
|
+
process.once("SIGINT", listener);
|
|
19
|
+
return () => process.off("SIGINT", listener);
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createReporter, shouldColor } from "./reporter.js";
|
|
2
|
+
function isTerminal(stream) {
|
|
3
|
+
return stream.isTTY === true;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* A reporter bound to the real stdout/stderr; colour is decided per stream. Used by `main.ts` and `bin/opdk.js`.
|
|
7
|
+
*/
|
|
8
|
+
export function createProcessReporter() {
|
|
9
|
+
return createReporter({
|
|
10
|
+
stdout: (text) => process.stdout.write(text),
|
|
11
|
+
stderr: (text) => process.stderr.write(text),
|
|
12
|
+
colorStdout: shouldColor({ isTTY: isTerminal(process.stdout), env: process.env }),
|
|
13
|
+
colorStderr: shouldColor({ isTTY: isTerminal(process.stderr), env: process.env }),
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Command } from "./types.js";
|
|
2
|
+
export type CommandRegistry = ReadonlyMap<string, Command>;
|
|
3
|
+
/** Builds the lookup table. A Map, so names like "constructor" never hit Object.prototype. */
|
|
4
|
+
export declare function createRegistry(commands: readonly Command[]): CommandRegistry;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
|
|
2
|
+
/** Builds the lookup table. A Map, so names like "constructor" never hit Object.prototype. */
|
|
3
|
+
export function createRegistry(commands) {
|
|
4
|
+
const registry = new Map();
|
|
5
|
+
for (const command of commands) {
|
|
6
|
+
if (!NAME_PATTERN.test(command.name)) {
|
|
7
|
+
throw new Error(`Invalid command name "${command.name}": use lowercase letters, digits and dashes, starting with a letter.`);
|
|
8
|
+
}
|
|
9
|
+
if (registry.has(command.name)) {
|
|
10
|
+
throw new Error(`Command "${command.name}" is registered twice.`);
|
|
11
|
+
}
|
|
12
|
+
registry.set(command.name, command);
|
|
13
|
+
}
|
|
14
|
+
return registry;
|
|
15
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one place that decides how CLI output looks. Commands call the reporter instead of writing
|
|
3
|
+
* `[step] text` themselves, so a glance tells which step passed (`✓`, green) and which failed (`✗`, red).
|
|
4
|
+
*
|
|
5
|
+
* Symbols stay in plain mode, so a CI log reads the same as a terminal, just without escape codes.
|
|
6
|
+
* Errors go to stderr, everything else to stdout.
|
|
7
|
+
*/
|
|
8
|
+
export interface ColorEnv {
|
|
9
|
+
readonly isTTY: boolean;
|
|
10
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Whether a stream gets ANSI colour. Modelled on Node's `getColorDepth` and the supports-color package, with deliberate
|
|
14
|
+
* differences (`NO_COLOR` beats `FORCE_COLOR`, an empty `NO_COLOR` is ignored, `GITHUB_ACTIONS` counts). In order:
|
|
15
|
+
* - a non-empty `NO_COLOR` (no-color.org) turns it off;
|
|
16
|
+
* - `FORCE_COLOR` set: `0` or `false` turn it off, anything else (an empty value included) turns it on, even
|
|
17
|
+
* on a pipe and even with `TERM=dumb`;
|
|
18
|
+
* - `TERM=dumb` turns it off;
|
|
19
|
+
* - `GITHUB_ACTIONS=true` turns it on, because the Actions log viewer renders ANSI;
|
|
20
|
+
* - otherwise colour only when the stream is a terminal.
|
|
21
|
+
*/
|
|
22
|
+
export declare function shouldColor({ isTTY, env }: ColorEnv): boolean;
|
|
23
|
+
/** Width of the step column. `bundle` is the longest step name the kit prints today. */
|
|
24
|
+
export declare const STEP_WIDTH = 6;
|
|
25
|
+
export interface FailureExtras {
|
|
26
|
+
/** Lines indented under the failure, in the failure colour. */
|
|
27
|
+
readonly details?: readonly string[];
|
|
28
|
+
/** What to do about it, indented under the details. */
|
|
29
|
+
readonly hint?: string;
|
|
30
|
+
}
|
|
31
|
+
export interface Reporter {
|
|
32
|
+
/** `✓ step message` on stdout. */
|
|
33
|
+
success(step: string, message: string): void;
|
|
34
|
+
/** `· step message` on stdout: progress that is neither a result nor a problem. */
|
|
35
|
+
info(step: string, message: string): void;
|
|
36
|
+
/** `! step message` on stdout; a warning does not fail the command. */
|
|
37
|
+
warn(step: string, message: string): void;
|
|
38
|
+
/** `✗ step message` on stderr, then the details and the hint. */
|
|
39
|
+
failure(step: string, message: string, extras?: FailureExtras): void;
|
|
40
|
+
/** `✗ message` on stderr, for errors that belong to no step (the CLI itself). Extra lines are indented. */
|
|
41
|
+
error(message: string, details?: readonly string[]): void;
|
|
42
|
+
/** Whether stderr gets colour; passed to esbuild so its own message layout matches. */
|
|
43
|
+
readonly colorStderr: boolean;
|
|
44
|
+
/** Text that is already formatted, such as esbuild's messages. Written as is. */
|
|
45
|
+
rawStderr(text: string): void;
|
|
46
|
+
}
|
|
47
|
+
export interface ReporterOptions {
|
|
48
|
+
stdout(text: string): void;
|
|
49
|
+
stderr(text: string): void;
|
|
50
|
+
readonly colorStdout: boolean;
|
|
51
|
+
readonly colorStderr: boolean;
|
|
52
|
+
}
|
|
53
|
+
export declare function createReporter(options: ReporterOptions): Reporter;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one place that decides how CLI output looks. Commands call the reporter instead of writing
|
|
3
|
+
* `[step] text` themselves, so a glance tells which step passed (`✓`, green) and which failed (`✗`, red).
|
|
4
|
+
*
|
|
5
|
+
* Symbols stay in plain mode, so a CI log reads the same as a terminal, just without escape codes.
|
|
6
|
+
* Errors go to stderr, everything else to stdout.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Whether a stream gets ANSI colour. Modelled on Node's `getColorDepth` and the supports-color package, with deliberate
|
|
10
|
+
* differences (`NO_COLOR` beats `FORCE_COLOR`, an empty `NO_COLOR` is ignored, `GITHUB_ACTIONS` counts). In order:
|
|
11
|
+
* - a non-empty `NO_COLOR` (no-color.org) turns it off;
|
|
12
|
+
* - `FORCE_COLOR` set: `0` or `false` turn it off, anything else (an empty value included) turns it on, even
|
|
13
|
+
* on a pipe and even with `TERM=dumb`;
|
|
14
|
+
* - `TERM=dumb` turns it off;
|
|
15
|
+
* - `GITHUB_ACTIONS=true` turns it on, because the Actions log viewer renders ANSI;
|
|
16
|
+
* - otherwise colour only when the stream is a terminal.
|
|
17
|
+
*/
|
|
18
|
+
export function shouldColor({ isTTY, env }) {
|
|
19
|
+
const noColor = env.NO_COLOR;
|
|
20
|
+
if (noColor !== undefined && noColor !== "") {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const force = env.FORCE_COLOR;
|
|
24
|
+
if (force !== undefined) {
|
|
25
|
+
return force !== "0" && force !== "false";
|
|
26
|
+
}
|
|
27
|
+
if (env.TERM === "dumb") {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if (env.GITHUB_ACTIONS === "true") {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return isTTY;
|
|
34
|
+
}
|
|
35
|
+
// A table of SGR codes instead of `util.styleText`: there must be exactly one decision point, and `styleText`
|
|
36
|
+
// re-applies its own TTY and NO_COLOR check unless `validateStream: false` is passed. It also closes each
|
|
37
|
+
// attribute with its own reset, while the output here ends every painted span with one `ESC[0m`.
|
|
38
|
+
const SGR = { bold: 1, dim: 2, red: 31, green: 32, yellow: 33 };
|
|
39
|
+
const RESET = "\u001b[0m";
|
|
40
|
+
function paint(text, styles, color) {
|
|
41
|
+
if (!color || text === "") {
|
|
42
|
+
return text;
|
|
43
|
+
}
|
|
44
|
+
return `\u001b[${styles.map((style) => String(SGR[style])).join(";")}m${text}${RESET}`;
|
|
45
|
+
}
|
|
46
|
+
/** Width of the step column. `bundle` is the longest step name the kit prints today. */
|
|
47
|
+
export const STEP_WIDTH = 6;
|
|
48
|
+
const SYMBOL = { success: "✓", failure: "✗", info: "·", warning: "!" };
|
|
49
|
+
const KIND_STYLE = {
|
|
50
|
+
success: "green",
|
|
51
|
+
failure: "red",
|
|
52
|
+
info: "dim",
|
|
53
|
+
warning: "yellow",
|
|
54
|
+
};
|
|
55
|
+
export function createReporter(options) {
|
|
56
|
+
const { colorStdout, colorStderr } = options;
|
|
57
|
+
const stdout = (text) => {
|
|
58
|
+
options.stdout(text);
|
|
59
|
+
};
|
|
60
|
+
const stderr = (text) => {
|
|
61
|
+
options.stderr(text);
|
|
62
|
+
};
|
|
63
|
+
const line = (out, color, kind, step, message) => {
|
|
64
|
+
const style = KIND_STYLE[kind];
|
|
65
|
+
const symbol = paint(SYMBOL[kind], [style], color);
|
|
66
|
+
const name = paint(step.padEnd(STEP_WIDTH), kind === "info" ? ["dim", "bold"] : ["bold", style], color);
|
|
67
|
+
out(`${symbol} ${name} ${paint(message, kind === "info" ? ["dim"] : [style], color)}\n`);
|
|
68
|
+
};
|
|
69
|
+
const indent = " ".repeat(STEP_WIDTH + 4);
|
|
70
|
+
return {
|
|
71
|
+
success: (step, message) => {
|
|
72
|
+
line(stdout, colorStdout, "success", step, message);
|
|
73
|
+
},
|
|
74
|
+
info: (step, message) => {
|
|
75
|
+
line(stdout, colorStdout, "info", step, message);
|
|
76
|
+
},
|
|
77
|
+
warn: (step, message) => {
|
|
78
|
+
line(stdout, colorStdout, "warning", step, message);
|
|
79
|
+
},
|
|
80
|
+
failure: (step, message, extras = {}) => {
|
|
81
|
+
line(stderr, colorStderr, "failure", step, message);
|
|
82
|
+
for (const detail of extras.details ?? []) {
|
|
83
|
+
stderr(`${indent} ${paint(detail, ["red"], colorStderr)}\n`);
|
|
84
|
+
}
|
|
85
|
+
if (extras.hint !== undefined) {
|
|
86
|
+
stderr(`${indent}${paint(extras.hint, ["yellow"], colorStderr)}\n`);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
error: (message, details = []) => {
|
|
90
|
+
stderr(`${paint(SYMBOL.failure, ["red"], colorStderr)} ${paint(message, ["red"], colorStderr)}\n`);
|
|
91
|
+
for (const detail of details) {
|
|
92
|
+
stderr(` ${paint(detail, ["red"], colorStderr)}\n`);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
colorStderr,
|
|
96
|
+
rawStderr: (text) => {
|
|
97
|
+
stderr(text);
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CommandRegistry } from "./registry.js";
|
|
2
|
+
import type { CommandContext } from "./types.js";
|
|
3
|
+
export interface CliOptions {
|
|
4
|
+
readonly registry: CommandRegistry;
|
|
5
|
+
readonly version: string;
|
|
6
|
+
readonly ctx: CommandContext;
|
|
7
|
+
}
|
|
8
|
+
/** Runs one CLI invocation and returns the exit code. */
|
|
9
|
+
export declare function runCli(argv: readonly string[], options: CliOptions): Promise<number>;
|
package/dist/cli/run.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { parseArgv } from "./argv.js";
|
|
2
|
+
const ROADMAP_URL = "https://github.com/Dimagious/obsidian-plugin-dev-kit#roadmap";
|
|
3
|
+
/** Runs one CLI invocation and returns the exit code. */
|
|
4
|
+
export async function runCli(argv, options) {
|
|
5
|
+
const { registry, version, ctx } = options;
|
|
6
|
+
const parsed = parseArgv(argv);
|
|
7
|
+
switch (parsed.kind) {
|
|
8
|
+
case "help":
|
|
9
|
+
ctx.stdout(renderHelp(registry, version));
|
|
10
|
+
return 0;
|
|
11
|
+
case "version":
|
|
12
|
+
ctx.stdout(`${version}\n`);
|
|
13
|
+
return 0;
|
|
14
|
+
case "command-help": {
|
|
15
|
+
const command = registry.get(parsed.name);
|
|
16
|
+
if (command === undefined) {
|
|
17
|
+
return unknownCommand(parsed.name, ctx);
|
|
18
|
+
}
|
|
19
|
+
ctx.stdout(`${command.usage.trimEnd()}\n`);
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
case "run": {
|
|
23
|
+
const command = registry.get(parsed.name);
|
|
24
|
+
if (command === undefined) {
|
|
25
|
+
return unknownCommand(parsed.name, ctx);
|
|
26
|
+
}
|
|
27
|
+
return runCommand(command, parsed.args, ctx);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function unknownCommand(name, ctx) {
|
|
32
|
+
ctx.reporter.error(`opdk: unknown command "${name}". Run "opdk --help" to list the available commands.`);
|
|
33
|
+
return 1;
|
|
34
|
+
}
|
|
35
|
+
async function runCommand(command, args, ctx) {
|
|
36
|
+
try {
|
|
37
|
+
return await command.run(args, ctx);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
41
|
+
const debug = ctx.env.OPDK_DEBUG === "1" && error instanceof Error ? error.stack : undefined;
|
|
42
|
+
ctx.reporter.error(`opdk: command "${command.name}" failed: ${message}`, debug?.split("\n"));
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function renderHelp(registry, version) {
|
|
47
|
+
const lines = [`opdk ${version}`, "", "Usage: opdk <command> [options]", ""];
|
|
48
|
+
if (registry.size === 0) {
|
|
49
|
+
lines.push(`No commands are registered yet. See the roadmap: ${ROADMAP_URL}`);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
lines.push("Commands:");
|
|
53
|
+
const width = Math.max(...Array.from(registry.keys(), (name) => name.length));
|
|
54
|
+
for (const command of registry.values()) {
|
|
55
|
+
lines.push(` ${command.name.padEnd(width)} ${command.summary}`);
|
|
56
|
+
}
|
|
57
|
+
lines.push("", 'Run "opdk <command> --help" for details.');
|
|
58
|
+
}
|
|
59
|
+
lines.push("", "Options:", " -h, --help Show this help", " --version Show the kit version");
|
|
60
|
+
return `${lines.join("\n")}\n`;
|
|
61
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Reporter } from "./reporter.js";
|
|
2
|
+
/** What a command gets from the CLI. Everything process-related is injected so commands stay testable. */
|
|
3
|
+
export interface CommandContext {
|
|
4
|
+
/** Directory the user ran `opdk` in (the plugin root). */
|
|
5
|
+
readonly cwd: string;
|
|
6
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
7
|
+
stdout(text: string): void;
|
|
8
|
+
stderr(text: string): void;
|
|
9
|
+
/** How a command prints results and problems; use it instead of writing `stdout`/`stderr` directly. */
|
|
10
|
+
readonly reporter: Reporter;
|
|
11
|
+
/**
|
|
12
|
+
* Registers a Ctrl+C (SIGINT) listener for long-running commands such as `build --vault --watch`
|
|
13
|
+
* and returns a function that removes it. Only registered on demand, so a plain build stays interruptible.
|
|
14
|
+
*/
|
|
15
|
+
onInterrupt?(listener: () => void): () => void;
|
|
16
|
+
}
|
|
17
|
+
/** A subcommand. Each kit module registers one of these. */
|
|
18
|
+
export interface Command {
|
|
19
|
+
/** Lowercase letters, digits and dashes, starting with a letter. */
|
|
20
|
+
readonly name: string;
|
|
21
|
+
/** One line shown in `opdk --help`. */
|
|
22
|
+
readonly summary: string;
|
|
23
|
+
/** Full text shown by `opdk <name> --help`. */
|
|
24
|
+
readonly usage: string;
|
|
25
|
+
/** Returns the process exit code. A thrown error is reported by the CLI and exits 1. */
|
|
26
|
+
run(args: readonly string[], ctx: CommandContext): Promise<number>;
|
|
27
|
+
}
|