@dsh-workbench/shared 1.5.2-alpha.3 → 1.5.2-alpha.5
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/dist/components/Activity.css +69 -0
- package/dist/components/Activity.d.ts +56 -0
- package/dist/components/Activity.js +47 -0
- package/dist/components/ApprovalDetail.css +3 -0
- package/dist/components/ApprovalDetail.d.ts +42 -0
- package/dist/components/ApprovalDetail.js +53 -0
- package/dist/components/ApprovalPlan.css +39 -0
- package/dist/components/ApprovalPlan.d.ts +27 -0
- package/dist/components/ApprovalPlan.js +19 -0
- package/dist/components/Card.css +19 -0
- package/dist/components/Card.d.ts +26 -0
- package/dist/components/Card.js +18 -0
- package/dist/components/CardAnchor.d.ts +21 -0
- package/dist/components/CardAnchor.js +19 -0
- package/dist/components/CardAnchor.module.css +3 -0
- package/dist/components/CardBoundary.d.ts +42 -0
- package/dist/components/CardBoundary.js +58 -0
- package/dist/components/CardBoundary.module.css +5 -0
- package/dist/components/CardHead.css +87 -0
- package/dist/components/CardHead.d.ts +30 -0
- package/dist/components/CardHead.js +27 -0
- package/dist/components/ChoiceGroup.css +182 -0
- package/dist/components/ChoiceGroup.d.ts +54 -0
- package/dist/components/ChoiceGroup.js +63 -0
- package/dist/components/ConfirmBar.css +26 -0
- package/dist/components/ConfirmBar.d.ts +36 -0
- package/dist/components/ConfirmBar.js +26 -0
- package/dist/components/EmptyIllustration.d.ts +12 -0
- package/dist/components/EmptyIllustration.js +32 -0
- package/dist/components/Field.css +20 -0
- package/dist/components/Field.d.ts +18 -0
- package/dist/components/Field.js +15 -0
- package/dist/components/Form.css +6 -0
- package/dist/components/Form.d.ts +22 -0
- package/dist/components/Form.js +60 -0
- package/dist/components/FormActions.css +13 -0
- package/dist/components/FormActions.d.ts +29 -0
- package/dist/components/FormActions.js +43 -0
- package/dist/components/FormNodes.d.ts +107 -0
- package/dist/components/FormNodes.js +165 -0
- package/dist/components/FormRows.css +107 -0
- package/dist/components/FormRows.d.ts +36 -0
- package/dist/components/FormRows.js +26 -0
- package/dist/components/FormTypes.d.ts +84 -0
- package/dist/components/FormTypes.js +1 -0
- package/dist/components/Link.css +22 -0
- package/dist/components/Link.d.ts +22 -0
- package/dist/components/Link.js +17 -0
- package/dist/components/LongTaskRow.css +171 -0
- package/dist/components/LongTaskRow.d.ts +94 -0
- package/dist/components/LongTaskRow.js +103 -0
- package/dist/components/Note.css +13 -0
- package/dist/components/Note.d.ts +20 -0
- package/dist/components/Note.js +14 -0
- package/dist/components/ResourceStates.css +114 -0
- package/dist/components/ResourceStates.d.ts +44 -0
- package/dist/components/ResourceStates.js +41 -0
- package/dist/components/Rows.css +42 -0
- package/dist/components/Rows.d.ts +28 -0
- package/dist/components/Rows.js +21 -0
- package/dist/components/SaveBar.d.ts +24 -0
- package/dist/components/SaveBar.js +25 -0
- package/dist/components/SelectField.css +182 -0
- package/dist/components/SelectField.d.ts +89 -0
- package/dist/components/SelectField.js +260 -0
- package/dist/components/SelectFieldNodes.d.ts +49 -0
- package/dist/components/SelectFieldNodes.js +43 -0
- package/dist/components/SettingsSection.css +25 -0
- package/dist/components/SettingsSection.d.ts +20 -0
- package/dist/components/SettingsSection.js +17 -0
- package/dist/components/SetupShell.d.ts +93 -0
- package/dist/components/SetupShell.js +64 -0
- package/dist/components/SetupShell.module.css +81 -0
- package/dist/components/Spinner.css +5 -0
- package/dist/components/Spinner.d.ts +15 -0
- package/dist/components/Spinner.js +13 -0
- package/dist/components/Stack.css +6 -0
- package/dist/components/Stack.d.ts +14 -0
- package/dist/components/Stack.js +10 -0
- package/dist/components/StateBlock.css +38 -0
- package/dist/components/StateBlock.d.ts +24 -0
- package/dist/components/StateBlock.js +22 -0
- package/dist/components/TextField.css +32 -0
- package/dist/components/TextField.d.ts +14 -0
- package/dist/components/TextField.js +10 -0
- package/dist/components/board-module.d.ts +307 -0
- package/dist/components/board-module.js +1 -0
- package/dist/components/card-view-validate.d.ts +5 -0
- package/dist/components/card-view-validate.js +256 -0
- package/dist/components/icons.d.ts +63 -0
- package/dist/components/icons.js +107 -0
- package/dist/components/layout/icon-command.d.ts +8 -0
- package/dist/components/layout/icon-command.js +21 -0
- package/dist/contract/capture.d.ts +16 -0
- package/dist/contract/capture.js +1 -0
- package/dist/contract/diagnostics.d.ts +34 -0
- package/dist/contract/diagnostics.js +124 -0
- package/dist/contract/protocol/unit-manager.d.ts +50 -0
- package/dist/contract/protocol/unit-manager.js +2 -0
- package/dist/host/adapter-contract.d.ts +3 -0
- package/dist/host/adapter-contract.js +95 -0
- package/dist/host/adapter-set.d.ts +27 -0
- package/dist/host/adapter-set.js +258 -0
- package/dist/host/adapter-types.d.ts +87 -0
- package/dist/host/adapter-types.js +23 -0
- package/dist/host/adapters/platform.d.ts +14 -0
- package/dist/host/adapters/platform.js +40 -0
- package/dist/host/adapters.d.ts +28 -0
- package/dist/host/adapters.js +9 -0
- package/dist/host/auth-loss.d.ts +33 -0
- package/dist/host/auth-loss.js +107 -0
- package/dist/host/board-fact.d.ts +8 -0
- package/dist/host/board-fact.js +16 -0
- package/dist/host/boot-record.d.ts +11 -0
- package/dist/host/boot-record.js +31 -0
- package/dist/host/browser.d.ts +3 -0
- package/dist/host/browser.js +3 -0
- package/dist/host/bundle-manager.d.ts +4 -0
- package/dist/host/bundle-manager.js +338 -0
- package/dist/host/changelog.d.ts +28 -0
- package/dist/host/changelog.js +201 -0
- package/dist/host/cli-error.d.ts +8 -0
- package/dist/host/cli-error.js +70 -0
- package/dist/host/cli.d.ts +4 -0
- package/dist/host/cli.js +9 -0
- package/dist/host/config-sync.d.ts +40 -0
- package/dist/host/config-sync.js +267 -0
- package/dist/host/context-ref.d.ts +13 -0
- package/dist/host/context-ref.js +41 -0
- package/dist/host/data-error.d.ts +15 -0
- package/dist/host/data-error.js +23 -0
- package/dist/host/data-plan-check.d.ts +10 -0
- package/dist/host/data-plan-check.js +315 -0
- package/dist/host/data-plan.d.ts +15 -0
- package/dist/host/data-plan.js +283 -0
- package/dist/host/data-root.d.ts +5 -0
- package/dist/host/data-root.js +23 -0
- package/dist/host/diy-debug.d.ts +80 -0
- package/dist/host/diy-debug.js +89 -0
- package/dist/host/diy-preset.d.ts +26 -0
- package/dist/host/diy-preset.js +115 -0
- package/dist/host/diy-record.d.ts +135 -0
- package/dist/host/diy-record.js +163 -0
- package/dist/host/dsh-compat.d.ts +27 -0
- package/dist/host/dsh-compat.js +75 -0
- package/dist/host/dsh-plugins.d.ts +5 -0
- package/dist/host/dsh-plugins.js +51 -0
- package/dist/host/env-policy.d.ts +82 -0
- package/dist/host/env-policy.js +181 -0
- package/dist/host/extension.d.ts +7 -0
- package/dist/host/extension.js +40 -0
- package/dist/host/facts.d.ts +31 -0
- package/dist/host/facts.js +58 -0
- package/dist/host/fs-write.d.ts +96 -0
- package/dist/host/fs-write.js +183 -0
- package/dist/host/git.d.ts +5 -0
- package/dist/host/git.js +1 -0
- package/dist/host/host-actions.d.ts +2 -0
- package/dist/host/host-actions.js +5 -0
- package/dist/host/host-kit.d.ts +37 -0
- package/dist/host/host-kit.js +27 -0
- package/dist/host/http.d.ts +47 -0
- package/dist/host/http.js +289 -0
- package/dist/host/install-failure.d.ts +50 -0
- package/dist/host/install-failure.js +184 -0
- package/dist/host/installed-module.d.ts +25 -0
- package/dist/host/installed-module.js +289 -0
- package/dist/host/installed-sources.d.ts +10 -0
- package/dist/host/installed-sources.js +63 -0
- package/dist/host/lease.d.ts +70 -0
- package/dist/host/lease.js +264 -0
- package/dist/host/lib-capabilities.d.ts +22 -0
- package/dist/host/lib-capabilities.js +37 -0
- package/dist/host/lib-install.d.ts +19 -0
- package/dist/host/lib-install.js +40 -0
- package/dist/host/lib-runners.d.ts +18 -0
- package/dist/host/lib-runners.js +46 -0
- package/dist/host/local-declaration.d.ts +7 -0
- package/dist/host/local-declaration.js +33 -0
- package/dist/host/long-task.d.ts +97 -0
- package/dist/host/long-task.js +297 -0
- package/dist/host/market-index-claim.d.ts +19 -0
- package/dist/host/market-index-claim.js +87 -0
- package/dist/host/market-index-doc.d.ts +12 -0
- package/dist/host/market-index-doc.js +1 -0
- package/dist/host/market-index-parse.d.ts +28 -0
- package/dist/host/market-index-parse.js +413 -0
- package/dist/host/market-index.d.ts +13 -0
- package/dist/host/market-index.js +105 -0
- package/dist/host/module-root.d.ts +1 -0
- package/dist/host/module-root.js +5 -0
- package/dist/host/needs.d.ts +4 -0
- package/dist/host/needs.js +2 -0
- package/dist/host/npm-registry.d.ts +10 -0
- package/dist/host/npm-registry.js +16 -0
- package/dist/host/package-content.d.ts +8 -0
- package/dist/host/package-content.js +90 -0
- package/dist/host/package-kind.d.ts +13 -0
- package/dist/host/package-kind.js +51 -0
- package/dist/host/package-route.d.ts +33 -0
- package/dist/host/package-route.js +68 -0
- package/dist/host/panel-state.d.ts +120 -0
- package/dist/host/panel-state.js +177 -0
- package/dist/host/patch-blocks.d.ts +13 -0
- package/dist/host/patch-blocks.js +52 -0
- package/dist/host/patch.d.ts +4 -0
- package/dist/host/patch.js +7 -0
- package/dist/host/paths.d.ts +12 -0
- package/dist/host/paths.js +20 -0
- package/dist/host/payload.d.ts +12 -0
- package/dist/host/payload.js +16 -0
- package/dist/host/plan-results.d.ts +5 -0
- package/dist/host/plan-results.js +71 -0
- package/dist/host/plan-run.d.ts +41 -0
- package/dist/host/plan-run.js +87 -0
- package/dist/host/platform.d.ts +14 -0
- package/dist/host/platform.js +40 -0
- package/dist/host/plugin-ledger.d.ts +32 -0
- package/dist/host/plugin-ledger.js +106 -0
- package/dist/host/plugin.d.ts +3 -0
- package/dist/host/plugin.js +38 -0
- package/dist/host/port-types.d.ts +233 -0
- package/dist/host/port-types.js +1 -0
- package/dist/host/preflight.d.ts +32 -0
- package/dist/host/preflight.js +1 -0
- package/dist/host/process-alive.d.ts +6 -0
- package/dist/host/process-alive.js +16 -0
- package/dist/host/profile-bundles.d.ts +20 -0
- package/dist/host/profile-bundles.js +48 -0
- package/dist/host/profile-create.d.ts +19 -0
- package/dist/host/profile-create.js +124 -0
- package/dist/host/profile-port.d.ts +4 -0
- package/dist/host/profile-port.js +4 -0
- package/dist/host/profile-registries.d.ts +10 -0
- package/dist/host/profile-registries.js +149 -0
- package/dist/host/provision.d.ts +32 -0
- package/dist/host/provision.js +105 -0
- package/dist/host/read-json.d.ts +1 -0
- package/dist/host/read-json.js +11 -0
- package/dist/host/recipe-rules.d.ts +5 -0
- package/dist/host/recipe-rules.js +412 -0
- package/dist/host/recipe.d.ts +14 -0
- package/dist/host/recipe.js +332 -0
- package/dist/host/registry-declaration.d.ts +21 -0
- package/dist/host/registry-declaration.js +42 -0
- package/dist/host/reinstall-record.d.ts +16 -0
- package/dist/host/reinstall-record.js +54 -0
- package/dist/host/release-age.d.ts +24 -0
- package/dist/host/release-age.js +92 -0
- package/dist/host/release-packages.generated.d.ts +1 -0
- package/dist/host/release-packages.generated.js +10 -0
- package/dist/host/release-probe.d.ts +37 -0
- package/dist/host/release-probe.js +156 -0
- package/dist/host/runtime/browser-argv.d.ts +1 -0
- package/dist/host/runtime/browser-argv.js +9 -0
- package/dist/host/runtime/dsh-compat.d.ts +1 -1
- package/dist/host/runtime/dsh-compat.js +4 -1
- package/dist/host/runtime/front-app.d.ts +6 -0
- package/dist/host/runtime/front-app.js +70 -0
- package/dist/host/shell-render.d.ts +52 -0
- package/dist/host/shell-render.js +117 -0
- package/dist/host/shell-text.d.ts +30 -0
- package/dist/host/shell-text.js +40 -0
- package/dist/host/shell.d.ts +25 -0
- package/dist/host/shell.js +55 -0
- package/dist/host/site.d.ts +45 -0
- package/dist/host/site.js +182 -0
- package/dist/host/spec.d.ts +8 -0
- package/dist/host/spec.js +24 -0
- package/dist/host/submit-confirm.d.ts +19 -0
- package/dist/host/submit-confirm.js +45 -0
- package/dist/host/supply-index.d.ts +38 -0
- package/dist/host/supply-index.js +209 -0
- package/dist/host/supply-port.d.ts +5 -0
- package/dist/host/supply-port.js +50 -0
- package/dist/host/time.d.ts +22 -0
- package/dist/host/time.js +33 -0
- package/dist/host/tokens.d.ts +26 -0
- package/dist/host/tokens.js +154 -0
- package/dist/host/tool-approval.d.ts +86 -0
- package/dist/host/tool-approval.js +118 -0
- package/dist/host/tool-kit.d.ts +8 -0
- package/dist/host/tool-kit.js +22 -0
- package/dist/host/toolbox-port.d.ts +16 -0
- package/dist/host/toolbox-port.js +1 -0
- package/dist/host/unit-config.d.ts +34 -0
- package/dist/host/unit-config.js +28 -0
- package/dist/host/unit-data.d.ts +3 -0
- package/dist/host/unit-data.js +127 -0
- package/dist/host/unit-face.d.ts +95 -0
- package/dist/host/unit-face.js +1 -0
- package/dist/host/unit-plugin.d.ts +24 -0
- package/dist/host/unit-plugin.js +25 -0
- package/dist/host/update-journal-manifest.d.ts +19 -0
- package/dist/host/update-journal-manifest.js +134 -0
- package/dist/host/update-journal-types.d.ts +33 -0
- package/dist/host/update-journal-types.js +2 -0
- package/dist/host/update-journal.d.ts +43 -0
- package/dist/host/update-journal.js +409 -0
- package/dist/host/value-handoff.d.ts +16 -0
- package/dist/host/value-handoff.js +22 -0
- package/dist/host/version-range.d.ts +2 -0
- package/dist/host/version-range.js +13 -0
- package/dist/host/version.d.ts +4 -0
- package/dist/host/version.js +32 -0
- package/dist/host/workspace-store.d.ts +19 -0
- package/dist/host/workspace-store.js +133 -0
- package/dist/host/write-result.d.ts +1 -0
- package/dist/host/write-result.js +18 -0
- package/dist/lib/dsh-client.d.ts +1 -1
- package/package.json +51 -47
- package/src/host/runtime/__tests__/dsh-compat.test.ts +10 -2
- package/src/host/runtime/dsh-compat.ts +4 -2
- package/src/lib/dsh-client.ts +2 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { now } from './time.js';
|
|
2
|
+
import { productWrite } from './seam/intent.js';
|
|
3
|
+
import { RELEASE_PACKAGES } from './release-packages.generated.js';
|
|
4
|
+
import { LEDGER_READ_TIMEOUT_MS, VERSION_CHECK_FAILURE_TTL_MS, VERSION_CHECK_TIMEOUT_MS, VERSION_CHECK_TTL_MS, } from '../config/constants.js';
|
|
5
|
+
import { compareVersions, isValidVersion } from './version.js';
|
|
6
|
+
import { parseJsonLoose } from '../lib/text.js';
|
|
7
|
+
import { quoteShellArg } from './shell-text.js';
|
|
8
|
+
function cacheFlag(cacheDir) {
|
|
9
|
+
return cacheDir === undefined ? '' : ` --cache ${quoteShellArg(cacheDir)}`;
|
|
10
|
+
}
|
|
11
|
+
export function parseLedgerVersion(stdout, name) {
|
|
12
|
+
const parsed = parseJsonLoose(stdout);
|
|
13
|
+
if (typeof parsed !== "object" || parsed === null)
|
|
14
|
+
return undefined;
|
|
15
|
+
const entry = parsed
|
|
16
|
+
.dependencies?.[name];
|
|
17
|
+
if (typeof entry !== "object" || entry === null)
|
|
18
|
+
return undefined;
|
|
19
|
+
const version = entry.version;
|
|
20
|
+
return typeof version === "string" && isValidVersion(version.trim())
|
|
21
|
+
? version.trim()
|
|
22
|
+
: undefined;
|
|
23
|
+
}
|
|
24
|
+
function parseVersion(stdout) {
|
|
25
|
+
const value = stdout.trim();
|
|
26
|
+
return isValidVersion(value) ? value : undefined;
|
|
27
|
+
}
|
|
28
|
+
function parseNewestVersion(stdout, includePrerelease) {
|
|
29
|
+
if (!includePrerelease)
|
|
30
|
+
return parseVersion(stdout);
|
|
31
|
+
const parsed = parseJsonLoose(stdout);
|
|
32
|
+
const list = Array.isArray(parsed) ? parsed : typeof parsed === 'string' ? [parsed] : [];
|
|
33
|
+
return list
|
|
34
|
+
.filter((value) => typeof value === 'string' && isValidVersion(value))
|
|
35
|
+
.map((value) => value.trim())
|
|
36
|
+
.sort((left, right) => (compareVersions(right, left) ?? 0))[0];
|
|
37
|
+
}
|
|
38
|
+
function versionCacheTtl(value) {
|
|
39
|
+
return value === undefined ? VERSION_CHECK_FAILURE_TTL_MS : VERSION_CHECK_TTL_MS;
|
|
40
|
+
}
|
|
41
|
+
export function createReleaseProbe() {
|
|
42
|
+
const latestCache = new Map();
|
|
43
|
+
const existsCache = new Map();
|
|
44
|
+
return {
|
|
45
|
+
latestVersion: async (options) => latestVersionOf(latestCache, options),
|
|
46
|
+
missingReleaseParts: async (options) => missingPartsOf(latestCache, existsCache, options),
|
|
47
|
+
probeRelease: async (options) => probeOf(latestCache, options),
|
|
48
|
+
reset: () => {
|
|
49
|
+
latestCache.clear();
|
|
50
|
+
existsCache.clear();
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function viewCommandOf(input) {
|
|
55
|
+
const target = input.includePrerelease
|
|
56
|
+
? `${quoteShellArg(input.name)} versions --json`
|
|
57
|
+
: `${quoteShellArg(input.name)} version`;
|
|
58
|
+
const tail = `--registry ${quoteShellArg(input.registry)}${cacheFlag(input.cacheDir)}`;
|
|
59
|
+
const pnpm = process.env.DSH_WB_PNPM?.trim() ?? '';
|
|
60
|
+
return [`npm view ${target} ${tail}`, `${pnpm === '' ? 'pnpm' : pnpm} view ${target} ${tail}`];
|
|
61
|
+
}
|
|
62
|
+
async function latestVersionOf(latestCache, options) {
|
|
63
|
+
const { seam, name, registry, force = false, includePrerelease = false, cacheDir } = options;
|
|
64
|
+
const key = `${registry}|${name}|${includePrerelease ? 'pre' : 'stable'}`;
|
|
65
|
+
const hit = latestCache.get(key);
|
|
66
|
+
if (!force && hit !== undefined && now() - hit.at < versionCacheTtl(hit.value))
|
|
67
|
+
return hit.value;
|
|
68
|
+
let read;
|
|
69
|
+
for (const command of viewCommandOf({ name, registry, includePrerelease, cacheDir })) {
|
|
70
|
+
const result = await seam
|
|
71
|
+
.run({
|
|
72
|
+
command,
|
|
73
|
+
intent: productWrite(),
|
|
74
|
+
retry: 'safe',
|
|
75
|
+
timeoutMs: VERSION_CHECK_TIMEOUT_MS,
|
|
76
|
+
bestEffort: true,
|
|
77
|
+
})
|
|
78
|
+
.catch(() => undefined);
|
|
79
|
+
const found = result?.exitCode === 0 ? parseNewestVersion(result.stdout, includePrerelease) : undefined;
|
|
80
|
+
if (found !== undefined) {
|
|
81
|
+
read = found;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
latestCache.set(key, { at: now(), value: read });
|
|
86
|
+
return read;
|
|
87
|
+
}
|
|
88
|
+
async function versionExists(existsCache, options) {
|
|
89
|
+
const { seam, name, version, registry, force = false, cacheDir } = options;
|
|
90
|
+
const key = `${registry}|${name}@${version}`;
|
|
91
|
+
const hit = existsCache.get(key);
|
|
92
|
+
if (!force && hit !== undefined && (hit.value || now() - hit.at < VERSION_CHECK_FAILURE_TTL_MS))
|
|
93
|
+
return hit.value;
|
|
94
|
+
const pnpm = process.env.DSH_WB_PNPM?.trim() ?? '';
|
|
95
|
+
const spec = `${quoteShellArg(`${name}@${version}`)} version --registry ${quoteShellArg(registry)}${cacheFlag(cacheDir)}`;
|
|
96
|
+
let value = false;
|
|
97
|
+
for (const command of [`npm view ${spec}`, `${pnpm === '' ? 'pnpm' : pnpm} view ${spec}`]) {
|
|
98
|
+
const result = await seam
|
|
99
|
+
.run({
|
|
100
|
+
command,
|
|
101
|
+
intent: productWrite(),
|
|
102
|
+
retry: 'safe',
|
|
103
|
+
timeoutMs: VERSION_CHECK_TIMEOUT_MS,
|
|
104
|
+
bestEffort: true,
|
|
105
|
+
})
|
|
106
|
+
.catch(() => undefined);
|
|
107
|
+
if (result?.exitCode === 0 && parseVersion(result.stdout) !== undefined) {
|
|
108
|
+
value = true;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
existsCache.set(key, { at: now(), value });
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
async function missingPartsOf(latestCache, existsCache, options) {
|
|
116
|
+
const { seam, name, registry, version, force = false, cacheDir } = options;
|
|
117
|
+
const checked = await Promise.all(RELEASE_PACKAGES.filter((part) => part !== name).map(async (part) => ({
|
|
118
|
+
part,
|
|
119
|
+
present: await versionExists(existsCache, { seam, name: part, version, registry, force, cacheDir }),
|
|
120
|
+
})));
|
|
121
|
+
return checked.filter((entry) => !entry.present).map((entry) => entry.part);
|
|
122
|
+
}
|
|
123
|
+
async function probeOf(latestCache, options) {
|
|
124
|
+
const { seam, package: pkg, registry, force = false, check = true, includePrerelease = false, cacheDir, prefix } = options;
|
|
125
|
+
if (pkg === "")
|
|
126
|
+
return undefined;
|
|
127
|
+
const ledger = await seam
|
|
128
|
+
.run({
|
|
129
|
+
command: `npm ls --prefix ${quoteShellArg(prefix ?? '')} ${quoteShellArg(pkg)} --json --depth=0${cacheFlag(cacheDir)}`,
|
|
130
|
+
intent: productWrite(),
|
|
131
|
+
retry: 'safe',
|
|
132
|
+
timeoutMs: LEDGER_READ_TIMEOUT_MS,
|
|
133
|
+
bestEffort: true,
|
|
134
|
+
})
|
|
135
|
+
.catch(() => undefined);
|
|
136
|
+
const installed = ledger === undefined ? undefined : parseLedgerVersion(ledger.stdout, pkg);
|
|
137
|
+
if (installed === undefined)
|
|
138
|
+
return undefined;
|
|
139
|
+
const source = registry === undefined || registry.trim() === ""
|
|
140
|
+
? undefined
|
|
141
|
+
: registry.trim();
|
|
142
|
+
const latest = source === undefined || !check
|
|
143
|
+
? undefined
|
|
144
|
+
: await latestVersionOf(latestCache, {
|
|
145
|
+
seam,
|
|
146
|
+
name: pkg,
|
|
147
|
+
registry: source,
|
|
148
|
+
force,
|
|
149
|
+
includePrerelease,
|
|
150
|
+
cacheDir,
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
installed,
|
|
154
|
+
...(latest === undefined ? {} : { latest }),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sessionArgv(argv: readonly string[]): readonly string[];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const SESSION_FLAG = '--session';
|
|
2
|
+
export function sessionArgv(argv) {
|
|
3
|
+
if (argv[0] !== 'browser')
|
|
4
|
+
return argv;
|
|
5
|
+
const session = argv[1];
|
|
6
|
+
if (session === undefined || session === '' || session.startsWith('-'))
|
|
7
|
+
return argv;
|
|
8
|
+
return ['browser', SESSION_FLAG, session, ...argv.slice(2)];
|
|
9
|
+
}
|
|
@@ -16,7 +16,7 @@ export type DshCompatibility = {
|
|
|
16
16
|
readonly requirements: readonly DshRequirement[];
|
|
17
17
|
} | {
|
|
18
18
|
readonly verdict: 'unknown';
|
|
19
|
-
readonly reason: 'no-requirement' | 'missing-dsh-version' | 'malformed';
|
|
19
|
+
readonly reason: 'no-requirement' | 'peer-only' | 'missing-dsh-version' | 'malformed';
|
|
20
20
|
readonly requirements: readonly DshRequirement[];
|
|
21
21
|
};
|
|
22
22
|
export declare function dshRequirementsOf(manifest: DshManifestFields): readonly DshRequirement[];
|
|
@@ -23,13 +23,16 @@ export function dshCompatibilityOf(input) {
|
|
|
23
23
|
const requirements = input.requirements;
|
|
24
24
|
if (requirements.length === 0)
|
|
25
25
|
return { verdict: 'unknown', reason: 'no-requirement', requirements };
|
|
26
|
+
const engines = requirements.filter((item) => item.source === 'engines');
|
|
27
|
+
if (engines.length === 0)
|
|
28
|
+
return { verdict: 'unknown', reason: 'peer-only', requirements };
|
|
26
29
|
const version = input.dshVersion?.trim() ?? '';
|
|
27
30
|
if (version === '' || semver.valid(version) === null) {
|
|
28
31
|
return { verdict: 'unknown', reason: 'missing-dsh-version', requirements };
|
|
29
32
|
}
|
|
30
33
|
const unsatisfied = [];
|
|
31
34
|
let malformed = false;
|
|
32
|
-
for (const requirement of
|
|
35
|
+
for (const requirement of engines) {
|
|
33
36
|
const range = semver.validRange(requirement.range);
|
|
34
37
|
if (range === null) {
|
|
35
38
|
malformed = true;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HostOs } from '../../contract/protocol/platform.js';
|
|
2
|
+
export declare function frontAppCommand(platform: HostOs): string | undefined;
|
|
3
|
+
export declare function isBrowserAppName(app: string): boolean;
|
|
4
|
+
export declare function frontAppOf(stdout: string, platform: HostOs): string | undefined;
|
|
5
|
+
export declare function bringBackCommand(app: string, platform: HostOs): string | undefined;
|
|
6
|
+
export declare function activateAppCommand(app: string, platform: HostOs): string | undefined;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export function frontAppCommand(platform) {
|
|
2
|
+
if (platform === 'darwin')
|
|
3
|
+
return 'lsappinfo info -only bundleID $(lsappinfo front)';
|
|
4
|
+
if (platform === 'linux')
|
|
5
|
+
return 'xdotool getactivewindow';
|
|
6
|
+
if (platform === 'win32')
|
|
7
|
+
return WINDOWS_FRONT_APP;
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
const WINDOWS_FRONT_APP = [
|
|
11
|
+
"Add-Type -Namespace W -Name N -MemberDefinition '[DllImport(\"user32.dll\")] public static extern System.IntPtr GetForegroundWindow(); [DllImport(\"user32.dll\")] public static extern int GetWindowThreadProcessId(System.IntPtr h, out int p);'",
|
|
12
|
+
'$h=[W.N]::GetForegroundWindow()',
|
|
13
|
+
'$p=0',
|
|
14
|
+
'[void][W.N]::GetWindowThreadProcessId($h,[ref]$p)',
|
|
15
|
+
'$p',
|
|
16
|
+
].join('; ');
|
|
17
|
+
const BROWSER_APP = /chrome|chromium|edge|brave|vivaldi/i;
|
|
18
|
+
export function isBrowserAppName(app) {
|
|
19
|
+
return BROWSER_APP.test(app);
|
|
20
|
+
}
|
|
21
|
+
const BUNDLE_ID = /(?:bundleID|"CFBundleIdentifier")="([^"]+)"/;
|
|
22
|
+
export function frontAppOf(stdout, platform) {
|
|
23
|
+
const text = String(stdout ?? '');
|
|
24
|
+
if (platform === 'darwin')
|
|
25
|
+
return BUNDLE_ID.exec(text)?.[1]?.trim() || undefined;
|
|
26
|
+
const lines = text
|
|
27
|
+
.split('\n')
|
|
28
|
+
.map((line) => line.trim())
|
|
29
|
+
.filter((line) => line !== '');
|
|
30
|
+
const last = lines[lines.length - 1];
|
|
31
|
+
if (last === undefined)
|
|
32
|
+
return undefined;
|
|
33
|
+
const name = platform === 'win32' ? last : (last.split(/\s+/)[0] ?? '');
|
|
34
|
+
return name === '' ? undefined : name;
|
|
35
|
+
}
|
|
36
|
+
export function bringBackCommand(app, platform) {
|
|
37
|
+
const name = app.trim();
|
|
38
|
+
if (name === '')
|
|
39
|
+
return undefined;
|
|
40
|
+
if (platform === 'darwin')
|
|
41
|
+
return `open -b ${quoteFor(name, platform)}`;
|
|
42
|
+
if (platform === 'linux')
|
|
43
|
+
return `xdotool windowactivate ${quoteFor(name, platform)}`;
|
|
44
|
+
if (platform === 'win32')
|
|
45
|
+
return windowsBringBack(name);
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
export function activateAppCommand(app, platform) {
|
|
49
|
+
const name = app.trim();
|
|
50
|
+
if (name === '')
|
|
51
|
+
return undefined;
|
|
52
|
+
if (platform === 'darwin')
|
|
53
|
+
return `open -a ${quoteFor(name, platform)}`;
|
|
54
|
+
if (platform === 'win32')
|
|
55
|
+
return windowsBringBack(name);
|
|
56
|
+
if (platform === 'linux')
|
|
57
|
+
return `xdotool search --name ${quoteFor(name, platform)} windowactivate`;
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
function windowsBringBack(app) {
|
|
61
|
+
const pid = app.trim();
|
|
62
|
+
if (!/^\d+$/.test(pid))
|
|
63
|
+
return `if ((New-Object -ComObject WScript.Shell).AppActivate('${app.replaceAll("'", "''")}')) { exit 0 } else { exit 1 }`;
|
|
64
|
+
return `if ((New-Object -ComObject WScript.Shell).AppActivate(${pid})) { exit 0 } else { exit 1 }`;
|
|
65
|
+
}
|
|
66
|
+
function quoteFor(value, platform) {
|
|
67
|
+
if (platform === 'win32')
|
|
68
|
+
return `'${value.replaceAll("'", "''")}'`;
|
|
69
|
+
return `'${value.replaceAll("'", `'\\''`)}'`;
|
|
70
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { PluginPlanStep } from '../contract/plugin-source.js';
|
|
2
|
+
import type { ShellKind } from './shell-text.js';
|
|
3
|
+
export interface ShellInvocation {
|
|
4
|
+
readonly file: string;
|
|
5
|
+
readonly args: readonly string[];
|
|
6
|
+
}
|
|
7
|
+
export interface PowershellFileOptions {
|
|
8
|
+
readonly platform?: NodeJS.Platform;
|
|
9
|
+
readonly pathValue?: string | undefined;
|
|
10
|
+
readonly exists?: (file: string) => boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Windows 上从 PATH 里挑一个起得来的 PowerShell:优先 `pwsh`(PowerShell 7),机器上只有
|
|
14
|
+
* Windows PowerShell 5.1 的 `powershell.exe` 时退回它;两个都没有仍给 `pwsh`,让报错继续指向要装的那个。
|
|
15
|
+
* 非 win32 恒给 `pwsh`。
|
|
16
|
+
* @param options - 可注入的 `platform` / `pathValue` / `exists`,默认当前平台、`process.env.PATH`、`existsSync`。
|
|
17
|
+
* @returns 交给进程启动的文件名。
|
|
18
|
+
*/
|
|
19
|
+
export declare function powershellFileOf(options?: PowershellFileOptions): string;
|
|
20
|
+
export declare function callOperatorOf(kind: ShellKind, script: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* 按 shell 种类给出启动参数:pwsh 的 powershell 文件由 `powershellFileOf` 选(Windows 上没装 pwsh 就退回
|
|
23
|
+
* `powershell.exe`),`-NoProfile -NonInteractive -Command` 与 `&` 调用符两种 PowerShell 一致。
|
|
24
|
+
* @param kind - 目标 shell 种类。
|
|
25
|
+
* @param script - 要跑的脚本原文。
|
|
26
|
+
* @param options - 透给 `powershellFileOf` 的解析选项,默认按当前平台与 PATH 解析。
|
|
27
|
+
* @returns 可直接交给进程启动的文件名与参数。
|
|
28
|
+
*/
|
|
29
|
+
export declare function invocationOf(kind: ShellKind, script: string, options?: PowershellFileOptions): ShellInvocation;
|
|
30
|
+
export declare function quotedArgvLine(argv: readonly string[], platform?: NodeJS.Platform): string;
|
|
31
|
+
export declare function quotedLineOf(invocation: ShellInvocation, platform?: NodeJS.Platform): string;
|
|
32
|
+
/**
|
|
33
|
+
* 目录链接在两种 shell 下的落法:POSIX 是符号链接,Windows 是 junction(建它不需要开发者模式或管理员)。
|
|
34
|
+
* @param kind - 目标 shell 种类。
|
|
35
|
+
* @returns 交给 `node:fs` 的 `symlink` 类型。
|
|
36
|
+
*/
|
|
37
|
+
export declare function dirLinkTypeOf(kind: ShellKind): 'junction' | 'dir';
|
|
38
|
+
/**
|
|
39
|
+
* 把一个安装计划步骤渲染成人能读的一行;`run` 给原命令(带上它的环境变量与 pwsh 的 `&` 调用符),
|
|
40
|
+
* 文件操作按平台给等价写法。这一行只用于展示与事实记录:文件操作真跑在 `node:fs` 上,不经过 shell。
|
|
41
|
+
* @param step - 计划步骤。
|
|
42
|
+
* @param platform - 目标平台,默认当前 `process.platform`。
|
|
43
|
+
* @returns 该步骤的一行文本。
|
|
44
|
+
*/
|
|
45
|
+
export declare function planStepText(step: PluginPlanStep, platform?: NodeJS.Platform): string;
|
|
46
|
+
/**
|
|
47
|
+
* 把整个安装计划渲染成一行(步骤之间按目标 shell 的串联符连起来)。
|
|
48
|
+
* @param steps - 计划步骤。
|
|
49
|
+
* @param platform - 目标平台,默认当前 `process.platform`。
|
|
50
|
+
* @returns 计划的一行文本。
|
|
51
|
+
*/
|
|
52
|
+
export declare function planLineOf(steps: readonly PluginPlanStep[], platform?: NodeJS.Platform): string;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { win32 } from 'node:path';
|
|
3
|
+
import { quoteShellArgFor, shellKindOf } from './shell-text.js';
|
|
4
|
+
const QUOTED_HEAD = /^\s*['"]/;
|
|
5
|
+
const PLAN_SEPARATOR = { posix: ' && ', pwsh: '; ' };
|
|
6
|
+
const POWERSHELL_CANDIDATES = [
|
|
7
|
+
['pwsh.exe', 'pwsh'],
|
|
8
|
+
['powershell.exe', 'powershell.exe'],
|
|
9
|
+
];
|
|
10
|
+
/**
|
|
11
|
+
* Windows 上从 PATH 里挑一个起得来的 PowerShell:优先 `pwsh`(PowerShell 7),机器上只有
|
|
12
|
+
* Windows PowerShell 5.1 的 `powershell.exe` 时退回它;两个都没有仍给 `pwsh`,让报错继续指向要装的那个。
|
|
13
|
+
* 非 win32 恒给 `pwsh`。
|
|
14
|
+
* @param options - 可注入的 `platform` / `pathValue` / `exists`,默认当前平台、`process.env.PATH`、`existsSync`。
|
|
15
|
+
* @returns 交给进程启动的文件名。
|
|
16
|
+
*/
|
|
17
|
+
export function powershellFileOf(options = {}) {
|
|
18
|
+
const platform = options.platform ?? process.platform;
|
|
19
|
+
if (platform !== 'win32')
|
|
20
|
+
return 'pwsh';
|
|
21
|
+
const pathValue = options.pathValue ?? process.env['PATH'] ?? '';
|
|
22
|
+
const exists = options.exists ?? existsSync;
|
|
23
|
+
const dirs = pathValue
|
|
24
|
+
.split(win32.delimiter)
|
|
25
|
+
.map((dir) => dir.replace(/^"(.*)"$/u, '$1'))
|
|
26
|
+
.filter((dir) => dir !== '');
|
|
27
|
+
for (const [exe, file] of POWERSHELL_CANDIDATES) {
|
|
28
|
+
if (dirs.some((dir) => exists(win32.join(dir, exe))))
|
|
29
|
+
return file;
|
|
30
|
+
}
|
|
31
|
+
return 'pwsh';
|
|
32
|
+
}
|
|
33
|
+
export function callOperatorOf(kind, script) {
|
|
34
|
+
if (kind !== 'pwsh' || !QUOTED_HEAD.test(script))
|
|
35
|
+
return script;
|
|
36
|
+
return `& ${script}`;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 按 shell 种类给出启动参数:pwsh 的 powershell 文件由 `powershellFileOf` 选(Windows 上没装 pwsh 就退回
|
|
40
|
+
* `powershell.exe`),`-NoProfile -NonInteractive -Command` 与 `&` 调用符两种 PowerShell 一致。
|
|
41
|
+
* @param kind - 目标 shell 种类。
|
|
42
|
+
* @param script - 要跑的脚本原文。
|
|
43
|
+
* @param options - 透给 `powershellFileOf` 的解析选项,默认按当前平台与 PATH 解析。
|
|
44
|
+
* @returns 可直接交给进程启动的文件名与参数。
|
|
45
|
+
*/
|
|
46
|
+
export function invocationOf(kind, script, options = {}) {
|
|
47
|
+
return kind === 'pwsh'
|
|
48
|
+
? { file: powershellFileOf(options), args: ['-NoProfile', '-NonInteractive', '-Command', callOperatorOf('pwsh', script)] }
|
|
49
|
+
: { file: 'bash', args: ['-c', script] };
|
|
50
|
+
}
|
|
51
|
+
export function quotedArgvLine(argv, platform = process.platform) {
|
|
52
|
+
return argv.map((one) => quoteShellArgFor(one, platform)).join(' ');
|
|
53
|
+
}
|
|
54
|
+
export function quotedLineOf(invocation, platform = process.platform) {
|
|
55
|
+
return quotedArgvLine([invocation.file, ...invocation.args], platform);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 目录链接在两种 shell 下的落法:POSIX 是符号链接,Windows 是 junction(建它不需要开发者模式或管理员)。
|
|
59
|
+
* @param kind - 目标 shell 种类。
|
|
60
|
+
* @returns 交给 `node:fs` 的 `symlink` 类型。
|
|
61
|
+
*/
|
|
62
|
+
export function dirLinkTypeOf(kind) {
|
|
63
|
+
return kind === 'pwsh' ? 'junction' : 'dir';
|
|
64
|
+
}
|
|
65
|
+
function displayEnvOf(env, platform) {
|
|
66
|
+
if (env === undefined)
|
|
67
|
+
return '';
|
|
68
|
+
const entries = Object.entries(env);
|
|
69
|
+
if (entries.length === 0)
|
|
70
|
+
return '';
|
|
71
|
+
const quote = (value) => quoteShellArgFor(value, platform);
|
|
72
|
+
return shellKindOf(platform) === 'pwsh'
|
|
73
|
+
? entries.map(([name, value]) => `$env:${name}=${quote(value)}; `).join('')
|
|
74
|
+
: entries.map(([name, value]) => `${name}=${quote(value)} `).join('');
|
|
75
|
+
}
|
|
76
|
+
function displayRunOf(step, platform) {
|
|
77
|
+
return `${displayEnvOf(step.env, platform)}${callOperatorOf(shellKindOf(platform), step.line)}`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 把一个安装计划步骤渲染成人能读的一行;`run` 给原命令(带上它的环境变量与 pwsh 的 `&` 调用符),
|
|
81
|
+
* 文件操作按平台给等价写法。这一行只用于展示与事实记录:文件操作真跑在 `node:fs` 上,不经过 shell。
|
|
82
|
+
* @param step - 计划步骤。
|
|
83
|
+
* @param platform - 目标平台,默认当前 `process.platform`。
|
|
84
|
+
* @returns 该步骤的一行文本。
|
|
85
|
+
*/
|
|
86
|
+
export function planStepText(step, platform = process.platform) {
|
|
87
|
+
const quote = (value) => quoteShellArgFor(value, platform);
|
|
88
|
+
if (step.kind === 'run')
|
|
89
|
+
return displayRunOf(step, platform);
|
|
90
|
+
if (step.kind === 'remove') {
|
|
91
|
+
return platform === 'win32'
|
|
92
|
+
? `Remove-Item -Recurse -Force ${quote(step.path)}`
|
|
93
|
+
: `rm -rf ${quote(step.path)}`;
|
|
94
|
+
}
|
|
95
|
+
if (step.kind === 'mkdir') {
|
|
96
|
+
return platform === 'win32' ? `New-Item -ItemType Directory -Force ${quote(step.path)}` : `mkdir -p ${quote(step.path)}`;
|
|
97
|
+
}
|
|
98
|
+
if (step.kind === 'link') {
|
|
99
|
+
return platform === 'win32'
|
|
100
|
+
? `New-Item -ItemType Junction -Path ${quote(step.path)} -Target ${quote(step.target)}`
|
|
101
|
+
: `ln -s ${quote(step.target)} ${quote(step.path)}`;
|
|
102
|
+
}
|
|
103
|
+
return platform === 'win32'
|
|
104
|
+
? `Copy-Item -Recurse -Force ${quote(step.from)} ${quote(step.to)}`
|
|
105
|
+
: `cp -R ${quote(step.from)}/. ${quote(step.to)}/`;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 把整个安装计划渲染成一行(步骤之间按目标 shell 的串联符连起来)。
|
|
109
|
+
* @param steps - 计划步骤。
|
|
110
|
+
* @param platform - 目标平台,默认当前 `process.platform`。
|
|
111
|
+
* @returns 计划的一行文本。
|
|
112
|
+
*/
|
|
113
|
+
export function planLineOf(steps, platform = process.platform) {
|
|
114
|
+
return steps
|
|
115
|
+
.map((step) => planStepText(step, platform))
|
|
116
|
+
.join(PLAN_SEPARATOR[shellKindOf(platform)]);
|
|
117
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 一台机器上跑命令的那种 shell:POSIX 系(sh / bash / zsh),或 dsh 在 Windows 上用的 pwsh。
|
|
3
|
+
*/
|
|
4
|
+
export type ShellKind = 'posix' | 'pwsh';
|
|
5
|
+
/**
|
|
6
|
+
* 按目标平台判 shell 种类:win32 是 pwsh(dsh 的 Windows 执行器就是 PowerShell),其余是 POSIX。
|
|
7
|
+
* @param platform - 目标平台,默认当前 `process.platform`。
|
|
8
|
+
* @returns `'pwsh'` 或 `'posix'`。
|
|
9
|
+
*/
|
|
10
|
+
export declare function shellKindOf(platform?: NodeJS.Platform): ShellKind;
|
|
11
|
+
/**
|
|
12
|
+
* 按目标平台把一个值裹成 shell 字面量:POSIX 用单引号并按 `'\''` 逃逸,pwsh 也用单引号但按 `''` 逃逸。
|
|
13
|
+
* @param value - 任意值;不是字符串的先 `String()`。
|
|
14
|
+
* @param platform - 目标平台,默认当前 `process.platform`。
|
|
15
|
+
* @returns 可安全拼进该平台 shell 命令的一段文本;含空格 / 元字符的值不会被当成语法。
|
|
16
|
+
*/
|
|
17
|
+
export declare function quoteShellArgFor(value: unknown, platform?: NodeJS.Platform): string;
|
|
18
|
+
/**
|
|
19
|
+
* 按当前平台把值裹成 shell 字面量;跨平台代码要固定平台时用 `quoteShellArgFor`。
|
|
20
|
+
* @param value - 任意值;不是字符串的先 `String()`。
|
|
21
|
+
* @returns 可安全拼进当前平台 shell 命令的一段文本。
|
|
22
|
+
*/
|
|
23
|
+
export declare function quoteShellArg(value: unknown): string;
|
|
24
|
+
/**
|
|
25
|
+
* 取一段文本的末尾若干行(如命令输出),顺带去掉首尾空白。
|
|
26
|
+
* @param text - 原文;`null` / `undefined` 当空串。
|
|
27
|
+
* @param lines - 最多留几行,默认 6;0 给全篇、负数从开头砍掉那么多行(都是 `slice` 语义),调用方传正数。
|
|
28
|
+
* @returns 末尾若干行、去首尾空行后的文本;原文行数不够就全给,空输入给空串。
|
|
29
|
+
*/
|
|
30
|
+
export declare function tailLines(text: string | null | undefined, lines?: number): string;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 按目标平台判 shell 种类:win32 是 pwsh(dsh 的 Windows 执行器就是 PowerShell),其余是 POSIX。
|
|
3
|
+
* @param platform - 目标平台,默认当前 `process.platform`。
|
|
4
|
+
* @returns `'pwsh'` 或 `'posix'`。
|
|
5
|
+
*/
|
|
6
|
+
export function shellKindOf(platform = process.platform) {
|
|
7
|
+
return platform === 'win32' ? 'pwsh' : 'posix';
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 按目标平台把一个值裹成 shell 字面量:POSIX 用单引号并按 `'\''` 逃逸,pwsh 也用单引号但按 `''` 逃逸。
|
|
11
|
+
* @param value - 任意值;不是字符串的先 `String()`。
|
|
12
|
+
* @param platform - 目标平台,默认当前 `process.platform`。
|
|
13
|
+
* @returns 可安全拼进该平台 shell 命令的一段文本;含空格 / 元字符的值不会被当成语法。
|
|
14
|
+
*/
|
|
15
|
+
export function quoteShellArgFor(value, platform = process.platform) {
|
|
16
|
+
const text = String(value);
|
|
17
|
+
if (shellKindOf(platform) === 'pwsh')
|
|
18
|
+
return `'${text.replace(/'/g, "''")}'`;
|
|
19
|
+
return `'${text.replace(/'/g, `'\\''`)}'`;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 按当前平台把值裹成 shell 字面量;跨平台代码要固定平台时用 `quoteShellArgFor`。
|
|
23
|
+
* @param value - 任意值;不是字符串的先 `String()`。
|
|
24
|
+
* @returns 可安全拼进当前平台 shell 命令的一段文本。
|
|
25
|
+
*/
|
|
26
|
+
export function quoteShellArg(value) {
|
|
27
|
+
return quoteShellArgFor(value, process.platform);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 取一段文本的末尾若干行(如命令输出),顺带去掉首尾空白。
|
|
31
|
+
* @param text - 原文;`null` / `undefined` 当空串。
|
|
32
|
+
* @param lines - 最多留几行,默认 6;0 给全篇、负数从开头砍掉那么多行(都是 `slice` 语义),调用方传正数。
|
|
33
|
+
* @returns 末尾若干行、去首尾空行后的文本;原文行数不够就全给,空输入给空串。
|
|
34
|
+
*/
|
|
35
|
+
export function tailLines(text, lines = 6) {
|
|
36
|
+
const all = String(text ?? '')
|
|
37
|
+
.trim()
|
|
38
|
+
.split('\n');
|
|
39
|
+
return all.slice(-lines).join('\n');
|
|
40
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { DshShell, DshShellSandboxPolicy } from '../api/dsh.js';
|
|
2
|
+
export type { DshShellSandboxPolicy };
|
|
3
|
+
export interface ShellSandboxFacts {
|
|
4
|
+
readonly mode?: string | undefined;
|
|
5
|
+
readonly denied?: boolean | undefined;
|
|
6
|
+
readonly enforcement?: string | undefined;
|
|
7
|
+
readonly runnerFailed?: boolean | undefined;
|
|
8
|
+
}
|
|
9
|
+
export interface ShellResult {
|
|
10
|
+
readonly exitCode: number | null;
|
|
11
|
+
readonly timedOut: boolean;
|
|
12
|
+
readonly aborted: boolean;
|
|
13
|
+
readonly stdout: string;
|
|
14
|
+
readonly stderr: string;
|
|
15
|
+
readonly truncated: boolean;
|
|
16
|
+
readonly stalled: boolean;
|
|
17
|
+
readonly sandbox?: ShellSandboxFacts | undefined;
|
|
18
|
+
}
|
|
19
|
+
export declare function runShell(shell: DshShell, command: string, options?: {
|
|
20
|
+
workdir?: string;
|
|
21
|
+
timeoutMs?: number;
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
env?: Readonly<Record<string, string>>;
|
|
24
|
+
sandboxPolicy?: DshShellSandboxPolicy | undefined;
|
|
25
|
+
}): Promise<ShellResult>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { GIT_TIMEOUT_MS } from '../config/constants.js';
|
|
2
|
+
const DEFAULT_MAX_BYTES = 8 * 1024 * 1024;
|
|
3
|
+
const GUARD_SLACK_MS = 1_000;
|
|
4
|
+
function stalledResult() {
|
|
5
|
+
return {
|
|
6
|
+
exitCode: null,
|
|
7
|
+
timedOut: true,
|
|
8
|
+
aborted: false,
|
|
9
|
+
stdout: '',
|
|
10
|
+
stderr: '',
|
|
11
|
+
truncated: false,
|
|
12
|
+
stalled: true,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
async function settleWithin(work, limitMs) {
|
|
16
|
+
let timer;
|
|
17
|
+
const guard = new Promise((resolve) => {
|
|
18
|
+
timer = setTimeout(() => resolve(null), limitMs);
|
|
19
|
+
});
|
|
20
|
+
try {
|
|
21
|
+
return await Promise.race([work, guard]);
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
if (timer !== undefined)
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export async function runShell(shell, command, options = {}) {
|
|
29
|
+
const { workdir, timeoutMs = GIT_TIMEOUT_MS, signal, env, sandboxPolicy } = options;
|
|
30
|
+
const spec = shell.resolve({
|
|
31
|
+
command,
|
|
32
|
+
...(workdir === undefined ? {} : { workdir }),
|
|
33
|
+
timeoutMs,
|
|
34
|
+
onExpiry: 'kill',
|
|
35
|
+
stdoutMaxBytes: DEFAULT_MAX_BYTES,
|
|
36
|
+
...(signal === undefined ? {} : { signal }),
|
|
37
|
+
...(env === undefined ? {} : { env }),
|
|
38
|
+
...(sandboxPolicy === undefined ? {} : { sandboxPolicy }),
|
|
39
|
+
});
|
|
40
|
+
const settled = await settleWithin((async () => {
|
|
41
|
+
const execution = await shell.execute(spec);
|
|
42
|
+
const result = await execution.result();
|
|
43
|
+
return {
|
|
44
|
+
exitCode: result.exitCode,
|
|
45
|
+
timedOut: result.timedOut === true,
|
|
46
|
+
aborted: result.aborted === true,
|
|
47
|
+
stdout: result.stdout.text,
|
|
48
|
+
stderr: result.stderr.text,
|
|
49
|
+
truncated: result.stdout.truncated || result.stderr.truncated,
|
|
50
|
+
stalled: false,
|
|
51
|
+
...(result.sandbox === undefined ? {} : { sandbox: result.sandbox }),
|
|
52
|
+
};
|
|
53
|
+
})(), timeoutMs + GUARD_SLACK_MS);
|
|
54
|
+
return settled ?? stalledResult();
|
|
55
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { DataErrorCode, RequestPlan, RequestRecipe, ResolvedRequestPlan, ResolvedRequestRecipe } from '../contract/data.js';
|
|
2
|
+
import type { SiteEnvelope, UnitSite } from '../contract/unit.js';
|
|
3
|
+
export type UnitSites = Readonly<Record<string, UnitSite>>;
|
|
4
|
+
export interface SiteProblem {
|
|
5
|
+
readonly code: DataErrorCode;
|
|
6
|
+
readonly message: string;
|
|
7
|
+
readonly pageCode: string;
|
|
8
|
+
}
|
|
9
|
+
export type SiteResolution<T> = {
|
|
10
|
+
readonly ok: true;
|
|
11
|
+
readonly value: T;
|
|
12
|
+
} | {
|
|
13
|
+
readonly ok: false;
|
|
14
|
+
readonly problem: SiteProblem;
|
|
15
|
+
};
|
|
16
|
+
export interface PlanEnvelopeOptions {
|
|
17
|
+
readonly envelope?: SiteEnvelope | false | undefined;
|
|
18
|
+
}
|
|
19
|
+
export type EnvelopePath = {
|
|
20
|
+
readonly ok: true;
|
|
21
|
+
readonly path: string | undefined;
|
|
22
|
+
} | {
|
|
23
|
+
readonly ok: false;
|
|
24
|
+
readonly problem: string;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* 把一条只写 `site`(或同时写 `origin`)的配方解析成带 `origin` 的直白形状。
|
|
28
|
+
* 显式字段优先于站点默认值;`origin` 显式写时只覆盖站点的 origin。
|
|
29
|
+
*/
|
|
30
|
+
export declare function resolveRecipeOf(recipe: RequestRecipe, sites: UnitSites | undefined): SiteResolution<ResolvedRequestRecipe>;
|
|
31
|
+
/**
|
|
32
|
+
* 把一份只写 `site`(或同时写 `origin`)的读取计划解析成带 `origin` 的直白形状。
|
|
33
|
+
* 显式字段优先于站点默认值。
|
|
34
|
+
*/
|
|
35
|
+
export declare function resolvePlanOf(plan: RequestPlan, sites: UnitSites | undefined): SiteResolution<ResolvedRequestPlan>;
|
|
36
|
+
/**
|
|
37
|
+
* 把相对信封 `dataPath` 的路径展开成绝对路径;`envelope: false` 表示从根取、不展开。
|
|
38
|
+
* 多段 `dataPath`(如 `data.list`)不猜语义,要求调用方写全。
|
|
39
|
+
*/
|
|
40
|
+
export declare function envelopePathOf(path: string | undefined, envelope: SiteEnvelope | false | undefined): EnvelopePath;
|
|
41
|
+
/**
|
|
42
|
+
* 手写计划的信封展开:吃一份计划与站点,返回 `response.path` / `while.path` 已展开的计划。
|
|
43
|
+
* 运行时不认识信封;要用这份 helper 在代码里显式展开。
|
|
44
|
+
*/
|
|
45
|
+
export declare function expandPlanPathsOf(plan: RequestPlan, site: UnitSite, options?: PlanEnvelopeOptions): RequestPlan;
|