@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
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Davide Di Criscito
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,136 @@
1
- # Temporary Holding Version
1
+ # @dcdavidev/spellbookx
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The spellbook command-line interface. It installs two commands that do the same thing: `spellbookx` and its short alias `sbx`.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add -D @dcdavidev/spellbookx
9
+ ```
10
+
11
+ Or run it without installing it:
12
+
13
+ ```sh
14
+ pnpm dlx @dcdavidev/spellbookx init eslint prettier
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```sh
20
+ spellbookx < command > [options]
21
+ sbx < command > [options]
22
+ ```
23
+
24
+ | Option | Description |
25
+ | ----------------- | -------------------------- |
26
+ | `-h`, `--help` | Show the help and exit. |
27
+ | `-v`, `--version` | Show the version and exit. |
28
+
29
+ With no command, `spellbookx` shows the help. Run `spellbookx <command> --help` for the options of a command. An unknown command or option prints an error and the help, and exits with code `1`.
30
+
31
+ ## `init`
32
+
33
+ Sets up the spellbook configuration of one or more tools in the current folder:
34
+
35
+ ```sh
36
+ sbx init eslint prettier cspell
37
+ ```
38
+
39
+ Run it in the project root, next to `package.json`. For each tool, `init` installs the packages as development dependencies and creates the configuration files:
40
+
41
+ | Tool | Installs | Creates |
42
+ | -------------- | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
43
+ | `commitlint` | `@dcdavidev/commitlint-config`, `@commitlint/cli`, `@commitlint/config-conventional` | `commitlint.config.mjs` |
44
+ | `cspell` | `@dcdavidev/cspell-config`, `cspell` | `cspell.config.mjs` |
45
+ | `cz` or `czg` | `@dcdavidev/cz-config`, `czg` | `cz.config.mjs` |
46
+ | `editorconfig` | `@dcdavidev/editorconfig` | `.editorconfig`, copied from the package |
47
+ | `eslint` | `@dcdavidev/eslint-plugin`, `eslint`, `typescript` | `eslint.config.mjs` |
48
+ | `lefthook` | `lefthook` | `lefthook.yml` for the tools of the project, and the `prepare` script |
49
+ | `prettier` | `@dcdavidev/prettier-config`, `prettier` | `prettier.config.mjs` and `.prettierignore`, from the package |
50
+ | `vscode` | `@dcdavidev/vscode-config` | `.vscode/settings.json` and `.vscode/extensions.json`, copied from the package |
51
+
52
+ With pnpm, `prettier` also adds `*prettier-plugin-*` and `@prettier/plugin-*` to the `publicHoistPattern` of `pnpm-workspace.yaml`, so that Prettier can find its plugins. It uses the first `pnpm-workspace.yaml` found in the current folder or a parent folder, or creates one in the current folder. Missing items are added to an existing list, and the comments of the file are kept. The file is written before the install, so pnpm hoists the plugins right away. With `--skip-install`, run `pnpm install` afterwards.
53
+
54
+ `lefthook` writes the Git hooks of the spellbook repository, adapted to the project:
55
+
56
+ - `pre-commit` fixes and checks the staged files with ESLint, Prettier and CSpell, and `commit-msg` checks the message with Commitlint. Each job is added only for a tool named after `init` or already in the dependencies of `package.json`.
57
+ - There is no `pre-push` hook: run the full lint and the tests in CI instead.
58
+ - `post-merge` and `post-checkout` reinstall the dependencies when the lock file changes.
59
+
60
+ The commands use the package manager of the project, for example `pnpm exec eslint` or `npx --no eslint`. `init` also adds `"prepare": "lefthook install"` to the scripts of `package.json`, so that the hooks are installed with the dependencies, and runs `lefthook install` after the install.
61
+
62
+ The packages of every tool are installed with one command. The package manager (pnpm, Yarn, npm or Bun) is detected from the `packageManager` field of `package.json` or from the lock files, and npm is used when none is found. In the root of a pnpm workspace, `-w` is added.
63
+
64
+ | Option | Description |
65
+ | ---------------- | ------------------------------------------------- |
66
+ | `-f`, `--force` | Overwrite configuration files that already exist. |
67
+ | `--skip-install` | Create the files without installing the packages. |
68
+ | `-h`, `--help` | Show the help and exit. |
69
+
70
+ ### Existing Configuration
71
+
72
+ `init` never throws away a configuration that you already have.
73
+
74
+ These files are **merged** with the files of the package when they exist. Your values, comments and formatting are kept, and only what is missing is added:
75
+
76
+ | File | How it is merged |
77
+ | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
78
+ | `.editorconfig` | Missing sections are added at the end, and missing properties at the end of their section. `root=true` is added first. |
79
+ | `.vscode/settings.json` | Deep merge: missing settings are added, objects are merged key by key, and missing array items are added at the end. |
80
+ | `.vscode/extensions.json` | The same deep merge, so missing recommendations are added after yours. |
81
+ | `lefthook.yml` | Deep merge: missing hooks and settings are added. Jobs are matched by `name`, and jobs with a new name are added at the end. An existing `lefthook.yaml`, `.lefthook.yml` or `.config/lefthook.yml` is merged instead of creating `lefthook.yml`. |
82
+ | `package.json` | The `prepare` script is added if it is missing. An existing `prepare` script is kept. |
83
+ | `.prettierignore` | Missing patterns are added at the end, under a `# @dcdavidev/prettier-config` comment. |
84
+
85
+ With `--force`, a merge also replaces the values that differ from the package (for example `indent_size=4` becomes `indent_size=2`). Without it, your values win. Running `init` again changes nothing when the files are up to date.
86
+
87
+ The other configuration files are kept as they are:
88
+
89
+ - If the file that `init` would create exists, it is kept. Pass `--force` to overwrite it.
90
+ - If the tool is configured in another file, for example `.prettierrc.json`, `eslint.config.ts` or `lefthook.toml`, or in a `package.json` field (`prettier`, `commitlint`), no file is created, even with `--force`.
91
+
92
+ Each file is reported, followed by a summary. The command exits with code `1` if the install fails, or if a file cannot be created or merged (for example a `settings.json` that is not valid JSON, which is then left unchanged).
93
+
94
+ `editorconfig`, `prettier` and `vscode` read their files from the installed packages, so with `--skip-install` those packages must already be installed. With `--skip-install`, `lefthook` does not run `lefthook install`: it tells you to run it.
95
+
96
+ ### Output
97
+
98
+ Messages start with an emoji and are colored with [chalk](https://github.com/chalk/chalk): 📦 install, ✨ created, 🔀 merged, ♻️ overwritten, 👌 already up to date, ⏭️ skipped, ❌ error. Colors are turned off when the output is not a terminal, or when `NO_COLOR` is set. Set `FORCE_COLOR=1` to keep them.
99
+
100
+ ## Use It From Code
101
+
102
+ The package exports the `run` function that the commands call. It takes the arguments and returns the exit code:
103
+
104
+ ```ts
105
+ import { run } from '@dcdavidev/spellbookx';
106
+
107
+ const exitCode = run(['init', 'eslint', '--skip-install']);
108
+ ```
109
+
110
+ Pass a context as the second argument to choose the working folder (`cwd`), collect the output (`stdout`, `stderr`), replace the way programs are run (`exec`) and set the colors (`chalk`). The tool definitions are exported as `TOOLS`, the mergers as `mergeJson`, `mergeYaml`, `mergeEditorconfig` and `createLinesMerge`, and the Lefthook builder as `buildLefthookConfig`.
111
+
112
+ ## Agent Skill
113
+
114
+ The package ships an [agent skill](https://agentskills.io) that teaches coding agents, such as Claude Code, Codex and Cursor, when and how to run `sbx init`. Install it in a project with the [`skills`](https://github.com/vercel-labs/skills) CLI:
115
+
116
+ ```sh
117
+ npx skills add dcdavidev/spellbook
118
+ ```
119
+
120
+ The CLI asks which agents to install it for. To choose them on the command line:
121
+
122
+ ```sh
123
+ npx skills add dcdavidev/spellbook --skill spellbookx -a claude-code -a codex
124
+ ```
125
+
126
+ It writes the skill to `.agents/skills/spellbookx/`, which Codex, Cursor and most other agents read, and links it into `.claude/skills/` for Claude Code. Add `-g` to install it for your user instead of the project.
127
+
128
+ The skill is in [`skills/spellbookx/SKILL.md`](./skills/spellbookx/SKILL.md), and it is also published with the npm package.
129
+
130
+ ## Requirements
131
+
132
+ Node.js 26 or later.
133
+
134
+ ## License
135
+
136
+ This project is open-source software licensed under the [MIT License](https://github.com/dcdavidev/spellbook/blob/main/LICENSE).
package/dist/cli.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * @file Executable of the `spellbookx` and `sbx` commands.
4
+ */
5
+ export {};
6
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;GAEG"}
package/dist/cli.js ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * @file Executable of the `spellbookx` and `sbx` commands.
4
+ */
5
+ import { run } from './run.js';
6
+ process.exitCode = run(process.argv.slice(2));
7
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;GAEG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,OAAO,CAAC,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @file The `spellbookx init` command: installs the spellbook configurations
3
+ * of the chosen tools and creates their configuration files.
4
+ */
5
+ import type { Context } from '../context.js';
6
+ /**
7
+ * Text printed by `spellbookx init --help`.
8
+ */
9
+ export declare const INIT_HELP_TEXT: string;
10
+ /**
11
+ * Runs `spellbookx init`.
12
+ * @param args - The arguments after `init`.
13
+ * @param context - The environment of the command.
14
+ * @returns The exit code: `0` on success, `1` on an error.
15
+ */
16
+ export declare function init(args: readonly string[], context: Context): number;
17
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.d.ts","sources":["../../src/commands/init.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAiB7C;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,MAgB5B,CAAC;AAmIF;;;;;GAKG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,OAAO,EAAE,OAAO,GAAG,MAAM,CA+LtE"}
@@ -0,0 +1,310 @@
1
+ /**
2
+ * @file The `spellbookx init` command: installs the spellbook configurations
3
+ * of the chosen tools and creates their configuration files.
4
+ */
5
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
6
+ import { createRequire } from 'node:module';
7
+ import path from 'node:path';
8
+ import { parseArgs } from 'node:util';
9
+ import { formatHelp, formatMessage } from '../output.js';
10
+ import { getExecCommand, getInstallCommand, getPackageManager, } from '../package-manager.js';
11
+ import { findPnpmWorkspace, mergePnpmSettings } from '../pnpm-workspace.js';
12
+ import { isToolName, TOOL_NAMES, TOOLS, } from '../tools.js';
13
+ /**
14
+ * Text printed by `spellbookx init --help`.
15
+ */
16
+ export const INIT_HELP_TEXT = `Usage: spellbookx init <tool...> [options]
17
+
18
+ Installs the spellbook configuration of each tool and creates its
19
+ configuration files in the current folder. Existing files are kept, except
20
+ .editorconfig, .prettierignore, lefthook.yml, the .vscode/ files and the
21
+ prepare script of package.json, which are merged: what is missing is added.
22
+
23
+ Tools:
24
+ ${TOOL_NAMES.join(', ')}
25
+ (cz and czg are the same tool)
26
+
27
+ Options:
28
+ -f, --force Overwrite configuration files that already exist, and
29
+ use the package values when a merged value differs
30
+ --skip-install Create the files without installing the packages
31
+ -h, --help Show this help and exit
32
+ `;
33
+ /**
34
+ * Reads the `package.json` of the project.
35
+ * @param cwd - The project folder.
36
+ * @returns The parsed file, or `undefined` if it is missing or invalid.
37
+ */
38
+ function readPackageJson(cwd) {
39
+ try {
40
+ return JSON.parse(readFileSync(path.join(cwd, 'package.json'), 'utf8'));
41
+ }
42
+ catch {
43
+ return undefined;
44
+ }
45
+ }
46
+ /**
47
+ * Finds a file of the project that already does the job of a tool file.
48
+ * @param file - The tool file.
49
+ * @param cwd - The project folder.
50
+ * @param packageJson - The `package.json` of the project.
51
+ * @returns The file (or `package.json` field) that already exists, or
52
+ * `undefined` if there is none.
53
+ */
54
+ function findExisting(file, cwd, packageJson) {
55
+ const candidates = [
56
+ file.path,
57
+ ...(file.mergeInto ?? []),
58
+ ...file.alternatives,
59
+ ];
60
+ const existing = candidates.find((name) => existsSync(path.join(cwd, name)));
61
+ if (existing)
62
+ return existing;
63
+ if (file.packageJsonField &&
64
+ Object.hasOwn(packageJson, file.packageJsonField)) {
65
+ return `the "${file.packageJsonField}" field of package.json`;
66
+ }
67
+ return undefined;
68
+ }
69
+ /**
70
+ * Finds a package in the `node_modules` folders of a project, from `cwd` up
71
+ * to the root of the file system.
72
+ *
73
+ * Unlike `require.resolve`, it does not look in `NODE_PATH` or in the global
74
+ * folders. pnpm sets `NODE_PATH` for the binaries that it runs, so
75
+ * `require.resolve` could find another copy of the package than the one that
76
+ * the project installed.
77
+ * @param cwd - The project folder.
78
+ * @param packageName - The name of the package, for example
79
+ * `@dcdavidev/editorconfig`.
80
+ * @returns The folder of the package, or `undefined` if it is not installed.
81
+ */
82
+ function findInstalledPackage(cwd, packageName) {
83
+ let dir = path.resolve(cwd);
84
+ for (;;) {
85
+ const candidate = path.join(dir, 'node_modules', packageName);
86
+ if (existsSync(path.join(candidate, 'package.json')))
87
+ return candidate;
88
+ const parent = path.dirname(dir);
89
+ if (parent === dir)
90
+ return undefined;
91
+ dir = parent;
92
+ }
93
+ }
94
+ /**
95
+ * Reads the content of a tool file.
96
+ * @param file - The tool file.
97
+ * @param cwd - The project folder, where the packages are installed.
98
+ * @param options - What a generated file depends on.
99
+ * @returns The content of the file.
100
+ */
101
+ function readContent(file, cwd, options) {
102
+ const { source } = file;
103
+ if ('content' in source)
104
+ return source.content;
105
+ if ('generate' in source)
106
+ return source.generate(options);
107
+ const specifier = 'copyFrom' in source ? source.copyFrom : source.exportFrom.module;
108
+ const packageName = specifier.split('/').slice(0, 2).join('/');
109
+ const packageDir = findInstalledPackage(cwd, packageName);
110
+ let resolved;
111
+ if (packageDir) {
112
+ // Resolve from inside the package, which refers to itself through its
113
+ // `exports`, so that the copy installed in the project is used.
114
+ const require = createRequire(path.join(packageDir, 'package.json'));
115
+ try {
116
+ resolved = require.resolve(specifier);
117
+ }
118
+ catch {
119
+ resolved = undefined;
120
+ }
121
+ }
122
+ if (!resolved) {
123
+ throw new Error(`${packageName} is not installed. Install it, or run the command without --skip-install.`);
124
+ }
125
+ if ('copyFrom' in source)
126
+ return readFileSync(resolved, 'utf8');
127
+ // Node.js 22 and later can require ECMAScript modules synchronously.
128
+ const require = createRequire(path.join(cwd, 'package.json'));
129
+ const module = require(resolved);
130
+ const content = module[source.exportFrom.name];
131
+ if (typeof content !== 'string') {
132
+ throw new TypeError(`${specifier} does not export a string named ${source.exportFrom.name}`);
133
+ }
134
+ return content;
135
+ }
136
+ /**
137
+ * Runs `spellbookx init`.
138
+ * @param args - The arguments after `init`.
139
+ * @param context - The environment of the command.
140
+ * @returns The exit code: `0` on success, `1` on an error.
141
+ */
142
+ export function init(args, context) {
143
+ const { cwd, stdout, stderr, exec, chalk } = context;
144
+ const say = (kind, text) => (kind === 'error' ? stderr : stdout)(formatMessage(chalk, kind, text));
145
+ const help = formatHelp(chalk, INIT_HELP_TEXT);
146
+ const fail = (message, isHelpShown = false) => {
147
+ say('error', `spellbookx init: ${message}`);
148
+ if (isHelpShown)
149
+ stderr(`\n${help}`);
150
+ return 1;
151
+ };
152
+ let parsed;
153
+ try {
154
+ parsed = parseInitArgs(args);
155
+ }
156
+ catch (error) {
157
+ return fail(error.message, true);
158
+ }
159
+ const { values, positionals } = parsed;
160
+ if (values.help) {
161
+ stdout(help);
162
+ return 0;
163
+ }
164
+ const unknown = positionals.filter((name) => !isToolName(name));
165
+ if (unknown.length > 0) {
166
+ return fail(`unknown tool: ${unknown.join(', ')}`, true);
167
+ }
168
+ if (positionals.length === 0)
169
+ return fail('name at least one tool', true);
170
+ const packageJson = readPackageJson(cwd);
171
+ if (!packageJson) {
172
+ return fail(`no package.json in ${cwd}. Run it in the project root.`);
173
+ }
174
+ // `cz` and `czg` set up the same files, so keep only the first of them.
175
+ const tools = [];
176
+ for (const name of positionals) {
177
+ const tool = TOOLS[name];
178
+ if (tools.every(({ files }) => files !== tool.files))
179
+ tools.push(tool);
180
+ }
181
+ const counts = { created: 0, merged: 0, overwrote: 0, kept: 0, skipped: 0 };
182
+ const manager = getPackageManager(cwd);
183
+ // Written before the install, so that pnpm applies them while installing.
184
+ const pnpmSettings = Object.assign({}, ...tools.map(({ pnpmSettings: settings }) => settings ?? {}));
185
+ if (manager === 'pnpm' && Object.keys(pnpmSettings).length > 0) {
186
+ const file = findPnpmWorkspace(cwd);
187
+ const name = chalk.bold(path.relative(cwd, file));
188
+ try {
189
+ const isNew = !existsSync(file);
190
+ const current = isNew ? '' : readFileSync(file, 'utf8');
191
+ const merged = mergePnpmSettings(current, pnpmSettings);
192
+ if (merged === current) {
193
+ say('kept', `Kept ${name}: it is already up to date`);
194
+ counts.kept += 1;
195
+ }
196
+ else {
197
+ writeFileSync(file, merged);
198
+ say(isNew ? 'created' : 'merged', `${isNew ? 'Created' : 'Merged'} ${name}`);
199
+ counts[isNew ? 'created' : 'merged'] += 1;
200
+ if (values['skip-install']) {
201
+ say('install', `Run ${chalk.bold('pnpm install')} to apply the new pnpm settings`);
202
+ }
203
+ }
204
+ }
205
+ catch (error) {
206
+ return fail(`could not update ${path.relative(cwd, file)}: ${error.message}`);
207
+ }
208
+ }
209
+ if (!values['skip-install']) {
210
+ const dependencies = [...new Set(tools.flatMap((t) => t.dependencies))];
211
+ const { command, args: installArgs } = getInstallCommand(manager, dependencies, cwd);
212
+ say('install', `Installing with ${chalk.bold(`${command} ${installArgs.join(' ')}`)}`);
213
+ const code = exec(command, installArgs, cwd);
214
+ if (code !== 0)
215
+ return fail(`${command} exited with code ${code}`);
216
+ }
217
+ let isFailed = false;
218
+ const generateOptions = {
219
+ manager,
220
+ toolNames: new Set(positionals),
221
+ packageJson,
222
+ };
223
+ for (const tool of tools) {
224
+ for (const file of tool.files) {
225
+ const existing = findExisting(file, cwd, packageJson);
226
+ const mergeTargets = [file.path, ...(file.mergeInto ?? [])];
227
+ const isForce = values.force === true;
228
+ const isMerge = existing !== undefined &&
229
+ file.merge !== undefined &&
230
+ mergeTargets.includes(existing);
231
+ const isOverwrite = existing === file.path && !isMerge && isForce;
232
+ const filePath = isMerge && existing ? existing : file.path;
233
+ const name = chalk.bold(filePath);
234
+ if (existing && !isMerge && !isOverwrite) {
235
+ const hint = existing === file.path ? ' (use --force to overwrite it)' : '';
236
+ say('skipped', `Skipped ${name}: ${existing} already exists${hint}`);
237
+ counts.skipped += 1;
238
+ continue;
239
+ }
240
+ try {
241
+ const target = path.join(cwd, filePath);
242
+ const incoming = readContent(file, cwd, generateOptions);
243
+ if (isMerge && file.merge) {
244
+ const current = readFileSync(target, 'utf8');
245
+ const merged = file.merge(current, incoming, { force: isForce });
246
+ if (merged === current) {
247
+ say('kept', `Kept ${name}: it is already up to date`);
248
+ counts.kept += 1;
249
+ }
250
+ else {
251
+ writeFileSync(target, merged);
252
+ say('merged', `Merged ${name}`);
253
+ counts.merged += 1;
254
+ }
255
+ continue;
256
+ }
257
+ mkdirSync(path.dirname(target), { recursive: true });
258
+ writeFileSync(target, incoming);
259
+ say(isOverwrite ? 'overwrote' : 'created', `${isOverwrite ? 'Overwrote' : 'Created'} ${name}`);
260
+ counts[isOverwrite ? 'overwrote' : 'created'] += 1;
261
+ }
262
+ catch (error) {
263
+ isFailed = true;
264
+ say('error', `spellbookx init: could not ${isMerge ? 'merge' : 'create'} ${filePath}: ${error.message}`);
265
+ }
266
+ }
267
+ }
268
+ for (const { afterInstall } of tools) {
269
+ if (!afterInstall)
270
+ continue;
271
+ const { command, args: commandArgs } = getExecCommand(manager, afterInstall);
272
+ const commandLine = chalk.bold([command, ...commandArgs].join(' '));
273
+ if (values['skip-install']) {
274
+ say('install', `Run ${commandLine} after installing the packages`);
275
+ continue;
276
+ }
277
+ say('install', `Running ${commandLine}`);
278
+ const code = exec(command, commandArgs, cwd);
279
+ if (code === 0) {
280
+ continue;
281
+ }
282
+ isFailed = true;
283
+ say('error', `spellbookx init: ${command} exited with code ${code}`);
284
+ }
285
+ const summary = Object.entries(counts)
286
+ .filter(([, count]) => count > 0)
287
+ .map(([kind, count]) => `${count} ${kind}`)
288
+ .join(', ');
289
+ if (summary)
290
+ say('done', `Done: ${summary}`);
291
+ return isFailed ? 1 : 0;
292
+ }
293
+ /**
294
+ * Parses the arguments of `spellbookx init`.
295
+ * @param args - The arguments after `init`.
296
+ * @returns The options and the tool names.
297
+ */
298
+ function parseInitArgs(args) {
299
+ return parseArgs({
300
+ args: [...args],
301
+ options: {
302
+ force: { type: 'boolean', short: 'f' },
303
+ 'skip-install': { type: 'boolean' },
304
+ help: { type: 'boolean', short: 'h' },
305
+ },
306
+ allowPositionals: true,
307
+ strict: true,
308
+ });
309
+ }
310
+ //# sourceMappingURL=init.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.js","sources":["../../src/commands/init.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGtC,OAAO,EAAE,UAAU,EAAE,aAAa,EAAoB,MAAM,cAAc,CAAC;AAC3E,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAEL,UAAU,EAEV,UAAU,EAEV,KAAK,GACN,MAAM,aAAa,CAAC;AAErB;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAW;;;;;;;;IAQlC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC;;;;;;;;CAQxB,CAAC;AAEF;;;;GAIG;AACH,SAAS,eAAe,CAAC,GAAW;IAClC,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CACf,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,EAAE,MAAM,CAAC,CAC1B,CAAC;IAC/B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;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;AAED;;;;GAIG;AACH,SAAS,aAAa,CAAC,IAAuB;IAC5C,OAAO,SAAS,CAAC;QACf,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;QACf,OAAO,EAAE;YACP,KAAK,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE;YACtC,cAAc,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;YACnC,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE;SACtC;QACD,gBAAgB,EAAE,IAAI;QACtB,MAAM,EAAE,IAAI;KACb,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @file The environment that the commands run in: the working directory, the
3
+ * output streams and the way to run other programs.
4
+ */
5
+ import { type ChalkInstance } from 'chalk';
6
+ /**
7
+ * Runs a program and waits for it to finish.
8
+ * @param command - The program to run, for example `pnpm`.
9
+ * @param args - The arguments of the program.
10
+ * @param cwd - The folder to run it in.
11
+ * @returns The exit code of the program.
12
+ */
13
+ export type Exec = (command: string, args: readonly string[], cwd: string) => number;
14
+ /**
15
+ * The environment of a command.
16
+ */
17
+ export interface Context {
18
+ /**
19
+ * The folder that the command works in.
20
+ */
21
+ cwd: string;
22
+ /**
23
+ * Writes normal output, such as the help text.
24
+ */
25
+ stdout: (text: string) => void;
26
+ /**
27
+ * Writes error messages.
28
+ */
29
+ stderr: (text: string) => void;
30
+ /**
31
+ * Runs another program, such as the package manager.
32
+ */
33
+ exec: Exec;
34
+ /**
35
+ * Colors the output. Its color level decides whether colors are written.
36
+ */
37
+ chalk: ChalkInstance;
38
+ }
39
+ /**
40
+ * Runs a program with the terminal of this process, so that its output is
41
+ * shown as it is written.
42
+ * @param command - The program to run.
43
+ * @param args - The arguments of the program.
44
+ * @param cwd - The folder to run it in.
45
+ * @returns The exit code of the program, or `1` if it could not be started.
46
+ */
47
+ export declare const spawnExec: Exec;
48
+ /**
49
+ * Creates the context of the current process.
50
+ * @returns The context, with the current folder, the process streams,
51
+ * {@link spawnExec} and the default chalk instance, which detects whether the
52
+ * terminal supports colors (and honors `NO_COLOR` and `FORCE_COLOR`).
53
+ */
54
+ export declare function processContext(): Context;
55
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAc,EAAE,KAAK,aAAa,EAAE,MAAM,OAAO,CAAC;AAElD;;;;;;GAMG;AACH,MAAM,MAAM,IAAI,GAAG,CACjB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,GAAG,EAAE,MAAM,KACR,MAAM,CAAC;AAEZ;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/B;;OAEG;IACH,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/B;;OAEG;IACH,IAAI,EAAE,IAAI,CAAC;IACX;;OAEG;IACH,KAAK,EAAE,aAAa,CAAC;CACtB;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS,EAAE,IASvB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,IAAI,OAAO,CAQxC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @file The environment that the commands run in: the working directory, the
3
+ * output streams and the way to run other programs.
4
+ */
5
+ import { spawnSync } from 'node:child_process';
6
+ import chalk from 'chalk';
7
+ /**
8
+ * Runs a program with the terminal of this process, so that its output is
9
+ * shown as it is written.
10
+ * @param command - The program to run.
11
+ * @param args - The arguments of the program.
12
+ * @param cwd - The folder to run it in.
13
+ * @returns The exit code of the program, or `1` if it could not be started.
14
+ */
15
+ export const spawnExec = (command, args, cwd) => {
16
+ const result = spawnSync(command, args, {
17
+ cwd,
18
+ stdio: 'inherit',
19
+ // Package managers are `.cmd` scripts on Windows, which need a shell.
20
+ shell: process.platform === 'win32',
21
+ });
22
+ return result.status ?? 1;
23
+ };
24
+ /**
25
+ * Creates the context of the current process.
26
+ * @returns The context, with the current folder, the process streams,
27
+ * {@link spawnExec} and the default chalk instance, which detects whether the
28
+ * terminal supports colors (and honors `NO_COLOR` and `FORCE_COLOR`).
29
+ */
30
+ export function processContext() {
31
+ return {
32
+ cwd: process.cwd(),
33
+ stdout: (text) => process.stdout.write(text),
34
+ stderr: (text) => process.stderr.write(text),
35
+ exec: spawnExec,
36
+ chalk,
37
+ };
38
+ }
39
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C,OAAO,KAA6B,MAAM,OAAO,CAAC;AAyClD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,SAAS,GAAS,CAAC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE;IACpD,MAAM,MAAM,GAAG,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE;QACtC,GAAG;QACH,KAAK,EAAE,SAAS;QAChB,sEAAsE;QACtE,KAAK,EAAE,OAAO,CAAC,QAAQ,KAAK,OAAO;KACpC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC;AAC5B,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,cAAc;IAC5B,OAAO;QACL,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;QAClB,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;QAC5C,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;QAC5C,IAAI,EAAE,SAAS;QACf,KAAK;KACN,CAAC;AACJ,CAAC"}