create-codebase-snapshot 4.0.7 → 4.1.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 (3) hide show
  1. package/README.md +23 -3
  2. package/index.mjs +53 -3
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -8,8 +8,10 @@ npm create codebase-snapshot
8
8
  ```
9
9
 
10
10
  It detects the project type, asks a handful of questions with sensible defaults,
11
- and writes a `snapshot.config.mjs` that lists **every setting at its default** —
12
- so the config doubles as the documentation.
11
+ and writes the config file this project can actually load — `snapshot.config.ts`
12
+ for an ES-module project on Node ≥ 22.6, `snapshot.config.mjs` everywhere else —
13
+ listing **every setting at its default**, so the config doubles as the
14
+ documentation.
13
15
 
14
16
  ```bash
15
17
  npm create codebase-snapshot -- --preset react-vite --yes
@@ -19,8 +21,26 @@ npm create codebase-snapshot -- --preset react-vite --yes
19
21
  | :--- | :--- |
20
22
  | `--preset <name>` | Force a project preset instead of detecting one |
21
23
  | `--yes` | Accept every default without prompting |
22
- | `--minimal` | Write only the settings that differ from defaults |
23
24
  | `--force` | Overwrite an existing config |
24
25
 
26
+ ## What it writes
27
+
28
+ | Path | Notes |
29
+ | :--- | :--- |
30
+ | `snapshot.config.ts` / `snapshot.config.mjs` | Every setting at its default. `.ts` needs an ES-module `package.json` and Node ≥ 22.6; `.mjs` is used otherwise, because a `.ts` config is loaded with the enclosing package's module type. |
31
+ | `package.json` → `scripts.snapshot`, `scripts.snapshot:validate` | Skipped when there is no `package.json` to hold them. |
32
+ | `package.json` → `devDependencies` | `@ankurhalder/codebase-snapshot` at the version this scaffolder shipped with. A range you already declared is never overwritten. |
33
+ | `.gitignore` | `snapshot/`, `snapshot.tmp/`. |
34
+
35
+ The generated scripts run `npx @ankurhalder/codebase-snapshot …`, and `npx` uses the
36
+ local `devDependencies` copy when there is one — so the version is pinned by your
37
+ lockfile instead of drifting to `@latest`. Until you run `npm install`, `npx` would
38
+ fall back to downloading the newest release, which is why the printed next step is
39
+ `npm install && npm run snapshot`. Nothing is installed, and no network is used,
40
+ while scaffolding.
41
+
42
+ Without a `package.json` the scripts cannot be added, and the printed next step is
43
+ `npx @ankurhalder/codebase-snapshot generate` instead.
44
+
25
45
  This package is a shim: the wizard itself lives in the main package, so
26
46
  `npx @ankurhalder/codebase-snapshot init` does exactly the same thing.
package/index.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env node
2
+ import * as fs from "node:fs";
3
+ import * as path from "node:path";
2
4
  import { PromptCancelled, runInit } from "@ankurhalder/codebase-snapshot";
3
5
 
4
6
  /**
@@ -8,7 +10,52 @@ import { PromptCancelled, runInit } from "@ankurhalder/codebase-snapshot";
8
10
  * one prompt implementation to keep working. Everything it does is available as
9
11
  * `npx @ankurhalder/codebase-snapshot init`; this entry point only exists because
10
12
  * `npm create <x>` is the convention developers reach for first.
13
+ *
14
+ * The shim used to carry its own `devDependencies` writer, because `init` wrote the
15
+ * scripts and no dependency at all. The wizard records the dependency itself now, so
16
+ * this file reports what happened and names the next command — a second writer is a
17
+ * second source of truth, and the two drift.
18
+ */
19
+
20
+ const PACKAGE_NAME = "@ankurhalder/codebase-snapshot";
21
+
22
+ /** True when the package's own binary is already installed in the project. */
23
+ function hasLocalBinary() {
24
+ const names =
25
+ process.platform === "win32"
26
+ ? ["codebase-snapshot.cmd", "codebase-snapshot"]
27
+ : ["codebase-snapshot"];
28
+ return names.some((name) =>
29
+ fs.existsSync(path.join(process.cwd(), "node_modules", ".bin", name))
30
+ );
31
+ }
32
+
33
+ /** True when the project's package.json already carries the `snapshot` script. */
34
+ function hasSnapshotScript() {
35
+ try {
36
+ const pkg = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8"));
37
+ return typeof pkg?.scripts?.snapshot === "string";
38
+ } catch {
39
+ return false;
40
+ }
41
+ }
42
+
43
+ /**
44
+ * The command the developer should run next.
45
+ *
46
+ * `npm run snapshot` is only meaningful when the project has that script, which
47
+ * needs a `package.json`: the old text promised it unconditionally, so the
48
+ * documented next step failed with ENOENT in exactly the case that needs help
49
+ * most. A pre-existing script counts even when this run added nothing, hence the
50
+ * check is against `package.json` rather than against what the wizard wrote. The
51
+ * remaining forms are executable as printed, and `init` has already declared the
52
+ * devDependency by now — so `npm install` really does put the package in
53
+ * `node_modules`.
11
54
  */
55
+ function nextStep() {
56
+ if (!hasSnapshotScript()) return `npx ${PACKAGE_NAME} generate`;
57
+ return hasLocalBinary() ? "npm run snapshot" : "npm install && npm run snapshot";
58
+ }
12
59
 
13
60
  const argv = process.argv.slice(2);
14
61
 
@@ -25,7 +72,6 @@ if (argv.includes("--help") || argv.includes("-h")) {
25
72
  " Options:",
26
73
  " --preset <name> Force a project preset instead of detecting one",
27
74
  " --yes Accept every default without prompting",
28
- " --minimal Write only the settings that differ from defaults",
29
75
  " --force Overwrite an existing config",
30
76
  "",
31
77
  ].join("\n")
@@ -45,7 +91,6 @@ try {
45
91
  cwd: process.cwd(),
46
92
  preset: takeValue("--preset"),
47
93
  yes: argv.includes("--yes"),
48
- minimal: argv.includes("--minimal"),
49
94
  force: argv.includes("--force"),
50
95
  });
51
96
 
@@ -57,11 +102,16 @@ try {
57
102
  if (result.scripts.length > 0) {
58
103
  process.stderr.write(` Added npm scripts: ${result.scripts.join(", ")}\n`);
59
104
  }
105
+ if (result.dependency.added) {
106
+ process.stderr.write(
107
+ ` Added devDependency: ${result.dependency.name} ${result.dependency.range}\n`
108
+ );
109
+ }
60
110
  if (result.gitignoreEntries.length > 0) {
61
111
  process.stderr.write(` Added to .gitignore: ${result.gitignoreEntries.join(", ")}\n`);
62
112
  }
63
113
  for (const note of result.notes) process.stderr.write(` ⚠ ${note}\n`);
64
- process.stderr.write("\n Next: npm run snapshot\n\n");
114
+ process.stderr.write(`\n Next: ${nextStep()}\n\n`);
65
115
  } catch (err) {
66
116
  if (err instanceof PromptCancelled) {
67
117
  process.stderr.write("\n❌ Cancelled.\n");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-codebase-snapshot",
3
- "version": "4.0.7",
3
+ "version": "4.1.1",
4
4
  "description": "Set up @ankurhalder/codebase-snapshot in a repository",
5
5
  "license": "MIT",
6
6
  "author": "Ankur Halder <ankur.halder12345@gmail.com>",
@@ -22,7 +22,7 @@
22
22
  "README.md"
23
23
  ],
24
24
  "dependencies": {
25
- "@ankurhalder/codebase-snapshot": "^4.0.7"
25
+ "@ankurhalder/codebase-snapshot": "^4.1.1"
26
26
  },
27
27
  "engines": {
28
28
  "node": ">=18.0.0"