create-ts-monorepo-starter 0.1.0 → 0.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 +2 -2
  2. package/dist/cli.js +107 -5
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -5,11 +5,11 @@ Create a TypeScript monorepo for CLIs, libraries and browser libraries from the
5
5
  Changesets, CI and a release workflow, plus the example packages you pick. The project passes its own CI on the first
6
6
  commit.
7
7
 
8
+ ![Running npm create ts-monorepo-starter@latest: choosing a folder, the @acme scope and the lib and cli examples, then installing and committing](https://raw.githubusercontent.com/prosdevlab/create-ts-monorepo-starter/main/docs/demo/demo.gif)
9
+
8
10
  ```bash
9
11
  npm create ts-monorepo-starter@latest my-tools
10
12
  pnpm create ts-monorepo-starter my-tools
11
- yarn create ts-monorepo-starter my-tools
12
- bun create ts-monorepo-starter my-tools
13
13
  ```
14
14
 
15
15
  Runs on Node 22.13 or later. `--help` lists the options; `-y` asks nothing.
package/dist/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- import { cp, rename, rm, readdir, rmdir, readFile, writeFile, open, stat, mkdir, realpath, lstat } from 'node:fs/promises';
3
- import { isAbsolute, dirname, basename, resolve, join, relative, sep } from 'node:path';
2
+ import { mkdtemp, writeFile, rm, access, cp, rename, readdir, rmdir, readFile, open, stat, mkdir, realpath, lstat } from 'node:fs/promises';
3
+ import { join, isAbsolute, dirname, basename, resolve, relative, sep } from 'node:path';
4
4
  import { parseArgs, styleText, stripVTControlCharacters } from 'node:util';
5
5
  import { spawn } from 'node:child_process';
6
6
  import { constants, tmpdir } from 'node:os';
@@ -64,6 +64,7 @@ function parseArgv(argv) {
64
64
  yes: values.yes ?? false,
65
65
  help: values.help ?? false,
66
66
  version: values.version ?? false,
67
+ check: values.check ?? false,
67
68
  };
68
69
  if (positionals[0] !== undefined)
69
70
  options.folder = positionals[0];
@@ -92,6 +93,7 @@ function parse(argv) {
92
93
  yes: { type: 'boolean', short: 'y' },
93
94
  help: { type: 'boolean', short: 'h' },
94
95
  version: { type: 'boolean', short: 'v' },
96
+ check: { type: 'boolean' },
95
97
  },
96
98
  });
97
99
  }
@@ -286,6 +288,61 @@ async function detectPnpm(exec = defaultExec, cwd = tmpdir()) {
286
288
  const [major, minor] = [Number(match[1]), Number(match[2])];
287
289
  return { kind: major < 10 || (major === 10 && minor < 26) ? 'old' : 'ok', version };
288
290
  }
291
+ /** A toolchain problem the user has to fix before anything is created; `lines` say how. */
292
+ class ToolchainError extends Error {
293
+ lines;
294
+ constructor(message, lines) {
295
+ super(message);
296
+ this.lines = lines;
297
+ this.name = 'ToolchainError';
298
+ }
299
+ }
300
+ const PNPM_HELP = 'https://github.com/prosdevlab/create-ts-monorepo-starter#pnpm';
301
+ const fileExists = (path) => access(path).then(() => true, () => false);
302
+ /**
303
+ * Runs `pnpm --version` in an empty folder pinned to `spec` (`pnpm@12.15.0`), where pnpm switches
304
+ * itself to that version as the install will. Throws a ToolchainError when the switch fails.
305
+ */
306
+ async function checkSwitch(spec, found, exec = defaultExec, root = tmpdir(), exists = fileExists) {
307
+ const dir = await mkdtemp(join(root, 'ctms-pnpm-'));
308
+ try {
309
+ await writeFile(join(dir, 'package.json'), `${JSON.stringify({ packageManager: spec })}\n`);
310
+ const result = await exec('pnpm', ['--version'], { cwd: dir }).catch(() => null);
311
+ if (result?.code === 0)
312
+ return;
313
+ const output = result ? `${result.stdout}\n${result.stderr}` : '';
314
+ const installer = switchInstaller(output);
315
+ const runnable = installer !== undefined && (await exists(installer));
316
+ throw switchFailure(spec, found, output, runnable ? installer : undefined);
317
+ }
318
+ finally {
319
+ await rm(dir, { recursive: true, force: true });
320
+ }
321
+ }
322
+ /**
323
+ * The install script of the pnpm a failed switch downloaded, read from pnpm's error: it says the CLI
324
+ * "is missing at <tools>/bin" when the script that puts the binary there never ran.
325
+ */
326
+ function switchInstaller(output) {
327
+ const tools = /missing at "([^"]+?)[\\/]bin"/.exec(output)?.[1];
328
+ return tools === undefined ? undefined : join(tools, 'node_modules', 'pnpm', 'install.js');
329
+ }
330
+ /** The stop for a failed switch: pnpm's words, the fix (`installer`, or the README), and a retry. */
331
+ function switchFailure(spec, found, output, installer) {
332
+ const version = spec.slice('pnpm@'.length);
333
+ const said = output
334
+ .split(/\r?\n/)
335
+ .map((line) => line.trim())
336
+ .filter(Boolean)
337
+ .slice(0, 2);
338
+ return new ToolchainError(`pnpm ${found} couldn't switch to pnpm ${version}, which this project needs`, [
339
+ ...said.map((line) => `pnpm: ${line}`),
340
+ installer
341
+ ? `Fix: node ${installer}`
342
+ : `Fix your pnpm so it can switch to ${version}: ${PNPM_HELP}`,
343
+ 'Then run this command again. Nothing was created.',
344
+ ]);
345
+ }
289
346
  /** The install command for `dir`, or null when pnpm is too old to use. */
290
347
  function installCommand(status, dir, lockfileOnly) {
291
348
  if (!isAbsolute(dir))
@@ -3528,6 +3585,7 @@ Options
3528
3585
  --no-install don't install dependencies
3529
3586
  --force move a non-empty folder aside first
3530
3587
  -y, --yes accept every default, ask nothing
3588
+ --check check that pnpm is ready, create nothing
3531
3589
  -h, --help show this help
3532
3590
  -v, --version show the CLI and template versions
3533
3591
 
@@ -4040,6 +4098,12 @@ async function run(plan, ui, deps = {}) {
4040
4098
  },
4041
4099
  });
4042
4100
  try {
4101
+ // pnpm first: a pnpm that can't switch to the template's version stops the run before any file.
4102
+ const spec = pnpmSpec(await readFile(join(from, 'package.json'), 'utf8'));
4103
+ const pin = spec.slice('pnpm@'.length);
4104
+ const pnpm = await detectPnpm(exec);
4105
+ if (pnpm.kind === 'ok')
4106
+ await checkSwitch(spec, pnpm.version, exec);
4043
4107
  await (deps.generate ?? generate)({
4044
4108
  from,
4045
4109
  to: temp,
@@ -4075,8 +4139,6 @@ async function run(plan, ui, deps = {}) {
4075
4139
  }
4076
4140
  }
4077
4141
  }
4078
- const pin = pnpmSpec(await readFile(join(from, 'package.json'), 'utf8')).slice('pnpm@'.length);
4079
- const pnpm = await detectPnpm(exec);
4080
4142
  const command = installCommand(pnpm, temp, !plan.install);
4081
4143
  let installed = false;
4082
4144
  let seconds = 0;
@@ -4120,7 +4182,7 @@ async function run(plan, ui, deps = {}) {
4120
4182
  outcome = await initAndCommit(temp, message, exec);
4121
4183
  }
4122
4184
  catch (error) {
4123
- // Ruling 11: the files are generated and correct, so a failed commit only warns.
4185
+ // The files are generated and correct, so a failed commit only warns.
4124
4186
  outcome = 'failed';
4125
4187
  ui.warn("Couldn't make the first commit", [
4126
4188
  firstLine(error),
@@ -4175,6 +4237,8 @@ async function run(plan, ui, deps = {}) {
4175
4237
 
4176
4238
  const USAGE = 2;
4177
4239
  const FAILURE = 1;
4240
+ /** The machine needs fixing first (pnpm can't switch versions, or is too old); nothing was created. */
4241
+ const TOOLCHAIN = 3;
4178
4242
  const INTERRUPTED = 130;
4179
4243
  const usage = (message) => {
4180
4244
  process.stderr.write(`${message}\n`);
@@ -4237,6 +4301,10 @@ function report(ui, error) {
4237
4301
  ui.fail(CANCELLED, []);
4238
4302
  return INTERRUPTED;
4239
4303
  }
4304
+ if (error instanceof ToolchainError) {
4305
+ ui.fail(error.message, error.lines);
4306
+ return TOOLCHAIN;
4307
+ }
4240
4308
  if (error instanceof RunError) {
4241
4309
  ui.fail(error.message, error.lines);
4242
4310
  return FAILURE;
@@ -4245,6 +4313,38 @@ function report(ui, error) {
4245
4313
  ui.fail(title, lines);
4246
4314
  return FAILURE;
4247
4315
  }
4316
+ /** `--check`: is this machine's pnpm ready for the template's? Creates nothing. */
4317
+ async function check(ui, exec) {
4318
+ const spec = pnpmSpec();
4319
+ const version = spec.slice('pnpm@'.length);
4320
+ ui.intro();
4321
+ const pnpm = await detectPnpm(exec);
4322
+ if (pnpm.kind === 'none') {
4323
+ ui.warn("pnpm isn't installed", [
4324
+ `Creating works: the install uses a temporary pnpm ${version} (npm exec).`,
4325
+ `Working in the project needs pnpm 10.26 or newer: https://pnpm.io/installation`,
4326
+ ]);
4327
+ return 0;
4328
+ }
4329
+ if (pnpm.kind === 'old') {
4330
+ ui.fail(`pnpm ${pnpm.version} is too old for this project`, [
4331
+ `This project needs pnpm 10.26 or newer, which switches itself to pnpm ${version}.`,
4332
+ 'Upgrade pnpm (https://pnpm.io/installation), then run --check again.',
4333
+ ]);
4334
+ return TOOLCHAIN;
4335
+ }
4336
+ try {
4337
+ await checkSwitch(spec, pnpm.version, exec);
4338
+ }
4339
+ catch (error) {
4340
+ if (!(error instanceof ToolchainError))
4341
+ throw error;
4342
+ ui.fail(error.message, error.lines);
4343
+ return TOOLCHAIN;
4344
+ }
4345
+ ui.step(`Ready: pnpm ${pnpm.version} switches to pnpm ${version} for this project`);
4346
+ return 0;
4347
+ }
4248
4348
  /** Runs the command and resolves with its exit code. */
4249
4349
  async function main(argv, io = {}) {
4250
4350
  const parsed = parseArgv(argv);
@@ -4259,6 +4359,8 @@ async function main(argv, io = {}) {
4259
4359
  process.stdout.write(`${versionText()}\n`);
4260
4360
  return 0;
4261
4361
  }
4362
+ if (options.check)
4363
+ return check(io.ui ?? plainUi(), io.exec);
4262
4364
  const cwd = io.cwd ?? process.cwd();
4263
4365
  const interactive = !options.yes && (io.interactive ?? Boolean(process.stdin.isTTY && process.stdout.isTTY));
4264
4366
  // A bad flag fails at once, before any question.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-ts-monorepo-starter",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Create a TypeScript monorepo from the ts-monorepo-starter template",
5
5
  "keywords": [
6
6
  "create",
@@ -36,7 +36,7 @@
36
36
  "@rollup/plugin-node-resolve": "16.0.3",
37
37
  "@tsconfig/node22": "22.0.6",
38
38
  "@types/node": "22.20.5",
39
- "rollup": "4.65.0",
39
+ "rollup": "4.66.0",
40
40
  "rollup-plugin-license": "3.7.1",
41
41
  "typescript": "7.0.2"
42
42
  },