create-kerf-component 5.0.0-beta.125 → 5.0.0-beta.126

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/index.js CHANGED
@@ -7,15 +7,18 @@
7
7
  // delegation disposer.
8
8
  //
9
9
  // Usage:
10
- // npm create kerf-component@latest <dir>
10
+ // npm create kerf-component@latest <dir> [--name <package-name>]
11
11
  // npm init kerf-component <dir>
12
12
  // npx create-kerf-component <dir>
13
13
  //
14
14
  // <dir> is the target directory; its basename is the default package name (pass
15
- // `.` to scaffold into the current directory). Run with NO argument and it
16
- // prompts for the directory (or reads it from piped stdin) — `npm create
17
- // kerf-component` alone just works. No third-party dependencies — the
18
- // initializer is plain Node so `npm create` runs it with zero install latency.
15
+ // `.` to scaffold into the current directory). `--name` sets the package name
16
+ // instead, which is how to scaffold a scoped package (`--name @acme/widgets`).
17
+ // Run with NO argument and it prompts for the directory (or reads it from piped
18
+ // stdin) — `npm create kerf-component` alone just works. The initializer itself
19
+ // is plain Node; TypeScript is only an optional peer of the
20
+ // `kerf-component-catalog` bin, which runs inside a TypeScript project, so
21
+ // `npm create` installs nothing beyond this package.
19
22
 
20
23
  import {
21
24
  copyFileSync,
@@ -25,6 +28,7 @@ import {
25
28
  readdirSync,
26
29
  readFileSync,
27
30
  renameSync,
31
+ statSync,
28
32
  writeFileSync,
29
33
  } from 'node:fs';
30
34
  import { basename, dirname, join, resolve } from 'node:path';
@@ -42,9 +46,40 @@ function fail(msg) {
42
46
  }
43
47
 
44
48
  const USAGE =
45
- 'Usage: npm create kerf-component@latest <dir>\n' +
46
- ' (or: npx create-kerf-component <dir>)\n\n' +
47
- 'Scaffolds a kerf component package into <dir>. Pass `.` for the current directory.\n';
49
+ 'Usage: npm create kerf-component@latest <dir> [--name <package-name>]\n' +
50
+ ' (or: npx create-kerf-component <dir> [--name <package-name>])\n\n' +
51
+ 'Scaffolds a kerf component package into <dir>. Pass `.` for the current directory.\n' +
52
+ 'The package name defaults to the directory name; use --name for a scoped name\n' +
53
+ '(for example --name @acme/widgets).\n';
54
+
55
+ // Split the command line into the target directory and options. Anything that
56
+ // starts with `-` is an option: an unknown one is an error rather than a
57
+ // directory named `--foo`.
58
+ function parseArgs(args) {
59
+ const parsed = { target: undefined, name: undefined, help: false };
60
+ for (let index = 0; index < args.length; index++) {
61
+ const arg = args[index];
62
+ if (arg === '--help' || arg === '-h') parsed.help = true;
63
+ else if (arg === '--name') {
64
+ const value = args[++index];
65
+ if (value === undefined || value.startsWith('-'))
66
+ throw new Error(
67
+ '--name needs a package name, e.g. --name @acme/widgets.',
68
+ );
69
+ parsed.name = value;
70
+ } else if (arg.startsWith('--name=')) parsed.name = arg.slice(7);
71
+ else if (arg.startsWith('-'))
72
+ throw new Error(
73
+ `unknown option "${arg}". Run with --help for usage. (To use a directory whose name starts with "-", pass it as ./${arg}.)`,
74
+ );
75
+ else if (parsed.target === undefined) parsed.target = arg;
76
+ else
77
+ throw new Error(
78
+ `expected one target directory, received "${parsed.target}" and "${arg}".`,
79
+ );
80
+ }
81
+ return parsed;
82
+ }
48
83
 
49
84
  // Ask for the target directory when none was passed. `npm create kerf-component`
50
85
  // (no arg) is the most natural invocation, so prompt rather than error.
@@ -91,12 +126,17 @@ function walk(dir) {
91
126
  }
92
127
 
93
128
  async function main(argv) {
94
- let target = argv[2];
95
-
96
- if (target === '--help' || target === '-h') {
129
+ let options;
130
+ try {
131
+ options = parseArgs(argv.slice(2));
132
+ } catch (error) {
133
+ fail(error.message);
134
+ }
135
+ if (options.help) {
97
136
  process.stdout.write(USAGE);
98
137
  process.exit(0);
99
138
  }
139
+ let target = options.target;
100
140
 
101
141
  if (target == null || target === '') {
102
142
  // No directory given. Prompt interactively when attached to a terminal (the
@@ -114,26 +154,55 @@ async function main(argv) {
114
154
  }
115
155
 
116
156
  const targetDir = resolve(process.cwd(), target);
117
- const pkgName = target === '.' ? basename(targetDir) : basename(target);
157
+ const pkgName =
158
+ options.name ?? (target === '.' ? basename(targetDir) : basename(target));
118
159
 
119
160
  if (!isValidPackageName(pkgName)) {
120
- fail(`"${pkgName}" is not a valid npm package name.`);
161
+ fail(
162
+ options.name === undefined
163
+ ? `"${pkgName}" (from the directory name) is not a valid npm package name; pass --name <package-name> or choose another directory.`
164
+ : `"${pkgName}" is not a valid npm package name.`,
165
+ );
121
166
  }
122
167
 
123
- if (existsSync(targetDir) && readdirSync(targetDir).length > 0) {
124
- fail(`target directory "${target}" already exists and is not empty.`);
168
+ if (existsSync(targetDir)) {
169
+ if (!statSync(targetDir).isDirectory())
170
+ fail(
171
+ `target "${target}" already exists and is a file, not a directory; choose a new directory name.`,
172
+ );
173
+ if (readdirSync(targetDir).length > 0)
174
+ fail(`target directory "${target}" already exists and is not empty.`);
125
175
  }
126
176
 
127
177
  // Copy the whole template tree, then post-process each file in place: replace
128
178
  // the package-name token and rename the dotfile placeholders npm can't ship
129
179
  // verbatim (`_gitignore` → `.gitignore`).
130
- mkdirSync(targetDir, { recursive: true });
180
+ try {
181
+ mkdirSync(targetDir, { recursive: true });
182
+ } catch (error) {
183
+ if (error?.code === 'ENOTDIR' || error?.code === 'EEXIST')
184
+ fail(
185
+ `cannot create "${target}": part of that path is an existing file, not a directory.`,
186
+ );
187
+ throw error;
188
+ }
131
189
  cpSync(TEMPLATE_DIR, targetDir, { recursive: true });
132
190
  mkdirSync(join(targetDir, 'scripts'), { recursive: true });
133
191
  copyFileSync(
134
192
  join(dirname(fileURLToPath(import.meta.url)), 'catalog.js'),
135
193
  join(targetDir, 'scripts', 'kerf-component-catalog.mjs'),
136
194
  );
195
+ // The command's modules, imported relatively by the entry above. Their
196
+ // own package.json keeps them ES modules whatever the host package's type.
197
+ cpSync(
198
+ join(dirname(fileURLToPath(import.meta.url)), 'catalog'),
199
+ join(targetDir, 'scripts', 'catalog'),
200
+ { recursive: true },
201
+ );
202
+ writeFileSync(
203
+ join(targetDir, 'scripts', 'catalog', 'package.json'),
204
+ '{ "type": "module" }\n',
205
+ );
137
206
  for (const schema of [
138
207
  'component-metadata.schema.json',
139
208
  'component-composition.schema.json',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-kerf-component",
3
- "version": "5.0.0-beta.125",
3
+ "version": "5.0.0-beta.126",
4
4
  "description": "Scaffold a publishable kerf component package that already follows kerf's hard packaging rules.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -32,6 +32,7 @@
32
32
  "files": [
33
33
  "index.js",
34
34
  "catalog.js",
35
+ "catalog",
35
36
  "component-metadata.schema.json",
36
37
  "component-composition.schema.json",
37
38
  "component-catalog-extension.schema.json",
@@ -40,9 +41,21 @@
40
41
  "LICENSE"
41
42
  ],
42
43
  "scripts": {
44
+ "check": "npm run lint && npm test",
45
+ "lint": "eslint --max-warnings 0 .",
43
46
  "test": "node --test tests/*.test.js"
44
47
  },
45
- "dependencies": {
48
+ "peerDependencies": {
49
+ "typescript": "^5.0.0 || ^6.0.0"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "typescript": {
53
+ "optional": true
54
+ }
55
+ },
56
+ "devDependencies": {
57
+ "@eslint/js": "^9.39.5",
58
+ "eslint": "^9.39.5",
46
59
  "typescript": "^5.0.0 || ^6.0.0"
47
60
  }
48
61
  }
@@ -43,8 +43,8 @@
43
43
  "kerfjs": "^5.0.0-0"
44
44
  },
45
45
  "devDependencies": {
46
- "kerfjs": "^5.0.0-beta.125",
47
- "@kerfjs/ui": "^5.0.0-beta.125",
46
+ "kerfjs": "^5.0.0-beta.126",
47
+ "@kerfjs/ui": "^5.0.0-beta.126",
48
48
  "tsup": "^8",
49
49
  "typescript": "^5 || ^6"
50
50
  }