@donotdev/cli 0.3.6 → 0.3.8

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.
@@ -8850,7 +8850,7 @@ function globSync(pattern, options = {}) {
8850
8850
  });
8851
8851
  return results.map((p) => pathResolverInstance.normalizePath(p));
8852
8852
  }
8853
- var pathResolverInstance, getRepoRoot, getAppRoot, resolveFrameworkPackage, normalizePath, pathExists, read, readSync, write, writeSync, removeSync, statSync2, ensureDir, getRelativePathBetween, getDirname, getBasename, joinPath;
8853
+ var pathResolverInstance, getRepoRoot, getAppRoot, resolveFrameworkPackage, normalizePath, pathExists, read, readSync, write, writeSync, removeSync, statSync2, ensureDir, getRelativePathBetween, getDirname, joinPath;
8854
8854
  var init_pathResolver = __esm({
8855
8855
  "packages/tooling/src/utils/pathResolver.ts"() {
8856
8856
  "use strict";
@@ -8877,9 +8877,6 @@ var init_pathResolver = __esm({
8877
8877
  getDirname = (filePath) => {
8878
8878
  return normalizePath(dirname3(filePath));
8879
8879
  };
8880
- getBasename = (filePath, ext) => {
8881
- return basename2(filePath, ext);
8882
- };
8883
8880
  joinPath = (...pathSegments) => {
8884
8881
  return normalizePath(...pathSegments);
8885
8882
  };
@@ -9669,6 +9666,241 @@ function generatePackageJson(templateName, mode, options = {}) {
9669
9666
  return result;
9670
9667
  }
9671
9668
 
9669
+ // packages/tooling/src/maintenance/template-sync.ts
9670
+ init_utils2();
9671
+ init_cli_output();
9672
+ init_errors();
9673
+ init_pathResolver();
9674
+ init_app_detection();
9675
+ init_type_guards();
9676
+ var PROJECT_MANIFEST = [
9677
+ // Documentation: the framework's instructions and rules for any agent
9678
+ { pattern: "AGENTS.md.example", kind: "docs", strategy: "replace" },
9679
+ { pattern: "CLAUDE.md.example", kind: "docs", strategy: "create-if-missing" },
9680
+ {
9681
+ pattern: ".claude/rules/**/*.md.example",
9682
+ ignore: [".claude/rules/consumer-ux.md.example"],
9683
+ kind: "docs",
9684
+ strategy: "replace"
9685
+ },
9686
+ { pattern: ".cursor/rules/**/*.mdc.example", kind: "docs", strategy: "replace" },
9687
+ { pattern: ".dndev/args.json.example", kind: "docs", strategy: "create-if-missing" },
9688
+ // AI harness: agents, skills, hooks, their registration, the MCP servers
9689
+ { pattern: ".claude/agents/**/*.md.example", kind: "harness", strategy: "replace" },
9690
+ { pattern: ".claude/skills/**/*", kind: "harness", strategy: "replace" },
9691
+ { pattern: ".claude/hooks/**/*.example", kind: "harness", strategy: "replace" },
9692
+ { pattern: ".claude/settings.json.example", kind: "harness", strategy: "merge-settings" },
9693
+ { pattern: ".claude/rules/consumer-ux.md.example", kind: "harness", strategy: "replace" },
9694
+ { pattern: ".mcp.json.example", kind: "harness", strategy: "merge-mcp" },
9695
+ { pattern: ".cursor/mcp.json.example", kind: "harness", strategy: "merge-mcp" },
9696
+ { pattern: ".gemini/settings.json.example", kind: "harness", strategy: "merge-mcp" }
9697
+ ];
9698
+ function isGlobalHarness() {
9699
+ return process.env.DNDEV_HARNESS === "global";
9700
+ }
9701
+ function toDest(relativeTemplatePath) {
9702
+ return relativeTemplatePath.endsWith(".example") ? relativeTemplatePath.slice(0, -".example".length) : relativeTemplatePath;
9703
+ }
9704
+ async function listTemplateFiles(sourceRoot, kind) {
9705
+ const root = normalizePath(sourceRoot);
9706
+ const files = [];
9707
+ for (const entry of PROJECT_MANIFEST) {
9708
+ if (entry.kind !== kind) continue;
9709
+ const matches = await glob(entry.pattern, {
9710
+ cwd: root,
9711
+ onlyFiles: true,
9712
+ ignore: entry.ignore
9713
+ });
9714
+ for (const match of matches) {
9715
+ const relativeTemplatePath = normalizePath(match);
9716
+ files.push({
9717
+ sourceFile: joinPath(root, relativeTemplatePath),
9718
+ relativeDest: toDest(relativeTemplatePath),
9719
+ strategy: entry.strategy
9720
+ });
9721
+ }
9722
+ }
9723
+ return files;
9724
+ }
9725
+ function getSourceTemplatesPath() {
9726
+ try {
9727
+ const templatesRoot = getTemplatesRoot();
9728
+ const rootConsumerPath = joinPath(templatesRoot, "root-consumer");
9729
+ if (pathExists(rootConsumerPath)) {
9730
+ return normalizePath(rootConsumerPath);
9731
+ }
9732
+ } catch {
9733
+ }
9734
+ const appRoot = getAppRoot();
9735
+ if (appRoot) {
9736
+ const directPath = joinPath(
9737
+ appRoot,
9738
+ "node_modules",
9739
+ "@donotdev",
9740
+ "cli",
9741
+ "templates",
9742
+ "root-consumer"
9743
+ );
9744
+ if (pathExists(directPath)) {
9745
+ log2.info("Found templates via node_modules lookup");
9746
+ return normalizePath(directPath);
9747
+ }
9748
+ }
9749
+ log2.error("Could not find the framework templates (@donotdev/cli).");
9750
+ log2.info(" Templates are looked up the same way as create-project:");
9751
+ log2.info(" \u2022 Published mode: the templates shipped with the running CLI");
9752
+ log2.info(" \u2022 Dev mode: packages/cli/templates (from monorepo)");
9753
+ log2.info(" Run the global CLI (dndev), not a copy inside the project.");
9754
+ return null;
9755
+ }
9756
+ var PROJECT_SECTION_MARKER = "<!-- dndev:project-specific -->";
9757
+ function detectProjectPlatform(projectRoot) {
9758
+ const apps = detectApps(projectRoot);
9759
+ const withProvider = apps.find(
9760
+ (app) => app.provider === "firebase" || app.provider === "supabase"
9761
+ );
9762
+ if (withProvider?.provider === "firebase") return "firebase";
9763
+ if (withProvider?.provider === "supabase") return "supabase";
9764
+ if (apps.some((app) => app.functions === "vercel")) return "vercel";
9765
+ return "none";
9766
+ }
9767
+ function mergeProjectSection(template, existing) {
9768
+ const templateIndex = template.indexOf(PROJECT_SECTION_MARKER);
9769
+ if (templateIndex === -1 || existing === null) return template;
9770
+ const existingIndex = existing.indexOf(PROJECT_SECTION_MARKER);
9771
+ if (existingIndex === -1) return template;
9772
+ return template.slice(0, templateIndex + PROJECT_SECTION_MARKER.length) + existing.slice(existingIndex + PROJECT_SECTION_MARKER.length);
9773
+ }
9774
+ function mergeMcpConfig(template, existing) {
9775
+ if (existing === null) return template;
9776
+ const templateConfig = JSON.parse(template);
9777
+ const existingConfig = JSON.parse(existing);
9778
+ const merged = {
9779
+ ...existingConfig,
9780
+ ...templateConfig,
9781
+ mcpServers: {
9782
+ ...existingConfig.mcpServers,
9783
+ ...templateConfig.mcpServers
9784
+ }
9785
+ };
9786
+ if (JSON.stringify(merged) === JSON.stringify(existingConfig)) {
9787
+ return existing;
9788
+ }
9789
+ return JSON.stringify(merged, null, 2) + "\n";
9790
+ }
9791
+ function mergeHookGroups(existing, template) {
9792
+ const groupHooks = (group2) => isRecord(group2) && Array.isArray(group2.hooks) ? group2.hooks : [];
9793
+ const hookCommand = (hook) => isRecord(hook) ? hook.command : void 0;
9794
+ const templateCommands = new Set(
9795
+ template.flatMap((group2) => groupHooks(group2).map(hookCommand))
9796
+ );
9797
+ const kept = existing.flatMap((group2) => {
9798
+ if (!isRecord(group2)) return [group2];
9799
+ const ownHooks = groupHooks(group2).filter(
9800
+ (hook) => !templateCommands.has(hookCommand(hook))
9801
+ );
9802
+ return ownHooks.length > 0 ? [{ ...group2, hooks: ownHooks }] : [];
9803
+ });
9804
+ return [...template, ...kept];
9805
+ }
9806
+ function mergeJsonValue(existing, template, path) {
9807
+ if (Array.isArray(existing) && Array.isArray(template)) {
9808
+ if (path.length === 2 && path[0] === "hooks") {
9809
+ return mergeHookGroups(existing, template);
9810
+ }
9811
+ const existingItems = new Set(existing.map((item) => JSON.stringify(item)));
9812
+ return [
9813
+ ...existing,
9814
+ ...template.filter((item) => !existingItems.has(JSON.stringify(item)))
9815
+ ];
9816
+ }
9817
+ if (isRecord(existing) && isRecord(template)) {
9818
+ const merged = {};
9819
+ for (const key of Object.keys(existing)) {
9820
+ merged[key] = key in template ? mergeJsonValue(existing[key], template[key], [...path, key]) : existing[key];
9821
+ }
9822
+ for (const key of Object.keys(template)) {
9823
+ if (!(key in existing)) merged[key] = template[key];
9824
+ }
9825
+ return merged;
9826
+ }
9827
+ return template;
9828
+ }
9829
+ function mergeClaudeSettings(template, existing) {
9830
+ if (existing === null) return template;
9831
+ const existingSettings = JSON.parse(existing);
9832
+ const merged = mergeJsonValue(existingSettings, JSON.parse(template), []);
9833
+ if (JSON.stringify(merged) === JSON.stringify(existingSettings)) {
9834
+ return existing;
9835
+ }
9836
+ return JSON.stringify(merged, null, 2) + "\n";
9837
+ }
9838
+ async function syncFile(file, appRoot) {
9839
+ const destPath = joinPath(appRoot, file.relativeDest);
9840
+ const exists = pathExists(destPath);
9841
+ if (file.strategy === "create-if-missing" && exists) return "preserved";
9842
+ const templateContent = asText(await read(file.sourceFile, { format: "text" }));
9843
+ if (!templateContent) {
9844
+ throw new Error(`Template file is empty or unreadable: ${file.sourceFile}`);
9845
+ }
9846
+ const existingContent = exists ? asText(await read(destPath, { format: "text" })) : null;
9847
+ let content;
9848
+ switch (file.strategy) {
9849
+ case "merge-mcp":
9850
+ content = mergeMcpConfig(templateContent, existingContent);
9851
+ break;
9852
+ case "merge-settings":
9853
+ content = mergeClaudeSettings(templateContent, existingContent);
9854
+ break;
9855
+ case "create-if-missing":
9856
+ content = templateContent.replace("{{platform}}", detectProjectPlatform(appRoot));
9857
+ break;
9858
+ case "replace":
9859
+ content = mergeProjectSection(templateContent, existingContent);
9860
+ break;
9861
+ }
9862
+ if (existingContent === content) return "unchanged";
9863
+ await ensureDir(getDirname(destPath));
9864
+ await write(destPath, content, { format: "text", overwrite: true });
9865
+ return "synced";
9866
+ }
9867
+ async function syncTemplateFiles(files, appRoot, options = {}) {
9868
+ const result = { synced: 0, preserved: 0, errors: 0 };
9869
+ for (const file of files) {
9870
+ const exists = pathExists(joinPath(appRoot, file.relativeDest));
9871
+ if (options.dryRun) {
9872
+ if (file.strategy === "create-if-missing" && exists) {
9873
+ log2.info(`[DRY RUN] Would preserve: ${file.relativeDest}`);
9874
+ result.preserved++;
9875
+ } else {
9876
+ log2.info(`[DRY RUN] Would sync: ${file.relativeDest}`);
9877
+ result.synced++;
9878
+ }
9879
+ continue;
9880
+ }
9881
+ try {
9882
+ const outcome = await syncFile(file, appRoot);
9883
+ if (outcome === "synced") {
9884
+ result.synced++;
9885
+ log2.info(`Synced: ${file.relativeDest}`);
9886
+ } else if (outcome === "preserved") {
9887
+ result.preserved++;
9888
+ }
9889
+ } catch (error2) {
9890
+ result.errors++;
9891
+ log2.error(
9892
+ `Error syncing ${file.relativeDest}:`,
9893
+ DoNotDevError.from(
9894
+ error2,
9895
+ `Failed to sync ${file.relativeDest}`,
9896
+ "file-operation-error"
9897
+ )
9898
+ );
9899
+ }
9900
+ }
9901
+ return result;
9902
+ }
9903
+
9672
9904
  // packages/tooling/src/cli/index.ts
9673
9905
  init_create_utils();
9674
9906
 
@@ -9867,321 +10099,188 @@ async function syncApp(appName, options = {}) {
9867
10099
  // packages/tooling/src/maintenance/sync-docs.ts
9868
10100
  init_utils2();
9869
10101
  init_cli_output();
10102
+ init_pathResolver();
10103
+ async function syncDocumentation(options = {}) {
10104
+ const appRoot = getAppRoot();
10105
+ if (!appRoot) {
10106
+ log2.error("Could not determine project root");
10107
+ return 1;
10108
+ }
10109
+ const sourceRoot = getSourceTemplatesPath();
10110
+ if (!sourceRoot) return 1;
10111
+ const files = await listTemplateFiles(sourceRoot, "docs");
10112
+ const result = await syncTemplateFiles(files, appRoot, options);
10113
+ if (options.dryRun) {
10114
+ log2.info(`[DRY RUN] Would sync ${result.synced} documentation file(s)`);
10115
+ if (result.preserved > 0) {
10116
+ log2.info(`[DRY RUN] Would preserve ${result.preserved} project-owned file(s)`);
10117
+ }
10118
+ return 0;
10119
+ }
10120
+ if (result.synced > 0) {
10121
+ log2.success(`Synced ${result.synced} documentation file(s) from framework`);
10122
+ } else {
10123
+ log2.info("Documentation up to date");
10124
+ }
10125
+ if (result.preserved > 0) {
10126
+ log2.info(`Preserved ${result.preserved} project-owned file(s)`);
10127
+ }
10128
+ if (result.errors > 0) {
10129
+ log2.warn(`${result.errors} documentation file(s) could not be synced`);
10130
+ return 1;
10131
+ }
10132
+ return 0;
10133
+ }
10134
+
10135
+ // packages/tooling/src/maintenance/sync-harness.ts
10136
+ init_utils2();
10137
+ init_cli_output();
9870
10138
  init_errors();
9871
10139
  init_pathResolver();
9872
- init_app_detection();
9873
10140
  init_type_guards();
9874
- var SYNC_PATHS = [
9875
- // Documentation
9876
- "guides/**/*.md.example",
9877
- "guides/**/*.json.example",
9878
- // Commands
9879
- ".claude/commands/**/*.md.example",
9880
- // Skills
9881
- ".claude/skills/**/*.md.example",
9882
- ".claude/skills/**/*.md",
9883
- // Agents
9884
- ".claude/agents/**/*.md.example",
9885
- // Hooks (template-owned, overwritten) + their registration (merged)
9886
- ".claude/hooks/**/*.example",
9887
- ".claude/settings.json.example",
9888
- // AI instructions (SSOT for agent behavior)
9889
- "AGENTS.md.example",
9890
- "CLAUDE.md.example",
9891
- // Rules
9892
- ".claude/rules/**/*.md.example",
9893
- // Config files
9894
- ".claude/mcp.json.example",
9895
- // MCP config
9896
- ".cursor/mcp.json.example",
9897
- ".mcp.json.example",
9898
- // Project args (created if missing, preserved if customized)
9899
- ".dndev/args.json.example"
9900
- ];
9901
- var PRESERVE_IF_EXISTS = [
9902
- "CLAUDE.md",
9903
- // User may have customized
9904
- "GEMINI.md",
9905
- // User may have customized
9906
- "args.json"
9907
- // User/agent may have customized features
9908
- ];
9909
- var PROJECT_SECTION_MARKER = "<!-- dndev:project-specific -->";
9910
- function detectProjectPlatform(projectRoot) {
9911
- const apps = detectApps(projectRoot);
9912
- const withProvider = apps.find(
9913
- (app) => app.provider === "firebase" || app.provider === "supabase"
10141
+ import { readdirSync as readdirSync4, rmdirSync, rmSync as rmSync2 } from "node:fs";
10142
+ function stripTemplateHooks(template, existing) {
10143
+ const settings2 = JSON.parse(existing);
10144
+ const templateSettings = JSON.parse(template);
10145
+ if (!isRecord(settings2) || !isRecord(settings2.hooks)) return existing;
10146
+ const templateHooks = isRecord(templateSettings) && isRecord(templateSettings.hooks) ? templateSettings.hooks : {};
10147
+ const commandsOf = (groups) => new Set(
10148
+ (Array.isArray(groups) ? groups : []).flatMap(
10149
+ (group2) => isRecord(group2) && Array.isArray(group2.hooks) ? group2.hooks.map((hook) => isRecord(hook) ? hook.command : void 0) : []
10150
+ )
9914
10151
  );
9915
- if (withProvider?.provider === "firebase") return "firebase";
9916
- if (withProvider?.provider === "supabase") return "supabase";
9917
- if (apps.some((app) => app.functions === "vercel")) return "vercel";
9918
- return "none";
9919
- }
9920
- function mergeProjectSection(template, existing) {
9921
- const templateIndex = template.indexOf(PROJECT_SECTION_MARKER);
9922
- if (templateIndex === -1 || existing === null) return template;
9923
- const existingIndex = existing.indexOf(PROJECT_SECTION_MARKER);
9924
- if (existingIndex === -1) return template;
9925
- return template.slice(0, templateIndex + PROJECT_SECTION_MARKER.length) + existing.slice(existingIndex + PROJECT_SECTION_MARKER.length);
9926
- }
9927
- function mergeMcpConfig(template, existing) {
9928
- if (existing === null) return template;
9929
- const templateConfig = JSON.parse(template);
9930
- const existingConfig = JSON.parse(existing);
9931
- const merged = {
9932
- ...existingConfig,
9933
- ...templateConfig,
9934
- mcpServers: {
9935
- ...existingConfig.mcpServers,
9936
- ...templateConfig.mcpServers
9937
- }
9938
- };
9939
- if (JSON.stringify(merged) === JSON.stringify(existingConfig)) {
9940
- return existing;
10152
+ const hooks = {};
10153
+ for (const [event, groups] of Object.entries(settings2.hooks)) {
10154
+ const templateCommands = commandsOf(templateHooks[event]);
10155
+ const kept = (Array.isArray(groups) ? groups : []).flatMap((group2) => {
10156
+ if (!isRecord(group2) || !Array.isArray(group2.hooks)) return [group2];
10157
+ const own = group2.hooks.filter(
10158
+ (hook) => !(isRecord(hook) && templateCommands.has(hook.command))
10159
+ );
10160
+ return own.length > 0 ? [{ ...group2, hooks: own }] : [];
10161
+ });
10162
+ if (kept.length > 0) hooks[event] = kept;
9941
10163
  }
9942
- return JSON.stringify(merged, null, 2) + "\n";
10164
+ const { hooks: _removed, ...rest } = settings2;
10165
+ const next = Object.keys(hooks).length > 0 ? { ...rest, hooks } : rest;
10166
+ if (JSON.stringify(next) === JSON.stringify(settings2)) return existing;
10167
+ return JSON.stringify(next, null, 2) + "\n";
9943
10168
  }
9944
- function mergeHookGroups(existing, template) {
9945
- const groupHooks = (group2) => isRecord(group2) && Array.isArray(group2.hooks) ? group2.hooks : [];
9946
- const hookCommand = (hook) => isRecord(hook) ? hook.command : void 0;
9947
- const templateCommands = new Set(
9948
- template.flatMap((group2) => groupHooks(group2).map(hookCommand))
10169
+ function stripTemplateServers(template, existing) {
10170
+ const config = JSON.parse(existing);
10171
+ const templateConfig = JSON.parse(template);
10172
+ if (!isRecord(config) || !isRecord(config.mcpServers)) return existing;
10173
+ const templateNames = new Set(
10174
+ isRecord(templateConfig) && isRecord(templateConfig.mcpServers) ? Object.keys(templateConfig.mcpServers) : []
9949
10175
  );
9950
- const kept = existing.flatMap((group2) => {
9951
- if (!isRecord(group2)) return [group2];
9952
- const ownHooks = groupHooks(group2).filter(
9953
- (hook) => !templateCommands.has(hookCommand(hook))
9954
- );
9955
- return ownHooks.length > 0 ? [{ ...group2, hooks: ownHooks }] : [];
9956
- });
9957
- return [...template, ...kept];
9958
- }
9959
- function mergeJsonValue(existing, template, path) {
9960
- if (Array.isArray(existing) && Array.isArray(template)) {
9961
- if (path.length === 2 && path[0] === "hooks") {
9962
- return mergeHookGroups(existing, template);
9963
- }
9964
- const existingItems = new Set(existing.map((item) => JSON.stringify(item)));
9965
- return [
9966
- ...existing,
9967
- ...template.filter((item) => !existingItems.has(JSON.stringify(item)))
9968
- ];
10176
+ const servers = Object.fromEntries(
10177
+ Object.entries(config.mcpServers).filter(([name]) => !templateNames.has(name))
10178
+ );
10179
+ if (Object.keys(servers).length === Object.keys(config.mcpServers).length) {
10180
+ return existing;
9969
10181
  }
9970
- if (isRecord(existing) && isRecord(template)) {
9971
- const merged = {};
9972
- for (const key of Object.keys(existing)) {
9973
- merged[key] = key in template ? mergeJsonValue(existing[key], template[key], [...path, key]) : existing[key];
9974
- }
9975
- for (const key of Object.keys(template)) {
9976
- if (!(key in existing)) merged[key] = template[key];
9977
- }
9978
- return merged;
10182
+ const { mcpServers: _servers, ...rest } = config;
10183
+ if (Object.keys(servers).length === 0 && Object.keys(rest).length === 0) {
10184
+ return null;
9979
10185
  }
9980
- return template;
10186
+ return JSON.stringify({ ...rest, mcpServers: servers }, null, 2) + "\n";
9981
10187
  }
9982
- function mergeClaudeSettings(template, existing) {
9983
- if (existing === null) return template;
9984
- const existingSettings = JSON.parse(existing);
9985
- const merged = mergeJsonValue(existingSettings, JSON.parse(template), []);
9986
- if (JSON.stringify(merged) === JSON.stringify(existingSettings)) {
9987
- return existing;
9988
- }
9989
- return JSON.stringify(merged, null, 2) + "\n";
10188
+ async function planRemoval(file, appRoot) {
10189
+ const destPath = joinPath(appRoot, file.relativeDest);
10190
+ if (!pathExists(destPath)) return null;
10191
+ if (file.strategy !== "merge-mcp" && file.strategy !== "merge-settings") {
10192
+ return { action: "delete" };
10193
+ }
10194
+ const template = asText(await read(file.sourceFile, { format: "text" }));
10195
+ const existing = asText(await read(destPath, { format: "text" }));
10196
+ if (template === null || existing === null) {
10197
+ throw new Error(`Cannot read ${file.relativeDest} or its template`);
10198
+ }
10199
+ const next = file.strategy === "merge-mcp" ? stripTemplateServers(template, existing) : stripTemplateHooks(template, existing);
10200
+ if (next === existing) return null;
10201
+ return next === null ? { action: "delete" } : { action: "write", content: next };
9990
10202
  }
9991
- function getSourceTemplatesPath() {
9992
- try {
9993
- const templatesRoot = getTemplatesRoot();
9994
- const rootConsumerPath = joinPath(templatesRoot, "root-consumer");
9995
- if (pathExists(rootConsumerPath)) {
9996
- return normalizePath(rootConsumerPath);
9997
- }
9998
- } catch {
9999
- }
10000
- const appRoot = getAppRoot();
10001
- if (appRoot) {
10002
- const directPath = joinPath(
10003
- appRoot,
10004
- "node_modules",
10005
- "@donotdev",
10006
- "cli",
10007
- "templates",
10008
- "root-consumer"
10009
- );
10010
- if (pathExists(directPath)) {
10011
- log2.info("Found templates via node_modules lookup");
10012
- return normalizePath(directPath);
10013
- }
10203
+ function removeHarnessFile(file, appRoot) {
10204
+ rmSync2(file);
10205
+ const stop = joinPath(appRoot, ".claude");
10206
+ let dir = getDirname(file);
10207
+ while (dir.startsWith(stop + "/") && readdirSync4(dir).length === 0) {
10208
+ rmdirSync(dir);
10209
+ dir = getDirname(dir);
10014
10210
  }
10015
- log2.warn(
10016
- "Could not find @donotdev/cli templates. Is @donotdev/cli installed?"
10017
- );
10018
- return null;
10019
10211
  }
10020
- async function syncFile(sourcePath, destPath, preserveIfExists, appRoot) {
10021
- const destPathWithoutExample = destPath.endsWith(".example") && destPath.length > 8 ? destPath.slice(0, -8) : destPath;
10022
- if (preserveIfExists && pathExists(destPathWithoutExample)) {
10023
- const relativePath = getRelativePathBetween(
10024
- appRoot,
10025
- destPathWithoutExample
10026
- );
10027
- log2.info(`Preserving user-customized file: ${relativePath}`);
10028
- return false;
10029
- }
10030
- if (!pathExists(sourcePath)) {
10031
- log2.warn(`Source file not found: ${sourcePath}`);
10032
- return false;
10033
- }
10034
- const templateContent = asText(await read(sourcePath, { format: "text" }));
10035
- if (!templateContent) {
10036
- log2.warn(`Source file is empty: ${sourcePath}`);
10037
- return false;
10038
- }
10039
- const existingContent = pathExists(destPathWithoutExample) ? asText(await read(destPathWithoutExample, { format: "text" })) : null;
10040
- const relativeDest = getRelativePathBetween(appRoot, destPathWithoutExample);
10041
- let content;
10042
- if (getBasename(relativeDest).endsWith("mcp.json")) {
10043
- content = mergeMcpConfig(templateContent, existingContent);
10044
- } else if (relativeDest === ".claude/settings.json") {
10045
- content = mergeClaudeSettings(templateContent, existingContent);
10046
- } else if (relativeDest === ".dndev/args.json") {
10047
- content = templateContent.replace(
10048
- "{{platform}}",
10049
- detectProjectPlatform(appRoot)
10050
- );
10051
- } else {
10052
- content = mergeProjectSection(templateContent, existingContent);
10053
- }
10054
- if (existingContent === content) {
10055
- return false;
10212
+ async function listHarnessLeftovers() {
10213
+ if (!isGlobalHarness()) return [];
10214
+ const appRoot = getAppRoot();
10215
+ const sourceRoot = getSourceTemplatesPath();
10216
+ if (!appRoot || !sourceRoot) return [];
10217
+ const leftovers = [];
10218
+ for (const file of await listTemplateFiles(sourceRoot, "harness")) {
10219
+ if (await planRemoval(file, appRoot)) leftovers.push(file.relativeDest);
10056
10220
  }
10057
- await ensureDir(getDirname(destPathWithoutExample));
10058
- await write(destPathWithoutExample, content, {
10059
- format: "text",
10060
- overwrite: true
10061
- });
10062
- return true;
10221
+ return leftovers;
10063
10222
  }
10064
- async function syncTemplates(sourceRoot, destRoot, dryRun) {
10065
- let synced = 0;
10066
- let preserved = 0;
10223
+ async function removeHarnessCopies(files, appRoot, dryRun) {
10224
+ let removed = 0;
10067
10225
  let errors = 0;
10068
- const normalizedSourceRoot = normalizePath(sourceRoot);
10069
- const normalizedDestRoot = normalizePath(destRoot);
10070
- for (const pattern of SYNC_PATHS) {
10226
+ for (const file of files) {
10071
10227
  try {
10072
- const sourceFiles = await glob(pattern, {
10073
- cwd: normalizedSourceRoot,
10074
- absolute: true,
10075
- onlyFiles: true
10076
- });
10077
- for (const sourceFile of sourceFiles) {
10078
- const relativePath = normalizePath(sourceFile).replace(
10079
- normalizedSourceRoot + "/",
10080
- ""
10081
- );
10082
- const destFile = joinPath(normalizedDestRoot, relativePath);
10083
- const destWithoutExample = destFile.endsWith(".example") ? destFile.slice(0, -8) : destFile;
10084
- const shouldPreserve = PRESERVE_IF_EXISTS.some(
10085
- (preserve) => relativePath.includes(preserve.replace(".example", ""))
10086
- );
10087
- if (dryRun) {
10088
- if (shouldPreserve && pathExists(destWithoutExample)) {
10089
- const relativeDest = getRelativePathBetween(
10090
- normalizedDestRoot,
10091
- destWithoutExample
10092
- );
10093
- log2.info(`[DRY RUN] Would preserve: ${relativeDest}`);
10094
- preserved++;
10095
- } else {
10096
- const relativeDest = getRelativePathBetween(
10097
- normalizedDestRoot,
10098
- destWithoutExample
10099
- );
10100
- log2.info(`[DRY RUN] Would sync: ${relativeDest}`);
10101
- synced++;
10102
- }
10103
- } else {
10104
- const wasSynced = await syncFile(
10105
- sourceFile,
10106
- destFile,
10107
- shouldPreserve,
10108
- normalizedDestRoot
10109
- );
10110
- if (wasSynced) {
10111
- synced++;
10112
- const relativeDest = getRelativePathBetween(
10113
- normalizedDestRoot,
10114
- destWithoutExample
10115
- );
10116
- log2.info(`Synced: ${relativeDest}`);
10117
- } else if (shouldPreserve && pathExists(destWithoutExample)) {
10118
- preserved++;
10119
- }
10120
- }
10228
+ const removal = await planRemoval(file, appRoot);
10229
+ if (!removal) continue;
10230
+ removed++;
10231
+ if (dryRun) {
10232
+ log2.info(`[DRY RUN] Global harness, would remove the template copy or entries: ${file.relativeDest}`);
10233
+ continue;
10121
10234
  }
10235
+ const destPath = joinPath(appRoot, file.relativeDest);
10236
+ if (removal.action === "delete") {
10237
+ removeHarnessFile(destPath, appRoot);
10238
+ } else {
10239
+ await write(destPath, removal.content, { format: "text", overwrite: true });
10240
+ }
10241
+ log2.info(`Global harness, removed the template copy or entries: ${file.relativeDest}`);
10122
10242
  } catch (error2) {
10123
10243
  errors++;
10124
10244
  log2.error(
10125
- `Error syncing pattern ${pattern}:`,
10245
+ `Error removing ${file.relativeDest}:`,
10126
10246
  DoNotDevError.from(
10127
10247
  error2,
10128
- `Failed to sync ${pattern}`,
10248
+ `Failed to remove ${file.relativeDest}`,
10129
10249
  "file-operation-error"
10130
10250
  )
10131
10251
  );
10132
10252
  }
10133
10253
  }
10134
- return { synced, preserved, errors };
10135
- }
10136
- async function syncDocumentation(options = {}) {
10137
- const { dryRun } = options;
10138
- if (dryRun) {
10139
- log2.info("\u{1F50D} DRY RUN MODE - No files will be modified");
10254
+ if (removed === 0 && errors === 0) {
10255
+ log2.info("Global harness: no harness copy in this app");
10140
10256
  }
10257
+ return errors > 0 ? 1 : 0;
10258
+ }
10259
+ async function syncHarness(options = {}) {
10141
10260
  const appRoot = getAppRoot();
10142
10261
  if (!appRoot) {
10143
10262
  log2.error("Could not determine project root");
10144
10263
  return 1;
10145
10264
  }
10146
10265
  const sourceRoot = getSourceTemplatesPath();
10147
- if (!sourceRoot) {
10148
- log2.error("Could not find framework templates. Documentation sync failed.");
10149
- log2.info("");
10150
- log2.info(" Templates are looked up the same way as create-project:");
10151
- log2.info(" \u2022 Published mode: the templates shipped with the running CLI");
10152
- log2.info(" \u2022 Dev mode: packages/cli/templates (from monorepo)");
10153
- log2.info("");
10154
- log2.info(" To enable docs sync:");
10155
- log2.info(" \u2022 Run the global CLI (dndev), not a copy inside the project");
10156
- log2.info(" \u2022 Or run from framework monorepo (dev mode)");
10157
- return 1;
10158
- }
10159
- log2.info(`Source templates: ${sourceRoot}`);
10160
- log2.info(`Destination: ${appRoot}`);
10161
- const result = await syncTemplates(sourceRoot, appRoot, dryRun || false);
10162
- if (dryRun) {
10163
- log2.info(`
10164
- [DRY RUN] Would sync ${result.synced} file(s)`);
10165
- if (result.preserved > 0) {
10166
- log2.info(
10167
- `[DRY RUN] Would preserve ${result.preserved} user-customized file(s)`
10168
- );
10169
- }
10170
- log2.info("[DRY RUN] No files modified");
10266
+ if (!sourceRoot) return 1;
10267
+ const files = await listTemplateFiles(sourceRoot, "harness");
10268
+ if (isGlobalHarness()) {
10269
+ return removeHarnessCopies(files, appRoot, options.dryRun ?? false);
10270
+ }
10271
+ const result = await syncTemplateFiles(files, appRoot, options);
10272
+ if (options.dryRun) {
10273
+ log2.info(`[DRY RUN] Would sync ${result.synced} harness file(s)`);
10274
+ return 0;
10275
+ }
10276
+ if (result.synced > 0) {
10277
+ log2.success(`Synced ${result.synced} harness file(s) from framework`);
10171
10278
  } else {
10172
- if (result.synced > 0) {
10173
- log2.success(`
10174
- \u2705 Synced ${result.synced} file(s) from framework`);
10175
- } else {
10176
- log2.info("\n\u2705 All documentation up to date");
10177
- }
10178
- if (result.preserved > 0) {
10179
- log2.info(`\u2139\uFE0F Preserved ${result.preserved} user-customized file(s)`);
10180
- }
10181
- if (result.errors > 0) {
10182
- log2.warn(`\u26A0\uFE0F ${result.errors} error(s) occurred during sync`);
10183
- return 1;
10184
- }
10279
+ log2.info("AI harness up to date");
10280
+ }
10281
+ if (result.errors > 0) {
10282
+ log2.warn(`${result.errors} harness file(s) could not be synced`);
10283
+ return 1;
10185
10284
  }
10186
10285
  return 0;
10187
10286
  }
@@ -10386,50 +10485,63 @@ async function main(options = {}) {
10386
10485
  log2.error("Could not determine project root");
10387
10486
  return 1;
10388
10487
  }
10389
- let updatePackages = false;
10390
- let updateDocs = false;
10391
- let updateDemoApp = false;
10392
- let updateCockpitApp = false;
10393
- if (dryRun) {
10394
- updatePackages = true;
10395
- updateDocs = true;
10396
- updateDemoApp = true;
10397
- updateCockpitApp = true;
10398
- } else if (options.yes) {
10399
- updatePackages = true;
10400
- updateDocs = true;
10401
- } else {
10402
- const selections = await askForMultiSelection(
10403
- "What would you like to update?",
10404
- [
10405
- {
10406
- title: "Packages",
10407
- value: "packages",
10408
- hint: "Update @donotdev/* packages and dependencies"
10409
- },
10410
- {
10411
- title: "Documentation",
10412
- value: "docs",
10413
- hint: "Sync guides, commands, and agents from framework"
10414
- },
10415
- {
10416
- title: "Demo app",
10417
- value: "demo",
10418
- hint: "Sync demo showcase app from latest template"
10419
- },
10420
- {
10421
- title: "Cockpit app",
10422
- value: "cockpit",
10423
- hint: "Sync dndev cockpit app from latest template"
10424
- }
10425
- ],
10426
- [0, 1]
10427
- );
10428
- updatePackages = selections.includes("packages");
10429
- updateDocs = selections.includes("docs");
10430
- updateDemoApp = selections.includes("demo");
10431
- updateCockpitApp = selections.includes("cockpit");
10488
+ const demoPresent = pathExists(joinPath(appRoot, "apps", "demo"));
10489
+ const dndevPresent = pathExists(joinPath(appRoot, "apps", "dndev"));
10490
+ const globalHarness = isGlobalHarness();
10491
+ const harnessLeftovers = globalHarness ? await listHarnessLeftovers() : [];
10492
+ const choices = [
10493
+ {
10494
+ title: "Packages",
10495
+ value: "packages",
10496
+ hint: "Update @donotdev/* packages and dependencies",
10497
+ selected: true
10498
+ },
10499
+ {
10500
+ title: "Documentation",
10501
+ value: "docs",
10502
+ hint: "Sync AGENTS.md, CLAUDE.md and rules from framework",
10503
+ selected: true
10504
+ }
10505
+ ];
10506
+ if (!globalHarness) {
10507
+ choices.push({
10508
+ title: "AI Harness",
10509
+ value: "harness",
10510
+ hint: "Sync agents, skills, hooks and MCP config from framework",
10511
+ selected: true
10512
+ });
10513
+ } else if (harnessLeftovers.length > 0) {
10514
+ choices.push({
10515
+ title: "AI Harness",
10516
+ value: "harness",
10517
+ hint: `Global harness: remove ${harnessLeftovers.length} leftover app copies`,
10518
+ selected: true
10519
+ });
10432
10520
  }
10521
+ choices.push(
10522
+ {
10523
+ title: "Demo app",
10524
+ value: "demo",
10525
+ hint: demoPresent ? "Sync apps/demo from latest template" : "Create apps/demo from latest template",
10526
+ selected: demoPresent
10527
+ },
10528
+ {
10529
+ title: "dndev app",
10530
+ value: "dndev",
10531
+ hint: dndevPresent ? "Sync apps/dndev (cockpit) from latest template" : "Create apps/dndev (cockpit) from latest template",
10532
+ selected: dndevPresent
10533
+ }
10534
+ );
10535
+ const selections = dryRun || options.yes ? choices.filter((c2) => c2.selected).map((c2) => c2.value) : await askForMultiSelection(
10536
+ "What would you like to update?",
10537
+ choices,
10538
+ choices.flatMap((c2, i2) => c2.selected ? [i2] : [])
10539
+ );
10540
+ const updatePackages = selections.includes("packages");
10541
+ const updateDocs = selections.includes("docs");
10542
+ const updateHarness = selections.includes("harness");
10543
+ const updateDemoApp = selections.includes("demo");
10544
+ const updateDndevApp = selections.includes("dndev");
10433
10545
  if (updatePackages) {
10434
10546
  const files = globSync("**/package.json", {
10435
10547
  cwd: appRoot,
@@ -10654,6 +10766,15 @@ async function main(options = {}) {
10654
10766
  log2.warn("Some documentation files could not be synced");
10655
10767
  }
10656
10768
  }
10769
+ if (updateHarness) {
10770
+ log2.info(
10771
+ globalHarness ? "Removing harness copies (global harness)..." : "Syncing AI harness..."
10772
+ );
10773
+ const harnessResult = await syncHarness({ dryRun });
10774
+ if (harnessResult !== 0 && !dryRun) {
10775
+ log2.warn("Some harness files could not be synced");
10776
+ }
10777
+ }
10657
10778
  if (updateDemoApp) {
10658
10779
  log2.info("Syncing demo app...");
10659
10780
  const result = await syncApp("demo", { dryRun });
@@ -10666,13 +10787,13 @@ async function main(options = {}) {
10666
10787
  log2.info(`Preserved ${result.preserved} customized file(s)`);
10667
10788
  }
10668
10789
  }
10669
- if (updateCockpitApp) {
10670
- log2.info("Syncing cockpit app...");
10790
+ if (updateDndevApp) {
10791
+ log2.info("Syncing dndev app...");
10671
10792
  const result = await syncApp("dndev", { dryRun });
10672
10793
  if (result.synced > 0) {
10673
10794
  log2.success(`Synced ${result.synced} file(s) in apps/dndev`);
10674
10795
  } else if (result.errors === 0) {
10675
- log2.info("Cockpit app up to date");
10796
+ log2.info("dndev app up to date");
10676
10797
  }
10677
10798
  if (result.preserved > 0) {
10678
10799
  log2.info(`Preserved ${result.preserved} customized file(s)`);