create-codebase-snapshot 0.9.12 → 0.9.14

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 (2) hide show
  1. package/index.mjs +68 -29
  2. package/package.json +2 -2
package/index.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import * as fs from "node:fs";
3
3
  import * as path from "node:path";
4
- import { PromptCancelled, runInit } from "codebase-snapshot";
4
+ import { parseArgs } from "node:util";
5
+ import { PromptCancelled, runInit, stripJsonComments } from "codebase-snapshot";
5
6
 
6
7
  /**
7
8
  * `npm create codebase-snapshot`
@@ -33,7 +34,13 @@ function hasLocalBinary() {
33
34
  /** True when the project's package.json already carries the `snapshot` script. */
34
35
  function hasSnapshotScript() {
35
36
  try {
36
- const pkg = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8"));
37
+ // Through `stripJsonComments`, like every other reader of `package.json` in this tool: a UTF-8
38
+ // BOM is what a Windows editor writes, and `JSON.parse` refuses it — so a project that already
39
+ // had the script was told to run `npx codebase-snapshot generate` instead of
40
+ // `npm run snapshot`, which is the command the rest of this repository's documentation names.
41
+ const pkg = JSON.parse(
42
+ stripJsonComments(fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8"))
43
+ );
37
44
  return typeof pkg?.scripts?.snapshot === "string";
38
45
  } catch {
39
46
  return false;
@@ -57,41 +64,73 @@ function nextStep() {
57
64
  return hasLocalBinary() ? "npm run snapshot" : "npm install && npm run snapshot";
58
65
  }
59
66
 
60
- const argv = process.argv.slice(2);
67
+ const HELP_TEXT = [
68
+ "create-codebase-snapshot",
69
+ "",
70
+ " Sets up codebase-snapshot in the current directory.",
71
+ "",
72
+ " Usage:",
73
+ " npm create codebase-snapshot",
74
+ "",
75
+ " Options:",
76
+ " --preset <name> Force a project preset instead of detecting one",
77
+ " --yes Accept every default without prompting",
78
+ " --force Overwrite an existing config",
79
+ "",
80
+ ].join("\n");
61
81
 
62
- if (argv.includes("--help") || argv.includes("-h")) {
63
- process.stderr.write(
64
- [
65
- "create-codebase-snapshot",
66
- "",
67
- " Sets up codebase-snapshot in the current directory.",
68
- "",
69
- " Usage:",
70
- " npm create codebase-snapshot",
71
- "",
72
- " Options:",
73
- " --preset <name> Force a project preset instead of detecting one",
74
- " --yes Accept every default without prompting",
75
- " --force Overwrite an existing config",
76
- "",
77
- ].join("\n")
78
- );
82
+ /**
83
+ * Arguments, parsed by the platform rather than by hand.
84
+ *
85
+ * The hand-rolled version used `argv.indexOf(flag)`, which has three silent failures that all
86
+ * matter here, and rule 1 of this repository says none of them may be silent:
87
+ *
88
+ * - `--preset=nextjs` — the form `npm create x -- --flag=value` makes natural — was never found,
89
+ * so the requested preset was ignored and auto-detection ran instead.
90
+ * - `--preset` with no value was accepted and treated as "no preset", with the same result.
91
+ * - an unknown flag, including a typo for a real one (`--forse`), was ignored outright. A user who
92
+ * typed `--forse` believed the overwrite had been forced.
93
+ *
94
+ * `parseArgs` with `strict: true` rejects all three by name, before any wizard code runs.
95
+ */
96
+ let parsed;
97
+ try {
98
+ parsed = parseArgs({
99
+ args: process.argv.slice(2),
100
+ options: {
101
+ preset: { type: "string" },
102
+ yes: { type: "boolean" },
103
+ force: { type: "boolean" },
104
+ help: { type: "boolean", short: "h" },
105
+ },
106
+ allowPositionals: false,
107
+ strict: true,
108
+ });
109
+ } catch (error) {
110
+ process.stderr.write(`\n❌ ${error instanceof Error ? error.message : String(error)}\n`);
111
+ process.stderr.write(`\n${HELP_TEXT}\n`);
112
+ process.exit(2);
113
+ }
114
+
115
+ if (parsed.values.help) {
116
+ process.stderr.write(HELP_TEXT);
79
117
  process.exit(0);
80
118
  }
81
119
 
82
- const takeValue = (flag) => {
83
- const index = argv.indexOf(flag);
84
- if (index === -1) return undefined;
85
- const value = argv[index + 1];
86
- return value && !value.startsWith("-") ? value : undefined;
87
- };
120
+ const preset = parsed.values.preset;
121
+ // `--preset=` parses as an empty string, which is not "no preset": somebody asked for a preset and
122
+ // named nothing.
123
+ if (preset !== undefined && preset.trim() === "") {
124
+ process.stderr.write("\n❌ --preset requires a name (it received an empty string).\n");
125
+ process.exit(2);
126
+ }
88
127
 
89
128
  try {
90
129
  const result = await runInit({
91
130
  cwd: process.cwd(),
92
- preset: takeValue("--preset"),
93
- yes: argv.includes("--yes"),
94
- force: argv.includes("--force"),
131
+ preset: preset?.trim(),
132
+ yes: parsed.values.yes === true,
133
+ force: parsed.values.force === true,
95
134
  });
96
135
 
97
136
  if (!result.written) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-codebase-snapshot",
3
- "version": "0.9.12",
3
+ "version": "0.9.14",
4
4
  "description": "Set up 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
- "codebase-snapshot": "^0.9.12"
25
+ "codebase-snapshot": "^0.9.14"
26
26
  },
27
27
  "engines": {
28
28
  "node": ">=18.0.0"