create-codebase-snapshot 0.0.0-stage → 0.9.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.
- package/README.md +45 -2
- package/index.mjs +122 -0
- package/package.json +31 -4
package/README.md
CHANGED
|
@@ -1,3 +1,46 @@
|
|
|
1
|
-
#
|
|
1
|
+
# create-codebase-snapshot
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sets up [`codebase-snapshot`](https://www.npmjs.com/package/codebase-snapshot)
|
|
4
|
+
in the current repository.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm create codebase-snapshot
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
It detects the project type, asks a handful of questions with sensible defaults,
|
|
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.
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm create codebase-snapshot -- --preset react-vite --yes
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
| Option | Effect |
|
|
21
|
+
| :--- | :--- |
|
|
22
|
+
| `--preset <name>` | Force a project preset instead of detecting one |
|
|
23
|
+
| `--yes` | Accept every default without prompting |
|
|
24
|
+
| `--force` | Overwrite an existing config |
|
|
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` | `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 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 codebase-snapshot generate` instead.
|
|
44
|
+
|
|
45
|
+
This package is a shim: the wizard itself lives in the main package, so
|
|
46
|
+
`npx codebase-snapshot init` does exactly the same thing.
|
package/index.mjs
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { PromptCancelled, runInit } from "codebase-snapshot";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `npm create codebase-snapshot`
|
|
8
|
+
*
|
|
9
|
+
* A thin shim over the same wizard the main package exposes, so there is exactly
|
|
10
|
+
* one prompt implementation to keep working. Everything it does is available as
|
|
11
|
+
* `npx codebase-snapshot init`; this entry point only exists because
|
|
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 = "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`.
|
|
54
|
+
*/
|
|
55
|
+
function nextStep() {
|
|
56
|
+
if (!hasSnapshotScript()) return `npx ${PACKAGE_NAME} generate`;
|
|
57
|
+
return hasLocalBinary() ? "npm run snapshot" : "npm install && npm run snapshot";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const argv = process.argv.slice(2);
|
|
61
|
+
|
|
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
|
+
);
|
|
79
|
+
process.exit(0);
|
|
80
|
+
}
|
|
81
|
+
|
|
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
|
+
};
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const result = await runInit({
|
|
91
|
+
cwd: process.cwd(),
|
|
92
|
+
preset: takeValue("--preset"),
|
|
93
|
+
yes: argv.includes("--yes"),
|
|
94
|
+
force: argv.includes("--force"),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
if (!result.written) {
|
|
98
|
+
process.exit(0);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
process.stderr.write(`\n✅ Created ${result.configPath} (preset: ${result.preset})\n`);
|
|
102
|
+
if (result.scripts.length > 0) {
|
|
103
|
+
process.stderr.write(` Added npm scripts: ${result.scripts.join(", ")}\n`);
|
|
104
|
+
}
|
|
105
|
+
if (result.dependency.added) {
|
|
106
|
+
process.stderr.write(
|
|
107
|
+
` Added devDependency: ${result.dependency.name} ${result.dependency.range}\n`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (result.gitignoreEntries.length > 0) {
|
|
111
|
+
process.stderr.write(` Added to .gitignore: ${result.gitignoreEntries.join(", ")}\n`);
|
|
112
|
+
}
|
|
113
|
+
for (const note of result.notes) process.stderr.write(` ⚠ ${note}\n`);
|
|
114
|
+
process.stderr.write(`\n Next: ${nextStep()}\n\n`);
|
|
115
|
+
} catch (err) {
|
|
116
|
+
if (err instanceof PromptCancelled) {
|
|
117
|
+
process.stderr.write("\n❌ Cancelled.\n");
|
|
118
|
+
process.exit(1);
|
|
119
|
+
}
|
|
120
|
+
process.stderr.write(`\n❌ ${err instanceof Error ? err.message : String(err)}\n`);
|
|
121
|
+
process.exit(1);
|
|
122
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-codebase-snapshot",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.9.1",
|
|
4
|
+
"description": "Set up codebase-snapshot in a repository",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Ankur Halder <ankur.halder12345@gmail.com>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/ankurhalder/codebase-snapshot.git",
|
|
10
|
+
"directory": "packages/create"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/ankurhalder/codebase-snapshot#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/ankurhalder/codebase-snapshot/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"bin": {
|
|
18
|
+
"create-codebase-snapshot": "./index.mjs"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"index.mjs",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"codebase-snapshot": "^0.9.1"
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18.0.0"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
}
|
|
33
|
+
}
|