agentdock-cli 0.7.1 → 1.0.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.
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.runSyncCommand = runSyncCommand;
7
+ const promises_1 = __importDefault(require("node:fs/promises"));
8
+ const node_path_1 = __importDefault(require("node:path"));
9
+ const yaml_1 = __importDefault(require("yaml"));
10
+ const scan_1 = require("../core/scan");
11
+ const config_1 = require("../core/config");
12
+ const sync_1 = require("../core/sync");
13
+ const command_error_codes_1 = require("../constants/command-error-codes");
14
+ const command_json_1 = require("../utils/command-json");
15
+ const plan_render_1 = require("../utils/plan-render");
16
+ const fs_1 = require("../utils/fs");
17
+ const i18n_1 = require("../i18n");
18
+ /** 读取 sync.yaml(--config 指定或 ./sync.yaml),CLI flag 优先于文件。 */
19
+ async function loadSyncFile(configPath) {
20
+ const p = configPath ? node_path_1.default.resolve(configPath) : node_path_1.default.resolve('sync.yaml');
21
+ if (!(await (0, fs_1.pathExists)(p))) {
22
+ if (configPath) {
23
+ throw new Error(`SYNC_CONFIG_MISSING: ${p}`);
24
+ }
25
+ return {};
26
+ }
27
+ const parsed = yaml_1.default.parse(await promises_1.default.readFile(p, 'utf8'));
28
+ return parsed ?? {};
29
+ }
30
+ const PUSH_LABELS = {
31
+ create: 'CREATE ',
32
+ overwrite: 'OVERWRITE',
33
+ skip: 'SKIP ',
34
+ conflict: 'CONFLICT ',
35
+ stale: 'STALE ',
36
+ };
37
+ function renderPush(result) {
38
+ const lines = [
39
+ `sync push → ${result.target} (${result.applied ? (0, i18n_1.t)('sync.applied') : (0, i18n_1.t)('sync.dryRun')})`,
40
+ ];
41
+ for (const entry of result.entries) {
42
+ lines.push(` ${PUSH_LABELS[entry.action]} ${entry.path}`);
43
+ }
44
+ if (result.conflicts.length > 0) {
45
+ lines.push(` ${(0, i18n_1.t)('sync.conflictHint')}`);
46
+ }
47
+ if (result.stale.length > 0) {
48
+ lines.push(` ${(0, i18n_1.t)('sync.stale')}`);
49
+ }
50
+ if (result.backupPath) {
51
+ lines.push(` ${(0, i18n_1.t)('sync.backup', { path: result.backupPath })}`);
52
+ }
53
+ return lines;
54
+ }
55
+ function renderPull(result) {
56
+ const lines = [
57
+ `sync pull → ${result.plan.targetPath} (${result.applied ? (0, i18n_1.t)('sync.applied') : (0, i18n_1.t)('sync.dryRun')})`,
58
+ ...(0, plan_render_1.renderPlan)(result.plan, 'dry-run', ` bundle: ${result.target}`),
59
+ ];
60
+ if (result.plan.conflicts.length > 0) {
61
+ lines.push(` ${(0, i18n_1.t)('sync.conflictHint')}`);
62
+ }
63
+ if (result.envWritten) {
64
+ lines.push(` ${(0, i18n_1.t)('sync.envWritten')}`);
65
+ }
66
+ if (result.snapshotPath) {
67
+ lines.push(` ${(0, i18n_1.t)('sync.snapshot', { path: result.snapshotPath })}`);
68
+ }
69
+ return lines;
70
+ }
71
+ function renderStatus(result) {
72
+ const lines = [`sync status → ${result.target}`];
73
+ if (result.bundleCreatedAt) {
74
+ lines.push(` ${(0, i18n_1.t)('sync.bundleTime', { time: result.bundleCreatedAt })}`);
75
+ }
76
+ lines.push(` ${(0, i18n_1.t)('sync.statusPlan', result.plan)}`);
77
+ lines.push(` ${(0, i18n_1.t)('sync.statusLocalOnly', { count: result.localOnly.length })}`);
78
+ for (const id of result.localOnly) {
79
+ lines.push(` + ${id}`);
80
+ }
81
+ lines.push(` ${(0, i18n_1.t)('sync.statusRemoteOnly', { count: result.remoteOnly.length })}`);
82
+ for (const id of result.remoteOnly) {
83
+ lines.push(` - ${id}`);
84
+ }
85
+ return lines;
86
+ }
87
+ async function runSyncCommand(action, target, options = {}) {
88
+ const root = options.root ?? (0, scan_1.defaultScanRoot)();
89
+ try {
90
+ if (action === '--help' || action === '-h' || action === 'help') {
91
+ return { exitCode: 0, stdout: [(0, i18n_1.t)('sync.usage')], stderr: [] };
92
+ }
93
+ if (options.undo === true) {
94
+ const result = await (0, sync_1.syncUndo)(root);
95
+ if (options.json === true) {
96
+ return { exitCode: 0, stdout: [(0, command_json_1.toJsonLine)('sync', true, result, [])], stderr: [] };
97
+ }
98
+ const lines = [`sync undo ← ${result.snapshotPath}`];
99
+ for (const rel of result.restored) {
100
+ lines.push(` RESTORED ${rel}`);
101
+ }
102
+ for (const rel of result.removed) {
103
+ lines.push(` REMOVED ${rel}`);
104
+ }
105
+ for (const rel of result.skippedModified) {
106
+ lines.push(` MODIFIED ${rel} (${(0, i18n_1.t)('sync.undoSkipped')})`);
107
+ }
108
+ return { exitCode: 0, stdout: lines, stderr: [] };
109
+ }
110
+ if (!action || !['pull', 'push', 'status'].includes(action) || !target) {
111
+ const message = (0, i18n_1.t)('sync.usage');
112
+ if (options.json === true) {
113
+ return {
114
+ exitCode: 1,
115
+ stdout: [(0, command_json_1.toJsonLine)('sync', false, { action: action ?? null, target: target ?? null }, [(0, command_json_1.toJsonError)(command_error_codes_1.COMMAND_ERROR_CODES.MISSING_ARGUMENT, message)])],
116
+ stderr: [],
117
+ };
118
+ }
119
+ return { exitCode: 1, stdout: [], stderr: [message] };
120
+ }
121
+ const file = await loadSyncFile(options.config);
122
+ const config = await (0, config_1.loadConfig)();
123
+ const opts = {
124
+ target,
125
+ root,
126
+ agent: options.agent ?? file.agent ?? config.agent ?? 'all',
127
+ mode: options.mode ?? file.mode ?? 'full',
128
+ include: options.include ?? file.include ?? [],
129
+ exclude: options.exclude ?? file.exclude ?? [],
130
+ apply: options.apply === true && options.dryRun !== true,
131
+ overwrite: options.overwrite === true,
132
+ backup: options.backup === true,
133
+ withEnv: options.withEnv ?? file.withEnv ?? false,
134
+ envPath: options.env,
135
+ };
136
+ if (action === 'push') {
137
+ const result = await (0, sync_1.syncPush)(opts);
138
+ if (options.json === true) {
139
+ return { exitCode: 0, stdout: [(0, command_json_1.toJsonLine)('sync', true, result, [])], stderr: [] };
140
+ }
141
+ return { exitCode: 0, stdout: renderPush(result), stderr: [] };
142
+ }
143
+ if (action === 'pull') {
144
+ const result = await (0, sync_1.syncPull)(opts);
145
+ if (options.json === true) {
146
+ return { exitCode: 0, stdout: [(0, command_json_1.toJsonLine)('sync', true, result, [])], stderr: [] };
147
+ }
148
+ return { exitCode: 0, stdout: renderPull(result), stderr: [] };
149
+ }
150
+ const result = await (0, sync_1.syncStatus)(opts);
151
+ if (options.json === true) {
152
+ return { exitCode: 0, stdout: [(0, command_json_1.toJsonLine)('sync', true, result, [])], stderr: [] };
153
+ }
154
+ return { exitCode: 0, stdout: renderStatus(result), stderr: [] };
155
+ }
156
+ catch (error) {
157
+ const message = error instanceof Error ? error.message : String(error);
158
+ if (options.json === true) {
159
+ const code = message.startsWith('SYNC_ENV') || message.startsWith('SYNC_PASSPHRASE')
160
+ ? command_error_codes_1.COMMAND_ERROR_CODES.SYNC_ENV_FAILED
161
+ : command_error_codes_1.COMMAND_ERROR_CODES.SYNC_FAILED;
162
+ return {
163
+ exitCode: 1,
164
+ stdout: [(0, command_json_1.toJsonLine)('sync', false, { action: action ?? null, target: target ?? null }, [(0, command_json_1.toJsonError)(code, message)])],
165
+ stderr: [],
166
+ };
167
+ }
168
+ return { exitCode: 1, stdout: [], stderr: [message] };
169
+ }
170
+ }
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.runVerifyCommand = runVerifyCommand;
7
+ const node_path_1 = __importDefault(require("node:path"));
8
+ const package_lock_1 = require("../core/package-lock");
9
+ const command_error_codes_1 = require("../constants/command-error-codes");
10
+ const i18n_1 = require("../i18n");
11
+ const command_json_1 = require("../utils/command-json");
12
+ async function runVerifyCommand(packagePath, options = {}) {
13
+ if (!packagePath) {
14
+ if (options.json === true) {
15
+ return {
16
+ exitCode: 1,
17
+ stdout: [(0, command_json_1.toJsonLine)('verify', false, { packagePath: null }, [(0, command_json_1.toJsonError)(command_error_codes_1.COMMAND_ERROR_CODES.MISSING_ARGUMENT, (0, i18n_1.t)('verify.usage'))])],
18
+ stderr: [],
19
+ };
20
+ }
21
+ return { exitCode: 1, stdout: [], stderr: [(0, i18n_1.t)('verify.usage')] };
22
+ }
23
+ try {
24
+ const result = await (0, package_lock_1.verifyPackage)(packagePath, options.strict === true, options.signature ? node_path_1.default.resolve(options.signature) : undefined);
25
+ if (options.json === true) {
26
+ return {
27
+ exitCode: result.ok ? 0 : 1,
28
+ stdout: [(0, command_json_1.toJsonLine)('verify', result.ok, result, [])],
29
+ stderr: [],
30
+ };
31
+ }
32
+ if (result.ok) {
33
+ const stdout = [(0, i18n_1.t)('verify.pass', { count: result.checked })];
34
+ if (result.signature.present && result.signature.valid) {
35
+ stdout.push((0, i18n_1.t)('verify.signaturePass', { fingerprint: result.signature.fingerprint ?? 'unknown' }));
36
+ }
37
+ if (result.extra.length > 0) {
38
+ stdout.push((0, i18n_1.t)('verify.extra', { count: result.extra.length }));
39
+ }
40
+ return { exitCode: 0, stdout, stderr: [] };
41
+ }
42
+ const stderr = [
43
+ (0, i18n_1.t)('verify.fail', { missing: result.missing.length, modified: result.modified.length }),
44
+ ];
45
+ if (result.lockCorrupt) {
46
+ stderr.push((0, i18n_1.t)('verify.lockCorrupt'));
47
+ }
48
+ if (result.signature.present && !result.signature.valid) {
49
+ stderr.push((0, i18n_1.t)('verify.signatureFail', { reason: result.signature.error ?? 'invalid signature' }));
50
+ }
51
+ for (const file of result.missing) {
52
+ stderr.push(` missing: ${file}`);
53
+ }
54
+ for (const file of result.modified) {
55
+ stderr.push(` modified: ${file}`);
56
+ }
57
+ if (result.extra.length > 0) {
58
+ stderr.push((0, i18n_1.t)('verify.extra', { count: result.extra.length }));
59
+ }
60
+ return { exitCode: 1, stdout: [], stderr };
61
+ }
62
+ catch (error) {
63
+ const message = error instanceof Error ? error.message : String(error);
64
+ if (options.json === true) {
65
+ let code = command_error_codes_1.COMMAND_ERROR_CODES.VERIFY_FAILED;
66
+ if (message.includes('VERIFY_LOCK_MISSING')) {
67
+ code = command_error_codes_1.COMMAND_ERROR_CODES.VERIFY_LOCK_MISSING;
68
+ }
69
+ else if (message.includes('SIGNATURE_INVALID')) {
70
+ code = command_error_codes_1.COMMAND_ERROR_CODES.SIGNATURE_INVALID;
71
+ }
72
+ return {
73
+ exitCode: 1,
74
+ stdout: [(0, command_json_1.toJsonLine)('verify', false, { packagePath }, [(0, command_json_1.toJsonError)(code, message)])],
75
+ stderr: [],
76
+ };
77
+ }
78
+ return { exitCode: 1, stdout: [], stderr: [message] };
79
+ }
80
+ }
@@ -9,6 +9,7 @@ exports.COMMAND_ERROR_CODES = {
9
9
  TEMPLATE_VARIABLE_MISSING: 'TEMPLATE_VARIABLE_MISSING',
10
10
  MISSING_PACKAGE_MANIFEST: 'MISSING_PACKAGE_MANIFEST',
11
11
  MISSING_INSTALL_PLAN: 'MISSING_INSTALL_PLAN',
12
+ PACKAGE_FORMAT_UNSUPPORTED: 'PACKAGE_FORMAT_UNSUPPORTED',
12
13
  INSTALL_CONFLICT: 'INSTALL_CONFLICT',
13
14
  PATH_ESCAPE: 'PATH_ESCAPE',
14
15
  LOCK_TIMEOUT: 'LOCK_TIMEOUT',
@@ -16,4 +17,14 @@ exports.COMMAND_ERROR_CODES = {
16
17
  DOCTOR_FAILED: 'DOCTOR_FAILED',
17
18
  LIST_FAILED: 'LIST_FAILED',
18
19
  REPORT_FAILED: 'REPORT_FAILED',
20
+ SYNC_FAILED: 'SYNC_FAILED',
21
+ SYNC_ENV_FAILED: 'SYNC_ENV_FAILED',
22
+ PACK_FAILED: 'PACK_FAILED',
23
+ VERIFY_FAILED: 'VERIFY_FAILED',
24
+ VERIFY_LOCK_MISSING: 'VERIFY_LOCK_MISSING',
25
+ ATTEST_FAILED: 'ATTEST_FAILED',
26
+ SIGN_KEY_MISSING: 'SIGN_KEY_MISSING',
27
+ SIGN_KEY_EXISTS: 'SIGN_KEY_EXISTS',
28
+ SIGN_FAILED: 'SIGN_FAILED',
29
+ SIGNATURE_INVALID: 'SIGNATURE_INVALID',
19
30
  };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JSON_SCHEMA_VERSION = exports.PACKAGE_FORMAT_VERSION = void 0;
4
+ /**
5
+ * 安装包元数据格式版本(包根目录 `package.json` 的 `formatVersion` 字段)。
6
+ *
7
+ * 兼容规则:
8
+ * - 旧包没有 `package.json` 或没有 `formatVersion` → 视为 1,正常安装;
9
+ * - `formatVersion` 大于本常量 → 明确报错 `PACKAGE_FORMAT_UNSUPPORTED`,不崩溃;
10
+ * - 新增不破坏旧包读取能力的字段时,才提升此版本号。
11
+ */
12
+ exports.PACKAGE_FORMAT_VERSION = 1;
13
+ /**
14
+ * `--json` 输出的 schema 版本(`schemaVersion` 字段)。
15
+ * 1.0 起冻结:字段只增不删、语义不改;破坏性调整必须提升版本号。
16
+ */
17
+ exports.JSON_SCHEMA_VERSION = 1;