@dsh-workbench/shared 0.3.11 → 1.0.0-beta.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/LICENSE +21 -0
- package/README.md +97 -0
- package/dist/api/blocker-table.d.ts +3 -3
- package/dist/api/blocker-table.js +47 -23
- package/dist/api/dsh.d.ts +1 -1
- package/dist/api/workbench-client.js +2 -1
- package/dist/components/Activity.css +0 -1
- package/dist/components/ApprovalDetail.css +1 -2
- package/dist/components/ApprovalDetail.d.ts +1 -1
- package/dist/components/ApprovalPlan.css +0 -1
- package/dist/components/Card.css +0 -1
- package/dist/components/Card.d.ts +1 -1
- package/dist/components/CardBoundary.d.ts +1 -1
- package/dist/components/CardHead.css +0 -1
- package/dist/components/CardHead.d.ts +1 -1
- package/dist/components/ChoiceGroup.css +1 -2
- package/dist/components/ConfirmBar.d.ts +1 -1
- package/dist/components/ConfirmBar.js +1 -1
- package/dist/components/Form.css +1 -1
- package/dist/components/FormActions.js +1 -1
- package/dist/components/LongTaskRow.css +1 -2
- package/dist/components/LongTaskRow.d.ts +1 -1
- package/dist/components/LongTaskRow.js +1 -1
- package/dist/components/Note.css +0 -1
- package/dist/components/ResourceStates.css +0 -1
- package/dist/components/SettingsSection.css +0 -1
- package/dist/components/Spinner.css +0 -1
- package/dist/components/Stack.css +0 -1
- package/dist/components/StateBlock.css +0 -1
- package/dist/components/board-module.d.ts +3 -3
- package/dist/components/icons.d.ts +1 -1
- package/dist/components/icons.js +11 -4
- package/dist/config/constants.d.ts +2 -2
- package/dist/config/constants.js +2 -2
- package/dist/contract/adapters.d.ts +10 -0
- package/dist/contract/cli.d.ts +3 -0
- package/dist/contract/data.d.ts +126 -0
- package/dist/{api → contract}/facts.d.ts +12 -3
- package/dist/{api → contract}/facts.js +19 -1
- package/dist/contract/needs.d.ts +132 -0
- package/dist/contract/needs.js +232 -0
- package/dist/contract/plugin-source.d.ts +1 -0
- package/dist/contract/protocol/bundle-install.d.ts +136 -0
- package/dist/contract/protocol/bundle-install.js +12 -0
- package/dist/contract/protocol/config-sync.d.ts +55 -0
- package/dist/contract/protocol/config-sync.js +2 -0
- package/dist/contract/protocol/diy.d.ts +4 -0
- package/dist/contract/protocol/diy.js +4 -0
- package/dist/contract/protocol/dsh-plugins.d.ts +11 -0
- package/dist/contract/protocol/dsh-plugins.js +1 -0
- package/dist/contract/protocol/long-tasks.d.ts +13 -0
- package/dist/contract/protocol/long-tasks.js +1 -0
- package/dist/contract/protocol/market-index.d.ts +195 -0
- package/dist/contract/protocol/market-index.js +7 -0
- package/dist/contract/protocol/platform.d.ts +44 -0
- package/dist/contract/protocol/platform.js +1 -0
- package/dist/contract/protocol/preflight-wake.d.ts +7 -0
- package/dist/contract/protocol/preflight-wake.js +1 -0
- package/dist/contract/protocol/profile-create.d.ts +30 -0
- package/dist/contract/protocol/profile-create.js +2 -0
- package/dist/contract/protocol/sidebar-edition.d.ts +14 -0
- package/dist/contract/protocol/sidebar-edition.js +2 -0
- package/dist/contract/protocol/unit-manifests.d.ts +6 -0
- package/dist/contract/protocol/unit-manifests.js +1 -0
- package/dist/contract/unit-board.js +1 -0
- package/dist/contract/unit-capability.d.ts +34 -0
- package/dist/contract/unit-capability.js +1 -0
- package/dist/{host → contract}/unit-declaration.d.ts +7 -5
- package/dist/{api → contract}/unit-declaration.js +19 -13
- package/dist/contract/unit.d.ts +57 -0
- package/dist/contract/unit.js +24 -0
- package/dist/contract/workbench-contract.d.ts +15 -0
- package/dist/contract/workbench-contract.js +1 -0
- package/dist/host/adapter-set.d.ts +26 -0
- package/dist/host/adapter-set.js +243 -0
- package/dist/host/adapter-types.d.ts +79 -0
- package/dist/host/adapter-types.js +23 -0
- package/dist/host/adapters.d.ts +22 -0
- package/dist/host/adapters.js +9 -0
- package/dist/host/browser.d.ts +3 -0
- package/dist/host/browser.js +3 -0
- package/dist/host/changelog.d.ts +21 -0
- package/dist/host/changelog.js +188 -0
- package/dist/host/channel-plugin.d.ts +15 -0
- package/dist/host/channel-plugin.js +42 -0
- package/dist/host/cli.d.ts +4 -0
- package/dist/host/cli.js +7 -0
- package/dist/host/command-name.d.ts +1 -0
- package/dist/host/command-name.js +14 -0
- package/dist/host/data-error.d.ts +13 -0
- package/dist/host/data-error.js +20 -0
- package/dist/host/data-root.js +3 -0
- package/dist/host/diy-debug.js +2 -1
- package/dist/host/diy-record.js +2 -1
- package/dist/host/facts.d.ts +1 -1
- package/dist/host/facts.js +3 -2
- package/dist/host/git.js +2 -2
- package/dist/host/http.js +1 -1
- package/dist/host/lib-capabilities.d.ts +22 -0
- package/dist/host/lib-capabilities.js +37 -0
- package/dist/host/lib-install.d.ts +9 -0
- package/dist/host/lib-install.js +10 -0
- package/dist/host/long-task.d.ts +1 -1
- package/dist/host/long-task.js +4 -3
- package/dist/host/market-index.d.ts +22 -0
- package/dist/host/market-index.js +420 -0
- package/dist/host/needs.d.ts +4 -0
- package/dist/host/needs.js +2 -0
- package/dist/host/patch.d.ts +9 -0
- package/dist/host/patch.js +78 -0
- package/dist/host/paths.d.ts +12 -0
- package/dist/host/paths.js +28 -0
- package/dist/host/platform.d.ts +14 -0
- package/dist/host/platform.js +40 -0
- package/dist/host/plugin-source.d.ts +8 -6
- package/dist/host/plugin-source.js +43 -33
- package/dist/host/plugin.d.ts +4 -0
- package/dist/host/plugin.js +39 -0
- package/dist/host/port-types.d.ts +191 -0
- package/dist/host/port-types.js +1 -0
- package/dist/host/preflight.d.ts +28 -0
- package/dist/host/preflight.js +1 -0
- package/dist/host/profile-port.d.ts +4 -0
- package/dist/host/profile-port.js +4 -0
- package/dist/host/provision.d.ts +2 -2
- package/dist/host/provision.js +5 -3
- package/dist/host/recipe-rules.d.ts +5 -0
- package/dist/host/recipe-rules.js +382 -0
- package/dist/host/recipe.d.ts +14 -0
- package/dist/host/recipe.js +283 -0
- package/dist/host/shell-text.d.ts +9 -2
- package/dist/host/shell-text.js +15 -3
- package/dist/host/shell.d.ts +2 -0
- package/dist/host/shell.js +7 -0
- package/dist/host/spec.d.ts +8 -0
- package/dist/host/spec.js +24 -0
- package/dist/host/supply-index.d.ts +34 -0
- package/dist/host/supply-index.js +202 -0
- package/dist/host/supply-port.d.ts +5 -0
- package/dist/host/supply-port.js +39 -0
- package/dist/host/time.d.ts +12 -0
- package/dist/host/time.js +19 -0
- package/dist/host/tokens.d.ts +26 -0
- package/dist/host/tokens.js +154 -0
- package/dist/host/tool-kit.js +1 -1
- package/dist/host/unit-face.d.ts +5 -4
- package/dist/host/version-range.d.ts +2 -0
- package/dist/host/version-range.js +73 -0
- package/dist/host/version.d.ts +3 -0
- package/dist/host/version.js +29 -0
- package/dist/host.d.ts +47 -16
- package/dist/host.js +47 -16
- package/dist/i18n/en.js +224 -71
- package/dist/i18n/index.d.ts +298 -73
- package/dist/i18n/merge.js +1 -1
- package/dist/i18n/need-text.d.ts +26 -0
- package/dist/i18n/need-text.js +26 -0
- package/dist/i18n/settings.d.ts +70 -0
- package/dist/i18n/settings.js +136 -0
- package/dist/i18n/zh.d.ts +297 -72
- package/dist/i18n/zh.js +226 -73
- package/dist/index.d.ts +23 -7
- package/dist/index.js +23 -7
- package/dist/{api → lib}/dsh-client.d.ts +11 -1
- package/dist/lib/dsh-client.js +1 -0
- package/dist/lib/market-index.d.ts +7 -0
- package/dist/lib/market-index.js +25 -0
- package/dist/lib/react.d.ts +0 -40
- package/dist/lib/react.js +0 -36
- package/dist/lib/ui.js +3 -9
- package/dist/testing/data.d.ts +13 -0
- package/dist/testing/data.js +46 -0
- package/dist/testing/declaration.d.ts +1 -1
- package/dist/testing/declaration.js +1 -1
- package/dist/testing/host-fakes.d.ts +0 -13
- package/dist/testing/host-fakes.js +0 -24
- package/dist/testing/needs.d.ts +16 -0
- package/dist/testing/needs.js +46 -0
- package/dist/testing.d.ts +2 -0
- package/dist/testing.js +2 -0
- package/package.json +18 -2
- package/src/api/__tests__/blocker-table.test.ts +135 -86
- package/src/api/blocker-table.ts +52 -26
- package/src/api/dsh.ts +1 -1
- package/src/api/host-write.ts +0 -1
- package/src/api/workbench-client.test.ts +2 -2
- package/src/api/workbench-client.ts +2 -1
- package/src/components/Activity.css +0 -1
- package/src/components/ApprovalDetail.css +1 -2
- package/src/components/ApprovalDetail.tsx +1 -1
- package/src/components/ApprovalPlan.css +0 -1
- package/src/components/Card.css +0 -1
- package/src/components/Card.tsx +1 -1
- package/src/components/CardAnchor.test.tsx +1 -1
- package/src/components/CardBoundary.tsx +1 -1
- package/src/components/CardHead.css +0 -1
- package/src/components/CardHead.tsx +1 -1
- package/src/components/ChoiceGroup.css +1 -2
- package/src/components/ConfirmBar.tsx +1 -1
- package/src/components/Form.css +1 -1
- package/src/components/Form.tsx +1 -1
- package/src/components/FormActions.tsx +1 -1
- package/src/components/LongTaskRow.css +1 -2
- package/src/components/LongTaskRow.tsx +1 -1
- package/src/components/Note.css +0 -1
- package/src/components/ResourceStates.css +0 -1
- package/src/components/SelectFieldNodes.tsx +0 -1
- package/src/components/SettingsSection.css +0 -1
- package/src/components/Spinner.css +0 -1
- package/src/components/Stack.css +0 -1
- package/src/components/StateBlock.css +0 -1
- package/src/components/__tests__/long-task-row.test.ts +1 -1
- package/src/components/board-module.ts +3 -3
- package/src/components/icons.tsx +16 -8
- package/src/config/constants.ts +2 -2
- package/src/{api → contract}/__tests__/facts.test.ts +73 -25
- package/src/{api → contract}/__tests__/long-task.test.ts +3 -3
- package/src/contract/__tests__/needs.test.ts +74 -0
- package/src/{host → contract}/__tests__/unit-declaration.test.ts +1 -1
- package/src/contract/__tests__/unit.test.ts +43 -0
- package/src/contract/adapters.ts +9 -0
- package/src/contract/cli.ts +3 -0
- package/src/contract/data.ts +145 -0
- package/src/{api → contract}/facts.ts +34 -4
- package/src/contract/needs.ts +331 -0
- package/src/contract/plugin-source.ts +1 -0
- package/src/contract/protocol/bundle-install.ts +198 -0
- package/src/contract/protocol/config-sync.ts +65 -0
- package/src/contract/protocol/diy.ts +4 -0
- package/src/contract/protocol/dsh-plugins.ts +13 -0
- package/src/contract/protocol/long-tasks.ts +16 -0
- package/src/contract/protocol/market-index.ts +246 -0
- package/src/contract/protocol/platform.ts +47 -0
- package/src/contract/protocol/preflight-wake.ts +17 -0
- package/src/contract/protocol/profile-create.ts +36 -0
- package/src/contract/protocol/sidebar-edition.ts +19 -0
- package/src/contract/protocol/unit-manifests.ts +8 -0
- package/src/{api → contract}/unit-board.ts +0 -1
- package/src/contract/unit-capability.ts +37 -0
- package/src/{host → contract}/unit-declaration.ts +22 -18
- package/src/contract/unit.ts +76 -0
- package/src/contract/workbench-contract.ts +30 -0
- package/src/host/__tests__/adapter-set.test.ts +228 -0
- package/src/host/__tests__/adapters.test.ts +27 -0
- package/src/host/__tests__/changelog.test.ts +102 -0
- package/src/host/__tests__/command-name.test.ts +16 -0
- package/src/host/__tests__/diy-record.test.ts +3 -3
- package/src/host/__tests__/facts.test.ts +5 -5
- package/src/host/__tests__/long-task.test.ts +1 -1
- package/src/host/__tests__/market-index.test.ts +179 -0
- package/src/host/__tests__/plugin-ledger.test.ts +10 -10
- package/src/host/__tests__/provision.test.ts +7 -6
- package/src/host/__tests__/shell-text.test.ts +16 -1
- package/src/host/__tests__/supply-index.test.ts +137 -0
- package/src/host/__tests__/tokens.test.ts +110 -0
- package/src/host/__tests__/version-range.test.ts +21 -0
- package/src/host/adapter-set.ts +298 -0
- package/src/host/adapter-types.ts +105 -0
- package/src/host/adapters.ts +44 -0
- package/src/host/browser.ts +3 -0
- package/src/host/changelog.ts +212 -0
- package/src/host/channel-plugin.ts +55 -0
- package/src/host/cli.ts +11 -0
- package/src/host/command-name.ts +15 -0
- package/src/host/data-error.ts +21 -0
- package/src/host/data-root.ts +2 -0
- package/src/host/diy-debug.ts +2 -1
- package/src/host/diy-record.ts +2 -1
- package/src/host/facts.ts +4 -3
- package/src/host/git.ts +2 -2
- package/src/host/http.ts +1 -1
- package/src/host/lib-capabilities.ts +52 -0
- package/src/host/lib-install.ts +18 -0
- package/src/host/long-task.ts +4 -3
- package/src/host/market-index.ts +413 -0
- package/src/host/needs.ts +6 -0
- package/src/host/patch.ts +93 -0
- package/src/host/paths.ts +37 -0
- package/src/host/platform.ts +48 -0
- package/src/host/plugin-source.ts +48 -56
- package/src/host/plugin.ts +39 -0
- package/src/host/port-types.ts +222 -0
- package/src/host/preflight.ts +31 -0
- package/src/host/profile-port.ts +9 -0
- package/src/host/provision.ts +7 -4
- package/src/host/recipe-rules.ts +330 -0
- package/src/host/recipe.test.ts +334 -0
- package/src/host/recipe.ts +323 -0
- package/src/host/shell-text.ts +15 -3
- package/src/host/shell.ts +9 -0
- package/src/host/spec.ts +32 -0
- package/src/host/supply-index.ts +256 -0
- package/src/host/supply-port.ts +45 -0
- package/src/host/time.ts +22 -0
- package/src/host/tokens.ts +186 -0
- package/src/host/tool-kit.ts +1 -1
- package/src/host/unit-face.ts +5 -4
- package/src/host/version-range.ts +71 -0
- package/src/host/version.ts +30 -0
- package/src/host.ts +47 -16
- package/src/i18n/en.ts +224 -71
- package/src/i18n/merge.test.ts +6 -6
- package/src/i18n/merge.ts +1 -1
- package/src/i18n/need-text.ts +27 -0
- package/src/i18n/settings.ts +141 -0
- package/src/i18n/zh.ts +227 -73
- package/src/index.ts +23 -7
- package/src/lib/blocker-action.ts +0 -1
- package/src/{api → lib}/dsh-client.ts +12 -1
- package/src/lib/market-index.ts +36 -0
- package/src/lib/react.ts +0 -40
- package/src/lib/text.test.ts +2 -2
- package/src/lib/ui.test.ts +6 -5
- package/src/lib/ui.ts +3 -9
- package/src/testing/__tests__/declaration.test.ts +1 -1
- package/src/testing/data.ts +66 -0
- package/src/testing/declaration.ts +2 -2
- package/src/testing/host-fakes.ts +0 -38
- package/src/testing/needs.ts +80 -0
- package/src/testing.ts +2 -0
- package/dist/api/unit-declaration.d.ts +0 -65
- package/dist/api/unit.d.ts +0 -85
- package/dist/api/unit.js +0 -8
- package/dist/api/workbench-contract.d.ts +0 -44
- package/dist/components/MockStrip.css +0 -39
- package/dist/components/MockStrip.d.ts +0 -15
- package/dist/components/MockStrip.js +0 -8
- package/dist/components/SaveBar.css +0 -7
- package/dist/host/browser-find.d.ts +0 -38
- package/dist/host/browser-find.js +0 -242
- package/dist/host/browser-read.d.ts +0 -28
- package/dist/host/browser-read.js +0 -243
- package/dist/host/opencli-runtime.d.ts +0 -25
- package/dist/host/opencli-runtime.js +0 -56
- package/dist/host/opencli-window.d.ts +0 -2
- package/dist/host/opencli-window.js +0 -8
- package/dist/host/opencli.d.ts +0 -37
- package/dist/host/opencli.js +0 -155
- package/dist/host/unit-declaration.js +0 -213
- package/dist/host/unit-libs.d.ts +0 -37
- package/dist/lib/diy-debug.d.ts +0 -80
- package/dist/lib/diy-debug.js +0 -88
- package/dist/lib/diy-record.d.ts +0 -135
- package/dist/lib/diy-record.js +0 -162
- package/dist/lib/lint-probe.d.ts +0 -1
- package/dist/lib/lint-probe.js +0 -6
- package/dist/lib/panel-state.d.ts +0 -120
- package/dist/lib/panel-state.js +0 -176
- package/src/api/__tests__/unit.test.ts +0 -38
- package/src/api/unit.ts +0 -93
- package/src/api/workbench-contract.ts +0 -59
- package/src/host/__tests__/browser-find.test.ts +0 -330
- package/src/host/__tests__/browser-read.test.ts +0 -228
- package/src/host/__tests__/opencli-runtime.test.ts +0 -46
- package/src/host/browser-find.ts +0 -311
- package/src/host/browser-read.ts +0 -311
- package/src/host/opencli-runtime.ts +0 -79
- package/src/host/opencli-window.ts +0 -11
- package/src/host/opencli.ts +0 -213
- package/src/host/unit-libs.ts +0 -41
- /package/dist/{api/blocker.js → contract/adapters.js} +0 -0
- /package/dist/{api → contract}/blocker.d.ts +0 -0
- /package/dist/{api/dsh-client.js → contract/blocker.js} +0 -0
- /package/dist/{api/unit-board.js → contract/cli.js} +0 -0
- /package/dist/{api/workbench-contract.js → contract/data.js} +0 -0
- /package/dist/{api → contract}/long-task.d.ts +0 -0
- /package/dist/{api → contract}/long-task.js +0 -0
- /package/dist/{host/unit-libs.js → contract/plugin-source.js} +0 -0
- /package/dist/{api → contract}/unit-board.d.ts +0 -0
- /package/src/{api → contract}/blocker.ts +0 -0
- /package/src/{api → contract}/long-task.ts +0 -0
|
@@ -8,6 +8,7 @@ describe('provision 请求体:每类动作各有各的目标字段', () => {
|
|
|
8
8
|
['update', 'unit'],
|
|
9
9
|
['uninstall', 'unit'],
|
|
10
10
|
['install-bundle', 'spec'],
|
|
11
|
+
['install-version', 'spec'],
|
|
11
12
|
['update-bundle', 'unit'],
|
|
12
13
|
['remove-bundle', 'name'],
|
|
13
14
|
['cancel-install', 'requestId'],
|
|
@@ -23,9 +24,9 @@ describe('provision 请求体:每类动作各有各的目标字段', () => {
|
|
|
23
24
|
ok: true,
|
|
24
25
|
request: { action: 'install', unit: 'issues' },
|
|
25
26
|
});
|
|
26
|
-
expect(parseProvisionBody({ action: 'update-dependency', id: 'plugin:
|
|
27
|
+
expect(parseProvisionBody({ action: 'update-dependency', id: 'plugin:demo' })).toEqual({
|
|
27
28
|
ok: true,
|
|
28
|
-
request: { action: 'update-dependency', id: 'plugin:
|
|
29
|
+
request: { action: 'update-dependency', id: 'plugin:demo' },
|
|
29
30
|
});
|
|
30
31
|
});
|
|
31
32
|
|
|
@@ -59,13 +60,13 @@ describe('provision 请求体:每类动作各有各的目标字段', () => {
|
|
|
59
60
|
});
|
|
60
61
|
|
|
61
62
|
it('用户装的那两笔插件动作按包名认目标(凭台账记的那个包名)', () => {
|
|
62
|
-
expect(parseProvisionBody({ action: 'update-plugin', name: ' opencli-plugin-
|
|
63
|
+
expect(parseProvisionBody({ action: 'update-plugin', name: ' opencli-plugin-inventory ' })).toEqual({
|
|
63
64
|
ok: true,
|
|
64
|
-
request: { action: 'update-plugin', name: 'opencli-plugin-
|
|
65
|
+
request: { action: 'update-plugin', name: 'opencli-plugin-inventory' },
|
|
65
66
|
});
|
|
66
|
-
expect(parseProvisionBody({ action: 'uninstall-plugin', name: 'opencli-plugin-
|
|
67
|
+
expect(parseProvisionBody({ action: 'uninstall-plugin', name: 'opencli-plugin-inventory' })).toEqual({
|
|
67
68
|
ok: true,
|
|
68
|
-
request: { action: 'uninstall-plugin', name: 'opencli-plugin-
|
|
69
|
+
request: { action: 'uninstall-plugin', name: 'opencli-plugin-inventory' },
|
|
69
70
|
});
|
|
70
71
|
expect(parseProvisionBody({ action: 'uninstall-plugin', unit: 'panels' })).toEqual({
|
|
71
72
|
ok: false,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { execFileSync } from 'node:child_process';
|
|
2
2
|
import { describe, expect, it } from 'vitest';
|
|
3
|
-
import { quoteShellArg, tailLines } from '../shell-text.js';
|
|
3
|
+
import { quoteShellArg, quoteShellArgFor, tailLines } from '../shell-text.js';
|
|
4
4
|
|
|
5
5
|
function shellSees(value: string): string {
|
|
6
6
|
return execFileSync('/bin/sh', ['-c', `printf '%s' ${quoteShellArg(value)}`], {
|
|
@@ -42,3 +42,18 @@ describe('tailLines', () => {
|
|
|
42
42
|
expect(tailLines(' ', 3)).toBe('');
|
|
43
43
|
});
|
|
44
44
|
});
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
describe('quoteShellArgFor', () => {
|
|
48
|
+
it('win32 用双引号,路径里的空格原样保留', () => {
|
|
49
|
+
expect(quoteShellArgFor('C:\\Program Files\\dsh\\x.cjs', 'win32')).toBe('"C:\\Program Files\\dsh\\x.cjs"');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('win32 里值自身的双引号被转义', () => {
|
|
53
|
+
expect(quoteShellArgFor('a"b', 'win32')).toBe('"a\\"b"');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('POSIX 仍用单引号', () => {
|
|
57
|
+
expect(quoteShellArgFor("it's", 'darwin')).toBe("'it'\\''s'");
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import type { ExtensionNeed, LibNeed, SupplySource } from '../../contract/needs.js';
|
|
3
|
+
import { buildSupplyIndex, type SupplyDeclaration } from '../supply-index.js';
|
|
4
|
+
|
|
5
|
+
function npm(pkg?: string, registryEnv = 'DSH_WB_REGISTRY'): SupplySource {
|
|
6
|
+
return {
|
|
7
|
+
kind: 'npm',
|
|
8
|
+
...(pkg === undefined ? {} : { package: pkg }),
|
|
9
|
+
registryEnv,
|
|
10
|
+
registryDefault: 'https://registry.invalid/',
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function extension(name: string, sources?: readonly SupplySource[]): ExtensionNeed {
|
|
15
|
+
return {
|
|
16
|
+
kind: 'extension',
|
|
17
|
+
runtime: 'opencli',
|
|
18
|
+
name,
|
|
19
|
+
commands: [`${name}-read`],
|
|
20
|
+
purpose: '测试',
|
|
21
|
+
...(sources === undefined ? {} : { sources }),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function lib(name: string, sources?: readonly SupplySource[]): LibNeed {
|
|
26
|
+
return { kind: 'lib', name, purpose: '测试', ...(sources === undefined ? {} : { sources }) };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function manifest(id: string, needs: SupplyDeclaration['needs']): SupplyDeclaration {
|
|
30
|
+
return { id, needs };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe('供给索引:一个包只有一个来源', () => {
|
|
34
|
+
it('★★ 同一个插件、同一份源:收成一个条目,两家都记上', () => {
|
|
35
|
+
const index = buildSupplyIndex([
|
|
36
|
+
manifest('a', [extension('coding', [npm('opencli-plugin-coding')])]),
|
|
37
|
+
manifest('b', [extension('coding', [npm('opencli-plugin-coding')])]),
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
expect(index.conflicts).toEqual([]);
|
|
41
|
+
expect(index.entries.size).toBe(1);
|
|
42
|
+
expect(index.entryOf('extension', 'coding')?.declarers).toEqual(['a', 'b']);
|
|
43
|
+
expect(index.sourcesOf('extension', 'coding')).toEqual([npm('opencli-plugin-coding')]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('★★ 一家写了源、一家没写:判冲突,不 last-wins', () => {
|
|
47
|
+
const index = buildSupplyIndex([
|
|
48
|
+
manifest('a', [extension('coding', [npm('opencli-plugin-coding')])]),
|
|
49
|
+
manifest('b', [extension('coding')]),
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
expect(index.conflicts.map((item) => item.kind)).toEqual(['source-mismatch']);
|
|
53
|
+
expect(index.entryOf('extension', 'coding')?.conflicted).toBe(true);
|
|
54
|
+
expect(index.sourcesOf('extension', 'coding')).toEqual([]);
|
|
55
|
+
expect(index.problems[0]).toContain('插件');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('★★ 同一个插件名来自两个包:判 name-mismatch', () => {
|
|
59
|
+
const index = buildSupplyIndex([
|
|
60
|
+
manifest('a', [extension('coding', [npm('opencli-plugin-coding')])]),
|
|
61
|
+
manifest('b', [extension('coding', [{ kind: 'git', url: 'github:acme/plugin-coding' }])]),
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
expect(index.conflicts.map((item) => item.kind)).toContain('name-mismatch');
|
|
65
|
+
expect(index.entryOf('extension', 'coding')?.conflicted).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('★★ 同一个包、同一个 registryEnv,一家只缺 registryDefault:不算冲突,缺的用有的一方补上', () => {
|
|
69
|
+
const index = buildSupplyIndex([
|
|
70
|
+
manifest('a', [
|
|
71
|
+
extension('coding', [{ kind: 'npm', package: 'opencli-plugin-coding', registryEnv: 'DSH_WB_REGISTRY' }]),
|
|
72
|
+
]),
|
|
73
|
+
manifest('b', [extension('coding', [npm('opencli-plugin-coding')])]),
|
|
74
|
+
]);
|
|
75
|
+
|
|
76
|
+
expect(index.conflicts).toEqual([]);
|
|
77
|
+
expect(index.sourcesOf('extension', 'coding')).toEqual([npm('opencli-plugin-coding')]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('★★ 共用库两家用不同的 registryEnv:判冲突,即使地址相同', () => {
|
|
81
|
+
const index = buildSupplyIndex([
|
|
82
|
+
manifest('argocd', [lib('opencli-workflow', [npm(undefined, 'DSH_WORKBENCH_ARGOCD_REGISTRY')])]),
|
|
83
|
+
manifest('cmdb', [lib('opencli-workflow', [npm(undefined, 'DSH_WORKBENCH_CMDB_REGISTRY')])]),
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
expect(index.conflicts).toHaveLength(1);
|
|
87
|
+
expect(index.conflicts[0]?.kind).toBe('source-mismatch');
|
|
88
|
+
expect(index.entryOf('lib', 'opencli-workflow')?.conflicted).toBe(true);
|
|
89
|
+
expect(index.sourcesOf('lib', 'opencli-workflow')).toEqual([]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('★★ 三家三个源:同一个包只报一条冲突,declarers 合并齐', () => {
|
|
93
|
+
const index = buildSupplyIndex([
|
|
94
|
+
manifest('a', [lib('opencli-workflow', [npm(undefined, 'DSH_WB_A_REGISTRY')])]),
|
|
95
|
+
manifest('b', [lib('opencli-workflow', [npm(undefined, 'DSH_WB_B_REGISTRY')])]),
|
|
96
|
+
manifest('c', [lib('opencli-workflow', [npm(undefined, 'DSH_WB_C_REGISTRY')])]),
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
expect(index.conflicts).toHaveLength(1);
|
|
100
|
+
expect(index.problems).toHaveLength(1);
|
|
101
|
+
expect(index.conflicts[0]?.declarers).toEqual(['a', 'b', 'c']);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('★ 跨 kind 同名不算冲突:bin:foo / extension:foo / lib:foo 是三件事', () => {
|
|
105
|
+
const index = buildSupplyIndex([
|
|
106
|
+
manifest('a', [
|
|
107
|
+
{ kind: 'bin', name: 'workflow', purpose: '测试' },
|
|
108
|
+
extension('workflow', [npm('opencli-plugin-workflow')]),
|
|
109
|
+
lib('workflow', [npm(undefined, 'DSH_WB_LIB_REGISTRY')]),
|
|
110
|
+
]),
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
expect(index.conflicts).toEqual([]);
|
|
114
|
+
expect(index.entries.size).toBe(2);
|
|
115
|
+
expect(index.entryOf('extension', 'workflow')?.package).toBe('opencli-plugin-workflow');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('没写 sources 的 extension 按约定落包名,进索引但不代装', () => {
|
|
119
|
+
const index = buildSupplyIndex([manifest('a', [extension('coding')])]);
|
|
120
|
+
|
|
121
|
+
expect(index.entryOf('extension', 'coding')?.package).toBe('opencli-plugin-coding');
|
|
122
|
+
expect(index.sourcesOf('extension', 'coding')).toEqual([]);
|
|
123
|
+
expect(index.conflicts).toEqual([]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('bin / env 不进供给索引(它们没有来源)', () => {
|
|
127
|
+
const index = buildSupplyIndex([
|
|
128
|
+
manifest('a', [
|
|
129
|
+
{ kind: 'bin', name: 'gitops', purpose: '测试' },
|
|
130
|
+
{ kind: 'env', name: 'DSH_TOKEN', purpose: '测试' },
|
|
131
|
+
]),
|
|
132
|
+
]);
|
|
133
|
+
|
|
134
|
+
expect(index.entries.size).toBe(0);
|
|
135
|
+
expect(index.conflicts).toEqual([]);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import {
|
|
6
|
+
DSH_THEME_PACKAGE,
|
|
7
|
+
SHELL_PACKAGE,
|
|
8
|
+
nearestTokens,
|
|
9
|
+
readTokens,
|
|
10
|
+
tokenEntriesIn,
|
|
11
|
+
tokenFamilyOf,
|
|
12
|
+
} from '../tokens.js';
|
|
13
|
+
|
|
14
|
+
const THEME_CSS = [
|
|
15
|
+
':root{--dsw-static-neutral-bluish-00:#fff;--dsw-font-family:system-ui;',
|
|
16
|
+
'--dsw-font-s-14:14px/22px var(--dsw-font-family);',
|
|
17
|
+
'--dsw-alias-bg-layer-1:var(--dsw-static-neutral-bluish-00)}',
|
|
18
|
+
'body[data-ds-dark-theme]{--dsw-static-neutral-bluish-00:#111;',
|
|
19
|
+
'--dsw-alias-bg-layer-1:var(--dsw-static-neutral-bluish-00)}',
|
|
20
|
+
].join('');
|
|
21
|
+
|
|
22
|
+
const SHELL_CSS = [
|
|
23
|
+
':root{--wb-len-16:16px;--wb-gap-xs:6px}',
|
|
24
|
+
'.card{color:var(--dsw-alias-bg-layer-1);padding:var(--wb-len-16)}',
|
|
25
|
+
].join('');
|
|
26
|
+
|
|
27
|
+
function fakeTree(): string {
|
|
28
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-tokens-'));
|
|
29
|
+
writeFileSync(join(root, 'package.json'), JSON.stringify({ name: 'fixture', private: true }));
|
|
30
|
+
|
|
31
|
+
const theme = join(root, 'node_modules', ...DSH_THEME_PACKAGE.split('/'));
|
|
32
|
+
mkdirSync(join(theme, 'lib'), { recursive: true });
|
|
33
|
+
writeFileSync(join(theme, 'package.json'), JSON.stringify({ name: DSH_THEME_PACKAGE, version: '9.9.9' }));
|
|
34
|
+
writeFileSync(join(theme, 'lib', 'client.js'), `const css = ${JSON.stringify(THEME_CSS)};`);
|
|
35
|
+
|
|
36
|
+
const shell = join(root, 'node_modules', ...SHELL_PACKAGE.split('/'));
|
|
37
|
+
mkdirSync(shell, { recursive: true });
|
|
38
|
+
writeFileSync(join(shell, 'package.json'), JSON.stringify({ name: SHELL_PACKAGE, version: '1.2.3' }));
|
|
39
|
+
writeFileSync(join(shell, 'client.js'), `const css = ${JSON.stringify(SHELL_CSS)};`);
|
|
40
|
+
return root;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('tokenEntriesIn', () => {
|
|
44
|
+
it('只认前缀,浅色与深色分开取,同一个方案里只留第一处', () => {
|
|
45
|
+
const entries = tokenEntriesIn(THEME_CSS, '--dsw-');
|
|
46
|
+
expect(entries.map((one) => one.name)).toEqual([
|
|
47
|
+
'--dsw-alias-bg-layer-1',
|
|
48
|
+
'--dsw-font-family',
|
|
49
|
+
'--dsw-font-s-14',
|
|
50
|
+
'--dsw-static-neutral-bluish-00',
|
|
51
|
+
]);
|
|
52
|
+
const layer = entries.find((one) => one.name === '--dsw-alias-bg-layer-1');
|
|
53
|
+
expect(layer?.values).toEqual({ light: '#fff', dark: '#111' });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('值里的 var() 按同一个方案解析到底,解析不了的原样留着', () => {
|
|
57
|
+
const entries = tokenEntriesIn(`${THEME_CSS}:root{--dsw-radius-md:12px;--dsw-shadow-lv1:0 0 2px var(--dsw-missing)}`, '--dsw-');
|
|
58
|
+
const font = entries.find((one) => one.name === '--dsw-font-s-14');
|
|
59
|
+
expect(font?.values.light).toBe('14px/22px system-ui');
|
|
60
|
+
const shadow = entries.find((one) => one.name === '--dsw-shadow-lv1');
|
|
61
|
+
expect(shadow?.values.light).toBe('0 0 2px var(--dsw-missing)');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('没有声明就给空数组,不把别的令牌带进来', () => {
|
|
65
|
+
expect(tokenEntriesIn('.x{color:red}', '--dsw-')).toEqual([]);
|
|
66
|
+
expect(tokenEntriesIn('', '--dsw-')).toEqual([]);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('readTokens', () => {
|
|
71
|
+
it('按起始目录解析出装着的主题与外壳,各带版本、文件与值', () => {
|
|
72
|
+
const root = fakeTree();
|
|
73
|
+
const set = readTokens([root]);
|
|
74
|
+
expect(set.dsw?.package).toBe(DSH_THEME_PACKAGE);
|
|
75
|
+
expect(set.dsw?.version).toBe('9.9.9');
|
|
76
|
+
expect(set.dsw?.file.endsWith(join('lib', 'client.js'))).toBe(true);
|
|
77
|
+
expect(set.wb?.version).toBe('1.2.3');
|
|
78
|
+
expect(set.wb?.entries.map((one) => one.name)).toEqual(['--wb-gap-xs', '--wb-len-16']);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('一个都解析不到就给两个空位,不抛', () => {
|
|
82
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-tokens-empty-'));
|
|
83
|
+
expect(readTokens([root])).toEqual({ dsw: null, wb: null });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('同一个起始目录上,前面那个包没装就接着往后找', () => {
|
|
87
|
+
const themeOnly = fakeTree();
|
|
88
|
+
const shellOnly = fakeTree();
|
|
89
|
+
const set = readTokens([themeOnly, shellOnly]);
|
|
90
|
+
expect(set.dsw?.version).toBe('9.9.9');
|
|
91
|
+
expect(set.wb?.version).toBe('1.2.3');
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('nearestTokens', () => {
|
|
96
|
+
it('拼错两个字母以内给最近的几个,差太远的不要', () => {
|
|
97
|
+
const names = ['--dsw-alias-bg-layer-1', '--dsw-alias-bg-layer-2', '--dsw-alias-label-primary'];
|
|
98
|
+
expect(nearestTokens('--dsw-alias-bg-layr-1', names)).toEqual(['--dsw-alias-bg-layer-1', '--dsw-alias-bg-layer-2']);
|
|
99
|
+
expect(nearestTokens('--dsw-alias-bg-layer-9', names)).toEqual(['--dsw-alias-bg-layer-1', '--dsw-alias-bg-layer-2']);
|
|
100
|
+
expect(nearestTokens('--complete-other-thing', names)).toEqual([]);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('tokenFamilyOf', () => {
|
|
105
|
+
it('取前缀后面那一段,用来分桶', () => {
|
|
106
|
+
expect(tokenFamilyOf('--dsw-alias-bg-layer-1')).toBe('alias');
|
|
107
|
+
expect(tokenFamilyOf('--wb-len-16')).toBe('len');
|
|
108
|
+
expect(tokenFamilyOf('--dsw-radius-md')).toBe('radius');
|
|
109
|
+
});
|
|
110
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { checkVersionRange } from '../version-range.js';
|
|
3
|
+
|
|
4
|
+
describe('checkVersionRange', () => {
|
|
5
|
+
it('空契约与空版本按满足算', () => {
|
|
6
|
+
expect(checkVersionRange(undefined, '1.2.3')).toBe('satisfied');
|
|
7
|
+
expect(checkVersionRange(' ', '0.0.1')).toBe('satisfied');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('caret 收下上界以内的版本', () => {
|
|
11
|
+
expect(checkVersionRange('^0.3.0', '0.3.11')).toBe('satisfied');
|
|
12
|
+
expect(checkVersionRange('^0.3.0', '0.4.0')).toBe('unsatisfied');
|
|
13
|
+
expect(checkVersionRange('^0.1.0', '0.3.11')).toBe('unsatisfied');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('比较符与畸形契约分得开', () => {
|
|
17
|
+
expect(checkVersionRange('>=0.3.0 <0.4.0', '0.3.11')).toBe('satisfied');
|
|
18
|
+
expect(checkVersionRange('>=0.3.0 <0.4.0', '0.4.0')).toBe('unsatisfied');
|
|
19
|
+
expect(checkVersionRange('nonsense', '0.3.11')).toBe('malformed');
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import type { DshHostContext } from '../api/dsh.js';
|
|
6
|
+
import {
|
|
7
|
+
ADAPTER_SET_API_VERSION,
|
|
8
|
+
SUPPORTED_ADAPTER_API_VERSIONS,
|
|
9
|
+
type AdapterSet,
|
|
10
|
+
type CoreAdapterSet,
|
|
11
|
+
} from './adapters.js';
|
|
12
|
+
import { isPackageName } from './spec.js';
|
|
13
|
+
|
|
14
|
+
export interface WorkbenchConfig {
|
|
15
|
+
readonly adapterSet?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type AdapterSetFailure =
|
|
19
|
+
| 'not-configured'
|
|
20
|
+
| 'no-profile'
|
|
21
|
+
| 'malformed'
|
|
22
|
+
| 'not-resolvable'
|
|
23
|
+
| 'import-failed'
|
|
24
|
+
| 'bad-export'
|
|
25
|
+
| 'factory-failed'
|
|
26
|
+
| 'incompatible-version'
|
|
27
|
+
| 'missing-member';
|
|
28
|
+
|
|
29
|
+
export type AdapterSetLoad =
|
|
30
|
+
| { readonly ok: true; readonly spec: string; readonly set: CoreAdapterSet }
|
|
31
|
+
| {
|
|
32
|
+
readonly ok: false;
|
|
33
|
+
readonly spec?: string;
|
|
34
|
+
readonly failure: AdapterSetFailure;
|
|
35
|
+
readonly detail: string;
|
|
36
|
+
readonly cause?: unknown;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const CORE_MEMBERS = ['data', 'supply', 'profile', 'probe'] as const;
|
|
40
|
+
|
|
41
|
+
const CORE_METHODS = [
|
|
42
|
+
['data', 'read'],
|
|
43
|
+
['data', 'request'],
|
|
44
|
+
['data', 'requestMany'],
|
|
45
|
+
['data', 'submit'],
|
|
46
|
+
['supply', 'resolvePluginSource'],
|
|
47
|
+
['supply', 'libInstallCommand'],
|
|
48
|
+
['supply', 'pluginInstallPlan'],
|
|
49
|
+
['supply', 'latestVersion'],
|
|
50
|
+
['supply', 'missingReleaseParts'],
|
|
51
|
+
['supply', 'probeRelease'],
|
|
52
|
+
['supply', 'splitNpmSpec'],
|
|
53
|
+
['profile', 'dirOf'],
|
|
54
|
+
['profile', 'dshPlugins'],
|
|
55
|
+
['profile', 'selfUpdateView'],
|
|
56
|
+
['probe', 'probeUnits'],
|
|
57
|
+
['probe', 'registerPreflight'],
|
|
58
|
+
['probe', 'declaredPluginCommands'],
|
|
59
|
+
] as const;
|
|
60
|
+
|
|
61
|
+
const FULL_MEMBERS = ['modules', 'runtime', 'plugins', 'actions'] as const;
|
|
62
|
+
|
|
63
|
+
const FULL_METHODS = [
|
|
64
|
+
['modules', 'isolatedHome'],
|
|
65
|
+
['modules', 'cliCatalogPath'],
|
|
66
|
+
['modules', 'vendorPluginsDir'],
|
|
67
|
+
['modules', 'opencliInstallCommand'],
|
|
68
|
+
['modules', 'profileDirOf'],
|
|
69
|
+
['modules', 'profileModules'],
|
|
70
|
+
['modules', 'readInstalledModule'],
|
|
71
|
+
['modules', 'readProfilePackage'],
|
|
72
|
+
['modules', 'readBundleManager'],
|
|
73
|
+
['modules', 'createRegistryNotebook'],
|
|
74
|
+
['modules', 'declarationGaps'],
|
|
75
|
+
['modules', 'moduleDeclarationOf'],
|
|
76
|
+
['modules', 'registryDeclarationOf'],
|
|
77
|
+
['modules', 'pathDeclarationOf'],
|
|
78
|
+
['modules', 'declarationRefusalOf'],
|
|
79
|
+
['modules', 'refusalReason'],
|
|
80
|
+
['modules', 'packageRouteOf'],
|
|
81
|
+
['modules', 'isNewerVersion'],
|
|
82
|
+
['modules', 'startSelfUpdate'],
|
|
83
|
+
['runtime', 'packageRoot'],
|
|
84
|
+
['runtime', 'opencliCommand'],
|
|
85
|
+
['runtime', 'seed'],
|
|
86
|
+
['runtime', 'warnUnwritableRoot'],
|
|
87
|
+
['plugins', 'declarationLookup'],
|
|
88
|
+
['plugins', 'createUnits'],
|
|
89
|
+
['actions', 'configSyncHandler'],
|
|
90
|
+
['actions', 'profileCreateHandler'],
|
|
91
|
+
['actions', 'configSyncAction'],
|
|
92
|
+
['actions', 'profileCreateAction'],
|
|
93
|
+
] as const;
|
|
94
|
+
|
|
95
|
+
function methodOf(value: Record<string, unknown>, member: string, method: string): unknown {
|
|
96
|
+
const part = value[member];
|
|
97
|
+
if (typeof part !== 'object' || part === null) return undefined;
|
|
98
|
+
return (part as Record<string, unknown>)[method];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function failure(
|
|
102
|
+
failureKind: AdapterSetFailure,
|
|
103
|
+
detail: string,
|
|
104
|
+
spec?: string,
|
|
105
|
+
cause?: unknown,
|
|
106
|
+
): AdapterSetLoad {
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
...(spec === undefined ? {} : { spec }),
|
|
110
|
+
failure: failureKind,
|
|
111
|
+
detail,
|
|
112
|
+
...(cause === undefined ? {} : { cause }),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function profileDirOf(ctx: DshHostContext): string | null {
|
|
117
|
+
const profile = ctx.get<{ dir?: unknown }>('profileContext');
|
|
118
|
+
const dir = typeof profile?.dir === 'string' ? profile.dir.trim() : '';
|
|
119
|
+
return dir === '' ? null : dir;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function packageDirOf(spec: string, from: string): string | undefined {
|
|
123
|
+
let dir = from;
|
|
124
|
+
for (;;) {
|
|
125
|
+
const candidate = join(dir, 'node_modules', spec);
|
|
126
|
+
if (existsSync(join(candidate, 'package.json'))) return candidate;
|
|
127
|
+
const up = dirname(dir);
|
|
128
|
+
if (up === dir) return undefined;
|
|
129
|
+
dir = up;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function entryOf(value: unknown): string | undefined {
|
|
134
|
+
if (typeof value === 'string') return value;
|
|
135
|
+
if (typeof value !== 'object' || value === null) return undefined;
|
|
136
|
+
const record = value as Record<string, unknown>;
|
|
137
|
+
for (const key of ['import', 'node', 'module', 'default', 'require']) {
|
|
138
|
+
const found = entryOf(record[key]);
|
|
139
|
+
if (found !== undefined) return found;
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function exportsEntryOf(value: unknown): string | undefined {
|
|
145
|
+
if (typeof value !== 'object' || value === null) return entryOf(value);
|
|
146
|
+
const record = value as Record<string, unknown>;
|
|
147
|
+
return '.' in record ? entryOf(record['.']) : entryOf(record);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function esmEntryUrl(spec: string, profileDir: string): string | undefined {
|
|
151
|
+
const dir = packageDirOf(spec, profileDir);
|
|
152
|
+
if (dir === undefined) return undefined;
|
|
153
|
+
let pkg: Record<string, unknown>;
|
|
154
|
+
try {
|
|
155
|
+
pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')) as Record<string, unknown>;
|
|
156
|
+
} catch {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
const main = typeof pkg['main'] === 'string' ? pkg['main'] : undefined;
|
|
160
|
+
const module = typeof pkg['module'] === 'string' ? pkg['module'] : undefined;
|
|
161
|
+
const entry = exportsEntryOf(pkg['exports']) ?? module ?? main ?? 'index.js';
|
|
162
|
+
const file = join(dir, entry);
|
|
163
|
+
return existsSync(file) ? pathToFileURL(file).href : undefined;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function resolveFromProfile(spec: string, profileDir: string): string | undefined {
|
|
167
|
+
const esm = esmEntryUrl(spec, profileDir);
|
|
168
|
+
if (esm !== undefined) return esm;
|
|
169
|
+
const parent = pathToFileURL(join(profileDir, 'package.json')).href;
|
|
170
|
+
try {
|
|
171
|
+
return pathToFileURL(createRequire(parent).resolve(spec)).href;
|
|
172
|
+
} catch {
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function versionOf(value: Record<string, unknown>): number | undefined {
|
|
178
|
+
const version = value['apiVersion'];
|
|
179
|
+
return typeof version === 'number' && Number.isInteger(version) ? version : undefined;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function validateAdapterSet(value: unknown, spec: string): AdapterSetLoad {
|
|
183
|
+
if (typeof value !== 'object' || value === null) {
|
|
184
|
+
return failure('missing-member', `${spec} 的 createAdapterSet(ctx) 没回一个对象`, spec);
|
|
185
|
+
}
|
|
186
|
+
const record = value as Record<string, unknown>;
|
|
187
|
+
const version = versionOf(record);
|
|
188
|
+
if (version === undefined || !SUPPORTED_ADAPTER_API_VERSIONS.includes(version)) {
|
|
189
|
+
return failure(
|
|
190
|
+
'incompatible-version',
|
|
191
|
+
`${spec} 的适配器协议版本是 ${String(record['apiVersion'])},工作台只认 v${ADAPTER_SET_API_VERSION}`,
|
|
192
|
+
spec,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
const missing: string[] = CORE_MEMBERS.filter(
|
|
196
|
+
(member) => record[member] === undefined || record[member] === null,
|
|
197
|
+
);
|
|
198
|
+
for (const [member, method] of CORE_METHODS) {
|
|
199
|
+
if (typeof methodOf(record, member, method) !== 'function') missing.push(`${member}.${method}`);
|
|
200
|
+
}
|
|
201
|
+
if (typeof record['id'] !== 'string' || record['id'].trim() === '') missing.push('id');
|
|
202
|
+
if (typeof record['version'] !== 'string' || record['version'].trim() === '') missing.push('version');
|
|
203
|
+
if (FULL_MEMBERS.some((member) => record[member] !== undefined)) {
|
|
204
|
+
for (const member of FULL_MEMBERS) {
|
|
205
|
+
if (typeof record[member] !== 'object' || record[member] === null) missing.push(member);
|
|
206
|
+
}
|
|
207
|
+
for (const [member, method] of FULL_METHODS) {
|
|
208
|
+
if (typeof methodOf(record, member, method) !== 'function') missing.push(`${member}.${method}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (missing.length > 0) {
|
|
212
|
+
return failure('missing-member', `${spec} 少了 ${missing.join(' / ')}`, spec);
|
|
213
|
+
}
|
|
214
|
+
return { ok: true, spec, set: record as unknown as CoreAdapterSet };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export async function loadAdapterSet(
|
|
218
|
+
ctx: DshHostContext,
|
|
219
|
+
spec: string | undefined,
|
|
220
|
+
): Promise<AdapterSetLoad> {
|
|
221
|
+
const wanted = typeof spec === 'string' ? spec.trim() : spec === undefined ? '' : String(spec).trim();
|
|
222
|
+
if (wanted === '') return failure('not-configured', '没有配置 adapterSet');
|
|
223
|
+
const profileDir = profileDirOf(ctx);
|
|
224
|
+
if (profileDir === null) return failure('no-profile', '找不到 profile 目录,无法从 profile 解析适配器集合', wanted);
|
|
225
|
+
if (!isPackageName(wanted)) {
|
|
226
|
+
return failure('malformed', `适配器集合要写包名(如 @acme/adapters-set),收到 ${JSON.stringify(wanted)}`, wanted);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const url = resolveFromProfile(wanted, profileDir);
|
|
230
|
+
if (url === undefined) {
|
|
231
|
+
return failure('not-resolvable', `在 profile 里解析不到 ${wanted},先把它装进这个 profile`, wanted);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
let mod: Record<string, unknown>;
|
|
235
|
+
try {
|
|
236
|
+
mod = (await import(url)) as Record<string, unknown>;
|
|
237
|
+
} catch (error) {
|
|
238
|
+
return failure('import-failed', `${wanted} 的入口没加载成:${String(error)}`, wanted, error);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const factory = mod['createAdapterSet'] ?? mod['default'];
|
|
242
|
+
if (typeof factory !== 'function') {
|
|
243
|
+
return failure('bad-export', `${wanted} 没导出 createAdapterSet(ctx),导出键:${Object.keys(mod).join(', ')}`, wanted);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
let value: Record<string, unknown>;
|
|
247
|
+
try {
|
|
248
|
+
const raw: unknown = await factory(ctx);
|
|
249
|
+
if (typeof raw !== 'object' || raw === null) {
|
|
250
|
+
return failure('missing-member', `${wanted} 的 createAdapterSet(ctx) 没回一个对象`, wanted);
|
|
251
|
+
}
|
|
252
|
+
value = raw as Record<string, unknown>;
|
|
253
|
+
} catch (error) {
|
|
254
|
+
return failure('factory-failed', `${wanted} 的 createAdapterSet(ctx) 抛了:${String(error)}`, wanted, error);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return validateAdapterSet(value, wanted);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function withDefaultAdapterSet(defaults: AdapterSet, loaded: AdapterSetLoad): AdapterSet {
|
|
261
|
+
if (!loaded.ok) return defaults;
|
|
262
|
+
const set = loaded.set as Partial<AdapterSet>;
|
|
263
|
+
return {
|
|
264
|
+
apiVersion: set.apiVersion ?? defaults.apiVersion,
|
|
265
|
+
id: set.id ?? defaults.id,
|
|
266
|
+
version: set.version ?? defaults.version,
|
|
267
|
+
data: set.data ?? defaults.data,
|
|
268
|
+
supply: set.supply ?? defaults.supply,
|
|
269
|
+
modules: set.modules ?? defaults.modules,
|
|
270
|
+
profile: set.profile ?? defaults.profile,
|
|
271
|
+
probe: set.probe ?? defaults.probe,
|
|
272
|
+
runtime: set.runtime ?? defaults.runtime,
|
|
273
|
+
plugins: set.plugins ?? defaults.plugins,
|
|
274
|
+
actions: set.actions ?? defaults.actions,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function isFullAdapterSet(value: CoreAdapterSet): value is AdapterSet {
|
|
279
|
+
const candidate = value as unknown as Record<string, unknown>;
|
|
280
|
+
return ['modules', 'runtime', 'plugins', 'actions'].every(
|
|
281
|
+
(member) => typeof candidate[member] === 'object' && candidate[member] !== null,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export async function resolveAdapterSet(
|
|
286
|
+
ctx: DshHostContext,
|
|
287
|
+
spec: string | undefined,
|
|
288
|
+
makeDefaults: () => Promise<AdapterSet>,
|
|
289
|
+
): Promise<{ readonly set: AdapterSet; readonly load: AdapterSetLoad; readonly usedDefaults: boolean }> {
|
|
290
|
+
const load = await loadAdapterSet(ctx, spec);
|
|
291
|
+
if (load.ok && isFullAdapterSet(load.set)) {
|
|
292
|
+
return { set: load.set, load, usedDefaults: false };
|
|
293
|
+
}
|
|
294
|
+
const defaults = await makeDefaults();
|
|
295
|
+
return { set: withDefaultAdapterSet(defaults, load), load, usedDefaults: true };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export type { AdapterSet, CoreAdapterSet };
|