create-codebase-snapshot 4.1.0 → 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 +1 -2
  2. package/index.mjs +13 -94
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -21,7 +21,6 @@ npm create codebase-snapshot -- --preset react-vite --yes
21
21
  | :--- | :--- |
22
22
  | `--preset <name>` | Force a project preset instead of detecting one |
23
23
  | `--yes` | Accept every default without prompting |
24
- | `--minimal` | Write only the settings that differ from defaults |
25
24
  | `--force` | Overwrite an existing config |
26
25
 
27
26
  ## What it writes
@@ -30,7 +29,7 @@ npm create codebase-snapshot -- --preset react-vite --yes
30
29
  | :--- | :--- |
31
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. |
32
31
  | `package.json` → `scripts.snapshot`, `scripts.snapshot:validate` | Skipped when there is no `package.json` to hold them. |
33
- | `package.json` → `devDependencies` | `@ankurhalder/codebase-snapshot` at the range this scaffolder shipped with. |
32
+ | `package.json` → `devDependencies` | `@ankurhalder/codebase-snapshot` at the version this scaffolder shipped with. A range you already declared is never overwritten. |
34
33
  | `.gitignore` | `snapshot/`, `snapshot.tmp/`. |
35
34
 
36
35
  The generated scripts run `npx @ankurhalder/codebase-snapshot …`, and `npx` uses the
package/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
  import * as fs from "node:fs";
3
- import { createRequire } from "node:module";
4
3
  import * as path from "node:path";
5
4
  import { PromptCancelled, runInit } from "@ankurhalder/codebase-snapshot";
6
5
 
@@ -11,91 +10,14 @@ import { PromptCancelled, runInit } from "@ankurhalder/codebase-snapshot";
11
10
  * one prompt implementation to keep working. Everything it does is available as
12
11
  * `npx @ankurhalder/codebase-snapshot init`; this entry point only exists because
13
12
  * `npm create <x>` is the convention developers reach for first.
14
- */
15
-
16
- const PACKAGE_NAME = "@ankurhalder/codebase-snapshot";
17
-
18
- const require = createRequire(import.meta.url);
19
-
20
- /** The range this shim was published against, read from its own manifest. */
21
- function dependencyRange() {
22
- try {
23
- const manifest = require("./package.json");
24
- const range = manifest?.dependencies?.[PACKAGE_NAME];
25
- return typeof range === "string" ? range : null;
26
- } catch {
27
- return null;
28
- }
29
- }
30
-
31
- /** Keeps the file's own indentation and trailing newline when rewriting JSON. */
32
- function serializePackageJson(raw, value) {
33
- const indent = /^[{[]\r?\n([ \t]+)/.exec(raw)?.[1];
34
- const spacing = raw.includes("\n") ? (indent ?? " ") : undefined;
35
- const body = JSON.stringify(value, null, spacing);
36
- return `${body}${raw.endsWith("\n") ? "\n" : ""}`;
37
- }
38
-
39
- /**
40
- * Records the runtime dependency the generated scripts need.
41
13
  *
42
- * The wizard writes `npx @ankurhalder/codebase-snapshot generate`, which resolves
43
- * the *latest* release when nothing is installed locally: the first run silently
44
- * downloaded @latest (observed pulling an older minor), failed behind a proxy and
45
- * could cross a major version. Naming the package in `devDependencies` makes
46
- * `npx` prefer that local copy, so the scripts become version-locked once the
47
- * project is installed — without this shim ever touching the network itself.
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.
48
18
  */
49
- function recordDependency(written) {
50
- if (!written) return { added: false };
51
19
 
52
- const range = dependencyRange();
53
- if (!range) {
54
- return {
55
- added: false,
56
- note: `Could not read ${PACKAGE_NAME}'s version from this package, so it was not added to devDependencies.`,
57
- };
58
- }
59
-
60
- const pkgPath = path.join(process.cwd(), "package.json");
61
- if (!fs.existsSync(pkgPath)) return { added: false };
62
-
63
- let raw;
64
- try {
65
- raw = fs.readFileSync(pkgPath, "utf8");
66
- } catch {
67
- return {
68
- added: false,
69
- note: `package.json could not be read, so ${PACKAGE_NAME} was not added to devDependencies.`,
70
- };
71
- }
72
-
73
- let pkg;
74
- try {
75
- pkg = JSON.parse(raw);
76
- } catch {
77
- return {
78
- added: false,
79
- note: `package.json could not be parsed, so ${PACKAGE_NAME} was not added to devDependencies.`,
80
- };
81
- }
82
-
83
- const declared = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"].some(
84
- (field) => pkg?.[field] && typeof pkg[field] === "object" && PACKAGE_NAME in pkg[field]
85
- );
86
- if (declared) return { added: false, range };
87
-
88
- pkg.devDependencies = { ...(pkg.devDependencies ?? {}), [PACKAGE_NAME]: range };
89
- try {
90
- fs.writeFileSync(pkgPath, serializePackageJson(raw, pkg));
91
- } catch {
92
- return {
93
- added: false,
94
- note: `package.json could not be written, so ${PACKAGE_NAME} was not added to devDependencies.`,
95
- };
96
- }
97
- return { added: true, range };
98
- }
20
+ const PACKAGE_NAME = "@ankurhalder/codebase-snapshot";
99
21
 
100
22
  /** True when the package's own binary is already installed in the project. */
101
23
  function hasLocalBinary() {
@@ -126,7 +48,9 @@ function hasSnapshotScript() {
126
48
  * documented next step failed with ENOENT in exactly the case that needs help
127
49
  * most. A pre-existing script counts even when this run added nothing, hence the
128
50
  * check is against `package.json` rather than against what the wizard wrote. The
129
- * remaining forms are executable as printed.
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`.
130
54
  */
131
55
  function nextStep() {
132
56
  if (!hasSnapshotScript()) return `npx ${PACKAGE_NAME} generate`;
@@ -148,7 +72,6 @@ if (argv.includes("--help") || argv.includes("-h")) {
148
72
  " Options:",
149
73
  " --preset <name> Force a project preset instead of detecting one",
150
74
  " --yes Accept every default without prompting",
151
- " --minimal Write only the settings that differ from defaults",
152
75
  " --force Overwrite an existing config",
153
76
  "",
154
77
  ].join("\n")
@@ -168,7 +91,6 @@ try {
168
91
  cwd: process.cwd(),
169
92
  preset: takeValue("--preset"),
170
93
  yes: argv.includes("--yes"),
171
- minimal: argv.includes("--minimal"),
172
94
  force: argv.includes("--force"),
173
95
  });
174
96
 
@@ -180,17 +102,14 @@ try {
180
102
  if (result.scripts.length > 0) {
181
103
  process.stderr.write(` Added npm scripts: ${result.scripts.join(", ")}\n`);
182
104
  }
105
+ if (result.dependency.added) {
106
+ process.stderr.write(
107
+ ` Added devDependency: ${result.dependency.name} ${result.dependency.range}\n`
108
+ );
109
+ }
183
110
  if (result.gitignoreEntries.length > 0) {
184
111
  process.stderr.write(` Added to .gitignore: ${result.gitignoreEntries.join(", ")}\n`);
185
112
  }
186
-
187
- const dependency = recordDependency(result.written);
188
- if (dependency.added) {
189
- process.stderr.write(` Added devDependency: ${PACKAGE_NAME} ${dependency.range}\n`);
190
- } else if (dependency.note) {
191
- process.stderr.write(` ⚠ ${dependency.note}\n`);
192
- }
193
-
194
113
  for (const note of result.notes) process.stderr.write(` ⚠ ${note}\n`);
195
114
  process.stderr.write(`\n Next: ${nextStep()}\n\n`);
196
115
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-codebase-snapshot",
3
- "version": "4.1.0",
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.1.0"
25
+ "@ankurhalder/codebase-snapshot": "^4.1.1"
26
26
  },
27
27
  "engines": {
28
28
  "node": ">=18.0.0"