@dommaker/harness 4.0.0 → 4.0.1

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.
@@ -25,10 +25,8 @@ export declare function resolveCiPlatform(options: {
25
25
  githubActions?: boolean;
26
26
  }, configured: CiPlatform | 'none' | undefined): CiResolution;
27
27
  /**
28
- * 读 config.yml 已持久化的 `ci.platform`(解析链第二级)
28
+ * 从本次 init 已读取的旧 config.yml 取 `ci.platform`(解析链第二级)
29
29
  *
30
- * 缺失 / 形状不符一律按未配置处理;config.yml 解析失败经 attempt 显式降级为未配置——
31
- * init 是用户首次上手入口,存量脏配置不该炸掉初始化(解析失败会真实抛到用户眼前的
32
- * 场景是 check/sync-docs 等执法面,那里不兜)。
30
+ * 缺失 / 形状不符一律按未配置处理;文件只由命令入口读取一次。
33
31
  */
34
- export declare function readConfiguredCiPlatform(projectPath: string): CiPlatform | 'none' | undefined;
32
+ export declare function readConfiguredCiPlatform(raw: Record<string, unknown> | undefined): CiPlatform | 'none' | undefined;
@@ -7,8 +7,6 @@
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.resolveCiPlatform = resolveCiPlatform;
9
9
  exports.readConfiguredCiPlatform = readConfiguredCiPlatform;
10
- const attempt_1 = require("../../../utils/attempt");
11
- const project_config_loader_1 = require("../../../core/project-config-loader");
12
10
  /** `--ci` 的可取值域(`none` = 不创建任何服务端 CI 文件,治理 CI 面亦不建,即旧 `--no-github-actions`) */
13
11
  const CI_FLAG_VALUES = ['github', 'gitlab', 'none'];
14
12
  /**
@@ -42,14 +40,11 @@ function resolveCiPlatform(options, configured) {
42
40
  return { kind: 'ok', platform: flag ?? configured ?? 'github' };
43
41
  }
44
42
  /**
45
- * 读 config.yml 已持久化的 `ci.platform`(解析链第二级)
43
+ * 从本次 init 已读取的旧 config.yml 取 `ci.platform`(解析链第二级)
46
44
  *
47
- * 缺失 / 形状不符一律按未配置处理;config.yml 解析失败经 attempt 显式降级为未配置——
48
- * init 是用户首次上手入口,存量脏配置不该炸掉初始化(解析失败会真实抛到用户眼前的
49
- * 场景是 check/sync-docs 等执法面,那里不兜)。
45
+ * 缺失 / 形状不符一律按未配置处理;文件只由命令入口读取一次。
50
46
  */
51
- function readConfiguredCiPlatform(projectPath) {
52
- const raw = (0, attempt_1.attempt)(() => (0, project_config_loader_1.loadRawProjectConfig)(projectPath), () => undefined);
47
+ function readConfiguredCiPlatform(raw) {
53
48
  const ci = raw?.ci;
54
49
  if (ci === null || typeof ci !== 'object')
55
50
  return undefined;
@@ -68,8 +68,27 @@ const governance_1 = require("./governance");
68
68
  */
69
69
  async function init(options, io = command_contract_1.processIO) {
70
70
  const projectPath = options.projectPath || process.cwd();
71
+ const configPath = path.join(projectPath, '.harness', 'config.yml');
72
+ let existingDocument;
73
+ let existingConfig = {};
74
+ let existingConfigError;
75
+ try {
76
+ const document = yaml.loadDocument(await fs.readFile(configPath, 'utf-8'));
77
+ if (document.errors.length > 0)
78
+ throw document.errors[0];
79
+ const value = document.toJS();
80
+ if (document.contents !== null && (value === null || typeof value !== 'object' || Array.isArray(value))) {
81
+ throw new TypeError('旧 config.yml 根节点必须是 YAML map');
82
+ }
83
+ existingDocument = document;
84
+ existingConfig = (value ?? {});
85
+ }
86
+ catch (error) {
87
+ if (error.code !== 'ENOENT')
88
+ existingConfigError = error;
89
+ }
71
90
  // CI 平台先解析:用法错误不落任何盘,且 `--print-snippets` 也要按解析出的平台出形
72
- const ci = (0, ci_platform_1.resolveCiPlatform)(options, (0, ci_platform_1.readConfiguredCiPlatform)(projectPath));
91
+ const ci = (0, ci_platform_1.resolveCiPlatform)(options, (0, ci_platform_1.readConfiguredCiPlatform)(existingConfig));
73
92
  if (ci.kind === 'error') {
74
93
  (0, command_contract_1.logError)(io, chalk_1.default.red(`❌ 用法错误: ${ci.reason}`));
75
94
  return { kind: 'usage-error', reason: ci.reason };
@@ -91,6 +110,13 @@ async function init(options, io = command_contract_1.processIO) {
91
110
  (0, snippets_1.printSnippets)(io, ci.platform, options.governance);
92
111
  return { kind: 'ok' };
93
112
  }
113
+ // 共享所有权文件读不懂时禁止覆盖;--print-snippets 不落盘,仍可沿用 CI 缺省探测
114
+ if (existingConfigError) {
115
+ const detail = existingConfigError instanceof Error ? existingConfigError.message : String(existingConfigError);
116
+ const reason = `无法读取旧配置 ${configPath}: ${detail}`;
117
+ (0, command_contract_1.logError)(io, chalk_1.default.red(`❌ ${reason}`));
118
+ return { kind: 'fail', reason };
119
+ }
94
120
  (0, command_contract_1.log)(io, chalk_1.default.blue('🚀 初始化 harness 配置...'));
95
121
  const configDir = path.join(projectPath, '.harness');
96
122
  // 创建配置目录
@@ -99,26 +125,67 @@ async function init(options, io = command_contract_1.processIO) {
99
125
  // 选择预设
100
126
  const preset = presets_1.PRESETS[options.preset];
101
127
  (0, command_contract_1.log)(io, chalk_1.default.gray(`预设: ${options.preset}`));
102
- // 合并治理配置
103
- const configData = { ...preset };
128
+ // config.yml 是共享所有权文件:只覆盖本次受管键,消费方扩展原样保留并列名告知
129
+ const ciManaged = options.ci !== undefined || options.githubActions === false;
130
+ const managedKeys = new Set([...Object.keys(preset), 'harness']);
131
+ if (options.governance)
132
+ managedKeys.add('governance');
133
+ if (ciManaged)
134
+ managedKeys.add('ci');
135
+ const preservedKeys = Object.keys(existingConfig).filter(key => !managedKeys.has(key));
136
+ const configData = { ...existingConfig, ...preset };
104
137
  if (options.governance) {
105
138
  configData.governance = presets_1.GOVERNANCE_PRESETS[options.governance];
106
139
  (0, command_contract_1.log)(io, chalk_1.default.gray(`治理级别: ${options.governance}`));
107
140
  }
108
- // 非默认平台才持久化:缺省即 github(旧配置零迁移),裸 init 重写 config.yml 时
109
- // 也因此不会丢掉已选定的平台
141
+ // 非默认平台才持久化;只有显式 CI flag 才接管 ci 段,裸 init 保留消费方扩展
110
142
  if (ci.platform !== 'github') {
111
- configData.ci = { platform: ci.platform };
112
143
  (0, command_contract_1.log)(io, chalk_1.default.gray(`CI 平台: ${ci.platform}`));
113
144
  }
114
- // 写入 harness 版本
145
+ if (ciManaged) {
146
+ if (ci.platform === 'github')
147
+ delete configData.ci;
148
+ else
149
+ configData.ci = { platform: ci.platform };
150
+ }
151
+ // 写入 harness 版本,保留 harness 段内的非受管扩展
115
152
  const pkgVersion = (0, package_version_1.getHarnessPackageVersion)();
116
- configData.harness = { version: pkgVersion };
117
- // 写入配置文件(harness 管理的配置,始终重新生成)
118
- const configPath = path.join(configDir, 'config.yml');
119
- const configContent = yaml.dump(configData, { indent: 2 });
153
+ const existingHarness = existingConfig.harness;
154
+ const harnessConfig = existingHarness !== null && typeof existingHarness === 'object' && !Array.isArray(existingHarness)
155
+ ? existingHarness
156
+ : {};
157
+ configData.harness = { ...harnessConfig, version: pkgVersion };
158
+ if (Object.keys(harnessConfig).some(key => key !== 'version'))
159
+ preservedKeys.push('harness');
160
+ // 旧配置经 Document 原地更新受管键,非受管键与注释(含 AUTO-GENERATED 标记)不失真
161
+ let configContent;
162
+ if (existingDocument) {
163
+ for (const key of Object.keys(preset))
164
+ existingDocument.set(key, configData[key]);
165
+ if (options.governance)
166
+ existingDocument.set('governance', configData.governance);
167
+ if (ciManaged) {
168
+ if (ci.platform === 'github')
169
+ existingDocument.delete('ci');
170
+ else
171
+ existingDocument.set('ci', configData.ci);
172
+ }
173
+ if (existingHarness !== null && typeof existingHarness === 'object' && !Array.isArray(existingHarness)) {
174
+ existingDocument.setIn(['harness', 'version'], pkgVersion);
175
+ }
176
+ else {
177
+ existingDocument.set('harness', configData.harness);
178
+ }
179
+ configContent = yaml.dumpDocument(existingDocument, { indent: 2 });
180
+ }
181
+ else {
182
+ configContent = yaml.dump(configData, { indent: 2 });
183
+ }
120
184
  await fs.writeFile(configPath, configContent, 'utf-8');
121
185
  (0, command_contract_1.log)(io, chalk_1.default.green(`✅ 已创建配置文件: ${configPath} (v${pkgVersion})`));
186
+ if (preservedKeys.length > 0) {
187
+ (0, command_contract_1.log)(io, chalk_1.default.gray(`保留 ${preservedKeys.length} 个非受管键:${preservedKeys.join('、')}`));
188
+ }
122
189
  // 受管示例文件:检查点 / Resolutions(RKB 狗粮)
123
190
  await (0, scaffold_1.runPlan)([(0, scaffold_1.checkpointsFile)(projectPath), (0, scaffold_1.resolutionsFile)(projectPath)], io);
124
191
  // CAPABILITIES.md / CHANGELOG.md 由 sync-docs(AI 治理)管理,init 不创建
@@ -3,11 +3,21 @@
3
3
  *
4
4
  * 全仓唯一 import 'yaml' 的模块;调用方一律经本模块,不允许直接依赖 yaml 包。
5
5
  * merge:true 对齐 js-yaml 的合并键(<<)默认展开口径;空文档返回 null(js-yaml 为 undefined)。
6
+ * 需保留注释的读-改-写走 loadDocument / dumpDocument(harness#210),普通值投影仍走 load / dump。
6
7
  */
8
+ import { parseDocument } from 'yaml';
9
+ export type YamlDocument = ReturnType<typeof parseDocument>;
7
10
  /** 解析 YAML 文本(合并键展开;空文档返回 null) */
8
11
  export declare function load(text: string): unknown;
12
+ /** 解析为保留注释的 YAML Document(供读-改-写受管键) */
13
+ export declare function loadDocument(text: string): YamlDocument;
9
14
  /** 序列化为 YAML 文本(options 透传给 yaml@2 stringify) */
10
15
  export declare function dump(value: unknown, options?: {
11
16
  lineWidth?: number;
12
17
  indent?: number;
13
18
  }): string;
19
+ /** 序列化保留注释的 YAML Document */
20
+ export declare function dumpDocument(document: YamlDocument, options?: {
21
+ lineWidth?: number;
22
+ indent?: number;
23
+ }): string;
@@ -4,16 +4,27 @@
4
4
  *
5
5
  * 全仓唯一 import 'yaml' 的模块;调用方一律经本模块,不允许直接依赖 yaml 包。
6
6
  * merge:true 对齐 js-yaml 的合并键(<<)默认展开口径;空文档返回 null(js-yaml 为 undefined)。
7
+ * 需保留注释的读-改-写走 loadDocument / dumpDocument(harness#210),普通值投影仍走 load / dump。
7
8
  */
8
9
  Object.defineProperty(exports, "__esModule", { value: true });
9
10
  exports.load = load;
11
+ exports.loadDocument = loadDocument;
10
12
  exports.dump = dump;
13
+ exports.dumpDocument = dumpDocument;
11
14
  const yaml_1 = require("yaml");
12
15
  /** 解析 YAML 文本(合并键展开;空文档返回 null) */
13
16
  function load(text) {
14
17
  return (0, yaml_1.parse)(text, { merge: true });
15
18
  }
19
+ /** 解析为保留注释的 YAML Document(供读-改-写受管键) */
20
+ function loadDocument(text) {
21
+ return (0, yaml_1.parseDocument)(text, { merge: true });
22
+ }
16
23
  /** 序列化为 YAML 文本(options 透传给 yaml@2 stringify) */
17
24
  function dump(value, options) {
18
25
  return (0, yaml_1.stringify)(value, options);
19
26
  }
27
+ /** 序列化保留注释的 YAML Document */
28
+ function dumpDocument(document, options) {
29
+ return document.toString(options);
30
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dommaker/harness",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "通用工程约束框架 - 约束检查(severity 分级)、检查点验证、测试门控",
5
5
  "keywords": [
6
6
  "harness",