@dcdavidev/spellbookx 0.0.0-stage → 0.3.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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +135 -2
  3. package/dist/cli.d.ts +6 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +7 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/commands/init.d.ts +17 -0
  8. package/dist/commands/init.d.ts.map +1 -0
  9. package/dist/commands/init.js +310 -0
  10. package/dist/commands/init.js.map +1 -0
  11. package/dist/context.d.ts +55 -0
  12. package/dist/context.d.ts.map +1 -0
  13. package/dist/context.js +39 -0
  14. package/dist/context.js.map +1 -0
  15. package/dist/index.d.ts +25 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +18 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/lefthook.d.ts +24 -0
  20. package/dist/lefthook.d.ts.map +1 -0
  21. package/dist/lefthook.js +91 -0
  22. package/dist/lefthook.js.map +1 -0
  23. package/dist/merge/editorconfig.d.ts +25 -0
  24. package/dist/merge/editorconfig.d.ts.map +1 -0
  25. package/dist/merge/editorconfig.js +123 -0
  26. package/dist/merge/editorconfig.js.map +1 -0
  27. package/dist/merge/json.d.ts +21 -0
  28. package/dist/merge/json.d.ts.map +1 -0
  29. package/dist/merge/json.js +189 -0
  30. package/dist/merge/json.js.map +1 -0
  31. package/dist/merge/lines.d.ts +13 -0
  32. package/dist/merge/lines.d.ts.map +1 -0
  33. package/dist/merge/lines.js +36 -0
  34. package/dist/merge/lines.js.map +1 -0
  35. package/dist/merge/types.d.ts +23 -0
  36. package/dist/merge/types.d.ts.map +1 -0
  37. package/dist/merge/types.js +5 -0
  38. package/dist/merge/types.js.map +1 -0
  39. package/dist/merge/yaml.d.ts +26 -0
  40. package/dist/merge/yaml.d.ts.map +1 -0
  41. package/dist/merge/yaml.js +163 -0
  42. package/dist/merge/yaml.js.map +1 -0
  43. package/dist/output.d.ts +43 -0
  44. package/dist/output.d.ts.map +1 -0
  45. package/dist/output.js +39 -0
  46. package/dist/output.js.map +1 -0
  47. package/dist/package-manager.d.ts +53 -0
  48. package/dist/package-manager.d.ts.map +1 -0
  49. package/dist/package-manager.js +85 -0
  50. package/dist/package-manager.js.map +1 -0
  51. package/dist/pnpm-workspace.d.ts +33 -0
  52. package/dist/pnpm-workspace.d.ts.map +1 -0
  53. package/dist/pnpm-workspace.js +73 -0
  54. package/dist/pnpm-workspace.js.map +1 -0
  55. package/dist/run.d.ts +30 -0
  56. package/dist/run.d.ts.map +1 -0
  57. package/dist/run.js +83 -0
  58. package/dist/run.js.map +1 -0
  59. package/dist/tools.d.ts +135 -0
  60. package/dist/tools.d.ts.map +1 -0
  61. package/dist/tools.js +254 -0
  62. package/dist/tools.js.map +1 -0
  63. package/dist/tsconfig.tsbuildinfo +1 -0
  64. package/package.json +76 -3
  65. package/skills/spellbookx/SKILL.md +93 -0
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @file Main entry point for `@dcdavidev/spellbookx`.
3
+ * Exports the functions that the commands call, so that they can be run from
4
+ * code and tested.
5
+ */
6
+ export { init, INIT_HELP_TEXT } from './commands/init.js';
7
+ export type { Context, Exec } from './context.js';
8
+ export { processContext, spawnExec } from './context.js';
9
+ export type { LefthookOptions } from './lefthook.js';
10
+ export { buildLefthookConfig } from './lefthook.js';
11
+ export { mergeEditorconfig } from './merge/editorconfig.js';
12
+ export { mergeJson } from './merge/json.js';
13
+ export { createLinesMerge } from './merge/lines.js';
14
+ export type { Merge, MergeOptions } from './merge/types.js';
15
+ export { mergeYaml } from './merge/yaml.js';
16
+ export type { MessageKind, MessageStyle } from './output.js';
17
+ export { formatHelp, formatMessage, MESSAGE_STYLES } from './output.js';
18
+ export type { Command } from './package-manager.js';
19
+ export { getExecCommand, getInstallCommand, getPackageManager, getRunCommand, LOCK_FILES, } from './package-manager.js';
20
+ export type { PnpmListSettings } from './pnpm-workspace.js';
21
+ export { findPnpmWorkspace, mergePnpmSettings, PNPM_WORKSPACE_FILE, } from './pnpm-workspace.js';
22
+ export { COMMAND_ALIAS, COMMAND_NAME, getVersion, HELP_TEXT, run, } from './run.js';
23
+ export type { GenerateOptions, Tool, ToolFile, ToolFileSource, ToolName, } from './tools.js';
24
+ export { isToolName, TOOL_NAMES, TOOLS } from './tools.js';
25
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAC1D,YAAY,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzD,YAAY,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,YAAY,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACxE,YAAY,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,UAAU,GACX,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,aAAa,EACb,YAAY,EACZ,UAAU,EACV,SAAS,EACT,GAAG,GACJ,MAAM,UAAU,CAAC;AAClB,YAAY,EACV,eAAe,EACf,IAAI,EACJ,QAAQ,EACR,cAAc,EACd,QAAQ,GACT,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @file Main entry point for `@dcdavidev/spellbookx`.
3
+ * Exports the functions that the commands call, so that they can be run from
4
+ * code and tested.
5
+ */
6
+ export { init, INIT_HELP_TEXT } from './commands/init.js';
7
+ export { processContext, spawnExec } from './context.js';
8
+ export { buildLefthookConfig } from './lefthook.js';
9
+ export { mergeEditorconfig } from './merge/editorconfig.js';
10
+ export { mergeJson } from './merge/json.js';
11
+ export { createLinesMerge } from './merge/lines.js';
12
+ export { mergeYaml } from './merge/yaml.js';
13
+ export { formatHelp, formatMessage, MESSAGE_STYLES } from './output.js';
14
+ export { getExecCommand, getInstallCommand, getPackageManager, getRunCommand, LOCK_FILES, } from './package-manager.js';
15
+ export { findPnpmWorkspace, mergePnpmSettings, PNPM_WORKSPACE_FILE, } from './pnpm-workspace.js';
16
+ export { COMMAND_ALIAS, COMMAND_NAME, getVersion, HELP_TEXT, run, } from './run.js';
17
+ export { isToolName, TOOL_NAMES, TOOLS } from './tools.js';
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE1D,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAExE,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,EACb,UAAU,GACX,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,aAAa,EACb,YAAY,EACZ,UAAU,EACV,SAAS,EACT,GAAG,GACJ,MAAM,UAAU,CAAC;AAQlB,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @file Builds a `lefthook.yml` for the tools of a project.
3
+ */
4
+ import type { WorkspacePackageManager } from '@dcdavidev/shared-spells/discover-workspace-scopes';
5
+ /**
6
+ * What the generated configuration depends on.
7
+ */
8
+ export interface LefthookOptions {
9
+ /**
10
+ * The package manager that runs the tools.
11
+ */
12
+ manager: WorkspacePackageManager;
13
+ /**
14
+ * The tools of the project. Each one gets its job.
15
+ */
16
+ tools: ReadonlySet<'commitlint' | 'cspell' | 'eslint' | 'prettier'>;
17
+ }
18
+ /**
19
+ * Builds the content of a `lefthook.yml` that runs the spellbook tools.
20
+ * @param options - The package manager and the tools.
21
+ * @returns The YAML content.
22
+ */
23
+ export declare function buildLefthookConfig(options: LefthookOptions): string;
24
+ //# sourceMappingURL=lefthook.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lefthook.d.ts","sources":["../src/lefthook.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,oDAAoD,CAAC;AAIlG;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,OAAO,EAAE,uBAAuB,CAAC;IACjC;;OAEG;IACH,KAAK,EAAE,WAAW,CAAC,YAAY,GAAG,QAAQ,GAAG,QAAQ,GAAG,UAAU,CAAC,CAAC;CACrE;AAsBD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,eAAe,GAAG,MAAM,CAsEpE"}
@@ -0,0 +1,91 @@
1
+ /**
2
+ * @file Builds a `lefthook.yml` for the tools of a project.
3
+ */
4
+ import { getExecCommand, LOCK_FILES } from './package-manager.js';
5
+ /**
6
+ * Joins a command into one line.
7
+ * @param command - The command and its arguments.
8
+ * @param command.command - The program, for example `pnpm`.
9
+ * @param command.args - The arguments, for example `['exec', 'eslint']`.
10
+ * @returns The command line, for example `pnpm exec eslint`.
11
+ */
12
+ function line({ command, args }) {
13
+ return [command, ...args].join(' ');
14
+ }
15
+ /**
16
+ * Quotes a value for YAML with single quotes.
17
+ * @param value - The text to quote.
18
+ * @returns The quoted text, with inner single quotes doubled.
19
+ */
20
+ function quote(value) {
21
+ return `'${value.replaceAll("'", "''")}'`;
22
+ }
23
+ /**
24
+ * Builds the content of a `lefthook.yml` that runs the spellbook tools.
25
+ * @param options - The package manager and the tools.
26
+ * @returns The YAML content.
27
+ */
28
+ export function buildLefthookConfig(options) {
29
+ const { manager, tools } = options;
30
+ const run = (...args) => line(getExecCommand(manager, args));
31
+ const sections = [
32
+ `# Lefthook configuration
33
+ # Documentation: https://lefthook.dev/configuration/
34
+ #
35
+ # Skip every hook for one command: LEFTHOOK=0 git commit ...
36
+ # Skip one job: LEFTHOOK_EXCLUDE=cspell git commit ...
37
+
38
+ assert_lefthook_installed: true`,
39
+ ];
40
+ const preCommit = [];
41
+ if (tools.has('eslint')) {
42
+ preCommit.push(` - name: 'eslint'
43
+ run: ${quote(run('eslint', '--no-warn-ignored', '--fix', '{staged_files}'))}
44
+ stage_fixed: true`);
45
+ }
46
+ if (tools.has('prettier')) {
47
+ preCommit.push(` - name: 'prettier'
48
+ run: ${quote(run('prettier', '--ignore-unknown', '--write', '{staged_files}'))}
49
+ stage_fixed: true`);
50
+ }
51
+ if (tools.has('cspell')) {
52
+ preCommit.push(` - name: 'cspell'
53
+ run: ${quote(run('cspell', '--no-progress', '--no-summary', '--no-must-find-files', '{staged_files}'))}
54
+ fail_text: 'Fix the misspelling or add the word to the CSpell dictionary.'`);
55
+ }
56
+ if (preCommit.length > 0) {
57
+ sections.push(`# Fix and check the staged files before each commit. The jobs run in order so
58
+ # that Prettier formats what ESLint fixed, and spelling is checked last.
59
+ pre-commit:
60
+ piped: true
61
+ jobs:
62
+ ${preCommit.join('\n')}`);
63
+ }
64
+ if (tools.has('commitlint')) {
65
+ sections.push(`# Check the commit message against the Conventional Commits rules.
66
+ commit-msg:
67
+ jobs:
68
+ - name: 'commitlint'
69
+ run: ${quote(run('commitlint', '--edit', '{1}', '--verbose'))}
70
+ fail_text: 'Use Conventional Commits, for example "feat: add a page".'`);
71
+ }
72
+ // No pre-push hook: the full lint and the tests belong in CI.
73
+ const lockFile = LOCK_FILES[manager];
74
+ const install = `${manager} install`;
75
+ sections.push(`# Reinstall dependencies when a pull or merge changes the lock file.
76
+ post-merge:
77
+ jobs:
78
+ - name: 'install'
79
+ files: 'git diff --name-only HEAD@{1} HEAD'
80
+ glob: ${quote(lockFile)}
81
+ run: ${quote(install)}`, `# Reinstall dependencies when switching to a branch with a different lock file.
82
+ # {1} and {2} are the previous and new HEAD; {3} is 1 for a branch checkout.
83
+ post-checkout:
84
+ jobs:
85
+ - name: 'install'
86
+ run: >-
87
+ [ "{3}" != 1 ] || git diff --quiet {1} {2} -- ${lockFile} ||
88
+ ${install}`);
89
+ return `${sections.join('\n\n')}\n`;
90
+ }
91
+ //# sourceM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IAAI,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QACxB,SAAS,CAAC,IAAI,CAAC;aACN,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,eAAe,EAAE,cAAc,EAAE,sBAAsB,EAAE,gBAAgB,CAAC,CAAC;iFAC3B,CAAC,CAAC;IACjF,CAAC;IACD,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC;;;;;EAKhB,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC;IAED,IAAI,KAAK,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;QAC5B,QAAQ,CAAC,IAAI,CAAC;;;;aAIL,KAAK,CAAC,GAAG,CAAC,YAAY,EAAE,QAAQ,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC;6EACU,CAAC,CAAC;IAC7E,CAAC;IAED,8DAA8D;IAE9D,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;IACrC,MAAM,OAAO,GAAG,GAAG,OAAO,UAAU,CAAC;IACrC,QAAQ,CAAC,IAAI,CACX;;;;;cAKU,KAAK,CAAC,QAAQ,CAAC;aAChB,KAAK,CAAC,OAAO,CAAC,EAAE,EACzB;;;;;;wDAMoD,QAAQ;UACtD,OAAO,EAAE,CAChB,CAAC;IAEF,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;AACtC,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @file Merge of `.editorconfig` files, section by section and property by
3
+ * property.
4
+ *
5
+ * The existing file is edited line by line, so its comments, order and
6
+ * formatting are kept:
7
+ *
8
+ * - A missing section is added at the end, with the comments above it.
9
+ * - A missing property is added after the last property of its section.
10
+ * - A different property value is replaced only with `force`.
11
+ * - Properties before the first section, such as `root=true`, are handled as
12
+ * a section of their own.
13
+ */
14
+ import type { Merge } from './types.js';
15
+ /**
16
+ * Merges an incoming `.editorconfig` file into an existing one.
17
+ * @param existing - The content of the file that the project already has.
18
+ * @param incoming - The content of the file from the package.
19
+ * @param options - How to resolve conflicts.
20
+ * @param options.force - Whether the incoming value replaces a different
21
+ * existing value.
22
+ * @returns The merged content.
23
+ */
24
+ export declare const mergeEditorconfig: Merge;
25
+ //# sourceMappingURL=editorconfig.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"editorconfig.d.ts","sources":["../../src/merge/editorconfig.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AA4FxC;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,EAAE,KA8D/B,CAAC"}
@@ -0,0 +1,123 @@
1
+ /**
2
+ * @file Merge of `.editorconfig` files, section by section and property by
3
+ * property.
4
+ *
5
+ * The existing file is edited line by line, so its comments, order and
6
+ * formatting are kept:
7
+ *
8
+ * - A missing section is added at the end, with the comments above it.
9
+ * - A missing property is added after the last property of its section.
10
+ * - A different property value is replaced only with `force`.
11
+ * - Properties before the first section, such as `root=true`, are handled as
12
+ * a section of their own.
13
+ */
14
+ const SECTION = /^\s*\[(.*)\]\s*$/;
15
+ const PROPERTY = /^\s*([^#;=\s[][^=]*?)\s*=\s*(.*?)\s*$/;
16
+ const COMMENT = /^\s*[#;]/;
17
+ /**
18
+ * Splits an `.editorconfig` file into its sections.
19
+ * @param lines - The lines of the file.
20
+ * @returns The sections, in order. The first one holds the lines before the
21
+ * first `[glob]` line.
22
+ */
23
+ function parseSections(lines) {
24
+ const preamble = {
25
+ name: '',
26
+ header: -1,
27
+ commentStart: -1,
28
+ properties: new Map(),
29
+ last: -1,
30
+ };
31
+ const sections = [preamble];
32
+ let current = preamble;
33
+ for (const [index, line] of lines.entries()) {
34
+ const section = SECTION.exec(line);
35
+ if (section) {
36
+ let commentStart = index;
37
+ while (commentStart > 0 && COMMENT.test(lines[commentStart - 1] ?? '')) {
38
+ commentStart -= 1;
39
+ }
40
+ current = {
41
+ name: (section[1] ?? '').trim(),
42
+ header: index,
43
+ commentStart,
44
+ properties: new Map(),
45
+ last: index,
46
+ };
47
+ sections.push(current);
48
+ continue;
49
+ }
50
+ const property = PROPERTY.exec(line);
51
+ if (!property)
52
+ continue;
53
+ const key = (property[1] ?? '').toLowerCase();
54
+ current.properties.set(key, { line: index, value: property[2] ?? '' });
55
+ current.last = index;
56
+ }
57
+ return sections;
58
+ }
59
+ /**
60
+ * Merges an incoming `.editorconfig` file into an existing one.
61
+ * @param existing - The content of the file that the project already has.
62
+ * @param incoming - The content of the file from the package.
63
+ * @param options - How to resolve conflicts.
64
+ * @param options.force - Whether the incoming value replaces a different
65
+ * existing value.
66
+ * @returns The merged content.
67
+ */
68
+ export const mergeEditorconfig = (existing, incoming, { force }) => {
69
+ if (existing.trim() === '')
70
+ return incoming;
71
+ const lines = existing.replace(/\r?\n$/, '').split(/\r?\n/);
72
+ const incomingLines = incoming.replace(/\r?\n$/, '').split(/\r?\n/);
73
+ const sections = parseSections(lines);
74
+ const firstHeader = sections[1]?.header ?? lines.length;
75
+ /**
76
+ Lines to insert, keyed by the index that they go before.
77
+ */
78
+ const inserts = new Map();
79
+ const appended = [];
80
+ /**
81
+ * Queues lines to insert before a line.
82
+ * @param index - The line that the new lines go before.
83
+ * @param text - The lines to insert.
84
+ */
85
+ const insert = (index, text) => {
86
+ inserts.set(index, [...(inserts.get(index) ?? []), ...text]);
87
+ };
88
+ for (const section of parseSections(incomingLines)) {
89
+ const target = sections.find(({ name }) => name === section.name);
90
+ if (!target) {
91
+ appended.push('', ...incomingLines.slice(section.commentStart, section.last + 1));
92
+ continue;
93
+ }
94
+ for (const [key, property] of section.properties) {
95
+ const line = incomingLines[property.line]?.trim() ?? '';
96
+ const current = target.properties.get(key);
97
+ if (!current) {
98
+ if (target.header === -1 && target.last === -1) {
99
+ // A preamble without properties: add them before the first section.
100
+ insert(firstHeader, [
101
+ line,
102
+ ...(firstHeader < lines.length ? [''] : []),
103
+ ]);
104
+ }
105
+ else {
106
+ insert(target.last + 1, [line]);
107
+ }
108
+ }
109
+ else if (force && current.value !== property.value) {
110
+ lines[current.line] = line;
111
+ }
112
+ }
113
+ }
114
+ const result = [];
115
+ for (const [index, line] of [...lines, undefined].entries()) {
116
+ result.push(...(inserts.get(index) ?? []));
117
+ if (line !== undefined)
118
+ result.push(line);
119
+ }
120
+ result.push(...appended);
121
+ return `${result.join('\n')}\n`;
122
+ };
123
+ //# sourceMappingURL=editorconfig.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"editorconfig.js","sources":["../../src/merge/editorconfig.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AA4CH,MAAM,OAAO,GAAG,kBAAkB,CAAC;AACnC,MAAM,QAAQ,GAAG,uCAAuC,CAAC;AACzD,MAAM,OAAO,GAAG,UAAU,CAAC;AAE3B;;;;;GAKG;AACH,SAAS,aAAa,CAAC,KAAwB;IAC7C,MAAM,QAAQ,GAAY;QACxB,IAAI,EAAE,EAAE;QACR,MAAM,EAAE,CAAC,CAAC;QACV,YAAY,EAAE,CAAC,CAAC;QAChB,UAAU,EAAE,IAAI,GAAG,EAAE;QACrB,IAAI,EAAE,CAAC,CAAC;KACT,CAAC;IACF,MAAM,QAAQ,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC5B,IAAI,OAAO,GAAG,QAAQ,CAAC;IAEvB,KAAK,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;QAC5C,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnC,IAAI,OAAO,EAAE,CAAC;YACZ,IAAI,YAAY,GAAG,KAAK,CAAC;YACzB,OAAO,YAAY,GAAG,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,CAAC,CAAC,IAAI,EAA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CAAC,IAAI,KAAK,CAAC,CAAC,EAAE,CAAC;oBAC/C,oEAAoE;oBACpE,MAAM,CAAC,WAAW,EAAE;wBAClB,IAAI;wBACJ,GAAG,CAAC,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;qBAC5C,CAAC,CAAC;gBACL,CAAC;qBAAM,CAAC;oBACN,MAAM,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;gBAClC,CAAC;YACH,CAAC;iBAAM,IAAI,KAAK,IAAI,OAAO,CAAC,KAAK,KAAK,QAAQ,CAAC,KAAK,EAAE,CAAC;gBACrD,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;YAC7B,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,KAAK,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,KAAK,EAAE,SAAS,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;QAC5D,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;QAC3C,IAAI,IAAI,KAAK,SAAS;YAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC5C,CAAC;IACD,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC;IAEzB,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;AAClC,CAAC,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @file Deep merge of JSON with comments (JSONC) files, such as the VS Code
3
+ * `settings.json` and `extensions.json`.
4
+ *
5
+ * The existing file is edited in place, so its comments, order and
6
+ * formatting are kept. Only the missing (or, with `force`, the different)
7
+ * values are written, with the indentation of the lines around them.
8
+ */
9
+ import type { Merge } from './types.js';
10
+ /**
11
+ * Deep merges an incoming JSONC file into an existing one.
12
+ * @param existing - The content of the file that the project already has.
13
+ * @param incoming - The content of the file from the package.
14
+ * @param options - How to resolve conflicts.
15
+ * @param options.force - Whether the incoming value replaces a different
16
+ * existing value.
17
+ * @returns The merged content.
18
+ * @throws {Error} When a file is not valid JSONC or does not hold an object.
19
+ */
20
+ export declare const mergeJson: Merge;
21
+ //# sourceMappingURL=json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.d.ts","sources":["../../src/merge/json.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAcH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAiMxC;;;;;;;;;GASG;AACH,eAAO,MAAM,SAAS,EAAE,KAUvB,CAAC"}
@@ -0,0 +1,189 @@
1
+ /**
2
+ * @file Deep merge of JSON with comments (JSONC) files, such as the VS Code
3
+ * `settings.json` and `extensions.json`.
4
+ *
5
+ * The existing file is edited in place, so its comments, order and
6
+ * formatting are kept. Only the missing (or, with `force`, the different)
7
+ * values are written, with the indentation of the lines around them.
8
+ */
9
+ import { isDeepStrictEqual } from 'node:util';
10
+ import { findNodeAtLocation, parse, parseTree, printParseErrorCode, } from 'jsonc-parser';
11
+ /**
12
+ * Checks whether a value is a plain object (not an array and not `null`).
13
+ * @param value - The value to check.
14
+ * @returns `true` if `value` is a plain object.
15
+ */
16
+ function isObject(value) {
17
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
18
+ }
19
+ /**
20
+ * Parses a JSONC text, allowing comments and trailing commas.
21
+ * @param text - The text to parse.
22
+ * @param label - The name of the text, used in the error message.
23
+ * @returns The parsed value.
24
+ * @throws {Error} When the text is not valid JSONC.
25
+ */
26
+ function parseJsonc(text, label) {
27
+ const errors = [];
28
+ const value = parse(text, errors, {
29
+ allowTrailingComma: true,
30
+ disallowComments: false,
31
+ });
32
+ const [error] = errors;
33
+ if (error) {
34
+ throw new Error(`${label} is not valid JSON: ${printParseErrorCode(error.error)} at offset ${error.offset}`);
35
+ }
36
+ return value;
37
+ }
38
+ /**
39
+ * Finds the node at a path of a JSONC text.
40
+ * @param text - The JSONC source to search.
41
+ * @param path - The keys and indexes that lead to the node.
42
+ * @returns The syntax tree node at that location.
43
+ * @throws {Error} When there is no node at the path.
44
+ */
45
+ function nodeAt(text, path) {
46
+ const root = parseTree(text, [], { allowTrailingComma: true });
47
+ const node = root && findNodeAtLocation(root, path);
48
+ if (!node)
49
+ throw new Error(`no value at ${JSON.stringify(path)}`);
50
+ return node;
51
+ }
52
+ /**
53
+ * Returns the indentation of the line that contains an offset.
54
+ * @param text - The JSONC source.
55
+ * @param offset - A character position inside the line.
56
+ * @returns The spaces and tabs at the start of the line.
57
+ */
58
+ function indentationAt(text, offset) {
59
+ const lineStart = text.lastIndexOf('\n', offset - 1) + 1;
60
+ return /^[\t ]*/.exec(text.slice(lineStart))?.[0] ?? '';
61
+ }
62
+ /**
63
+ * Serializes a value, indenting its lines after the first one.
64
+ * @param value - Any JSON-serializable data.
65
+ * @param indent - The indentation of the line that the value starts on.
66
+ * @param isInline - Whether to write the value on one line.
67
+ * @returns The JSON text of the value.
68
+ */
69
+ function stringify(value, indent, isInline) {
70
+ return isInline
71
+ ? JSON.stringify(value, undefined, 1).replaceAll(/\n\s*/g, ' ')
72
+ : JSON.stringify(value, undefined, 2).split('\n').join(`\n${indent}`);
73
+ }
74
+ /**
75
+ * Inserts a new last element into an object or an array.
76
+ * @param text - The JSONC source to edit.
77
+ * @param container - The object or array node.
78
+ * @param element - Builds the text of the element from its indentation and
79
+ * whether it goes on the same line as its container.
80
+ * @returns The updated text.
81
+ */
82
+ function appendElement(text, container, element) {
83
+ const start = container.offset;
84
+ const end = container.offset + container.length - 1; // the `}` or `]`
85
+ const isInline = !text.slice(start, end).includes('\n');
86
+ const last = container.children?.at(-1);
87
+ if (!last) {
88
+ if (isInline) {
89
+ return `${text.slice(0, start + 1)}${element('', true)}${text.slice(end)}`;
90
+ }
91
+ const indent = `${indentationAt(text, start)} `;
92
+ const body = text.slice(start + 1, end).trimEnd();
93
+ return `${text.slice(0, start + 1)}${body}\n${indent}${element(indent, false)}\n${indentationAt(text, end)}${text.slice(end)}`;
94
+ }
95
+ const lastEnd = last.offset + last.length;
96
+ let position = lastEnd;
97
+ while (text[position] === ' ' || text[position] === '\t')
98
+ position += 1;
99
+ const hasTrailingComma = text[position] === ',';
100
+ if (hasTrailingComma)
101
+ position += 1;
102
+ if (isInline) {
103
+ // Insert right after the last element, or after its trailing comma.
104
+ const at = hasTrailingComma ? position : lastEnd;
105
+ const separator = hasTrailingComma ? ' ' : ', ';
106
+ const after = hasTrailingComma ? ',' : '';
107
+ return `${text.slice(0, at)}${separator}${element('', true)}${after}${text.slice(at)}`;
108
+ }
109
+ // Go to the end of the line, after a comment that follows the last element.
110
+ const rest = text.slice(position);
111
+ const lineComment = /^[\t ]*(?:\/\/[^\n]*|\/\*.*?\*\/[\t ]*)?/.exec(rest);
112
+ const lineEnd = position + (lineComment?.[0].length ?? 0);
113
+ const indent = indentationAt(text, last.offset);
114
+ const inserted = `\n${indent}${element(indent, false)}${hasTrailingComma ? ',' : ''}`;
115
+ const comma = hasTrailingComma ? '' : ',';
116
+ return `${text.slice(0, lastEnd)}${comma}${text.slice(lastEnd, lineEnd)}${inserted}${text.slice(lineEnd)}`;
117
+ }
118
+ /**
119
+ * Replaces the value of a node.
120
+ * @param text - The JSONC source to edit.
121
+ * @param node - The syntax tree node whose text is replaced.
122
+ * @param value - The data written in its place.
123
+ * @returns The updated text.
124
+ */
125
+ function replaceNode(text, node, value) {
126
+ const end = node.offset + node.length;
127
+ const isInline = !text.slice(node.offset, end).includes('\n');
128
+ const json = stringify(value, indentationAt(text, node.offset), isInline);
129
+ return `${text.slice(0, node.offset)}${json}${text.slice(end)}`;
130
+ }
131
+ /**
132
+ * Merges an incoming value into the existing value found at `path`.
133
+ *
134
+ * - Objects are merged key by key.
135
+ * - Arrays get the incoming items that they do not contain yet, at the end.
136
+ * - A missing value is added.
137
+ * - A different value is replaced only with `isForced`.
138
+ * @param text - The current text of the file.
139
+ * @param path - The location of the value in the file.
140
+ * @param existing - The value at `path` in the existing file.
141
+ * @param incoming - The value at `path` in the incoming file.
142
+ * @param isForced - Whether a different value is replaced.
143
+ * @returns The updated text.
144
+ */
145
+ function mergeValue(text, path, existing, incoming, isForced) {
146
+ if (isObject(existing) && isObject(incoming)) {
147
+ let result = text;
148
+ for (const [key, value] of Object.entries(incoming)) {
149
+ result = Object.hasOwn(existing, key)
150
+ ? mergeValue(result, [...path, key], existing[key], value, isForced)
151
+ : appendElement(result, nodeAt(result, path), (indent, isInline) => `${JSON.stringify(key)}: ${stringify(value, indent, isInline)}`);
152
+ }
153
+ return result;
154
+ }
155
+ if (Array.isArray(existing) && Array.isArray(incoming)) {
156
+ let result = text;
157
+ for (const item of incoming) {
158
+ if (existing.some((current) => isDeepStrictEqual(current, item))) {
159
+ continue;
160
+ }
161
+ result = appendElement(result, nodeAt(result, path), (indent, isInline) => stringify(item, indent, isInline));
162
+ }
163
+ return result;
164
+ }
165
+ return isForced && !isDeepStrictEqual(existing, incoming)
166
+ ? replaceNode(text, nodeAt(text, path), incoming)
167
+ : text;
168
+ }
169
+ /**
170
+ * Deep merges an incoming JSONC file into an existing one.
171
+ * @param existing - The content of the file that the project already has.
172
+ * @param incoming - The content of the file from the package.
173
+ * @param options - How to resolve conflicts.
174
+ * @param options.force - Whether the incoming value replaces a different
175
+ * existing value.
176
+ * @returns The merged content.
177
+ * @throws {Error} When a file is not valid JSONC or does not hold an object.
178
+ */
179
+ export const mergeJson = (existing, incoming, { force }) => {
180
+ const next = parseJsonc(incoming, 'the package file');
181
+ if (existing.trim() === '')
182
+ return incoming;
183
+ const current = parseJsonc(existing, 'the existing file');
184
+ if (!isObject(current)) {
185
+ throw new Error('the existing file does not hold a JSON object');
186
+ }
187
+ return mergeValue(existing, [], current, next, force);
188
+ };
189
+ //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,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AACxD,CAAC,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @file Merge of files with one entry per line, such as `.prettierignore`.
3
+ */
4
+ import type { Merge } from './types.js';
5
+ /**
6
+ * Creates a merge that adds the incoming entries that the existing file does
7
+ * not have, at its end and in their order, under a comment.
8
+ * @param heading - The comment written above the added entries, for example
9
+ * `# @dcdavidev/prettier-config`.
10
+ * @returns The merge. It ignores `force`, because entries have no values.
11
+ */
12
+ export declare function createLinesMerge(heading: string): Merge;
13
+ //# sourceMappingURL=lines.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lines.d.ts","sources":["../../src/merge/lines.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAWxC;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,CAmBvD"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @file Merge of files with one entry per line, such as `.prettierignore`.
3
+ */
4
+ /**
5
+ * Checks whether a line is an entry: not empty and not a `#` comment.
6
+ * @param line - The line, trimmed.
7
+ * @returns `true` if the line is an entry.
8
+ */
9
+ function isEntry(line) {
10
+ return line !== '' && !line.startsWith('#');
11
+ }
12
+ /**
13
+ * Creates a merge that adds the incoming entries that the existing file does
14
+ * not have, at its end and in their order, under a comment.
15
+ * @param heading - The comment written above the added entries, for example
16
+ * `# @dcdavidev/prettier-config`.
17
+ * @returns The merge. It ignores `force`, because entries have no values.
18
+ */
19
+ export function createLinesMerge(heading) {
20
+ return (existing, incoming) => {
21
+ if (existing.trim() === '')
22
+ return incoming;
23
+ const present = new Set(existing.split(/\r?\n/).map((line) => line.trim()));
24
+ const missing = [
25
+ ...new Set(incoming
26
+ .split(/\r?\n/)
27
+ .map((line) => line.trim())
28
+ .filter((line) => isEntry(line) && !present.has(line))),
29
+ ];
30
+ if (missing.length === 0)
31
+ return existing;
32
+ const base = existing.replace(/\s*$/, '');
33
+ return `${base}\n\n${heading}\n${missing.join('\n')}\n`;
34
+ };
35
+ }
36
+ //# sourceMappingURL=lines.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lines.js","sources":["../../src/merge/lines.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;;;GAIG;AACH,SAAS,OAAO,CAAC,IAAY;IAC3B,OAAO,IAAI,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;AAC9C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAe;IAC9C,OAAO,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE;QAC5B,IAAI,QAAQ,CAAC,IAAI,EAAE,KAAK,EAAE;YAAE,OAAO,QAAQ,CAAC;QAE5C,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;QAC5E,MAAM,OAAO,GAAG;YACd,GAAG,IAAI,GAAG,CACR,QAAQ;iBACL,KAAK,CAAC,OAAO,CAAC;iBACd,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;iBAC1B,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CACzD;SACF,CAAC;QAEF,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,QAAQ,CAAC;QAE1C,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAC1C,OAAO,GAAG,IAAI,OAAO,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;IAC1D,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @file Shared types of the configuration file mergers.
3
+ */
4
+ /**
5
+ * Options of a merge.
6
+ */
7
+ export interface MergeOptions {
8
+ /**
9
+ * When `true`, the incoming value replaces a different existing value.
10
+ * When `false`, the existing value is kept.
11
+ */
12
+ force: boolean;
13
+ }
14
+ /**
15
+ * Merges the content of an incoming configuration file into an existing one.
16
+ * @param existing - The content of the file that the project already has.
17
+ * @param incoming - The content of the file from the package.
18
+ * @param options - How to resolve conflicts.
19
+ * @returns The merged content. It is equal to `existing` when nothing had to
20
+ * change.
21
+ */
22
+ export type Merge = (existing: string, incoming: string, options: MergeOptions) => string;
23
+ //# sourceMappingURL=types.d.ts.map