create-nestjs-monorepo 0.0.0-stage → 0.1.0

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 +27 -2
  2. package/index.mjs +154 -0
  3. package/package.json +22 -4
package/README.md CHANGED
@@ -1,3 +1,28 @@
1
- # Temporary Holding Version
1
+ # create-nestjs-monorepo
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Starts a project from the [nestjs-monorepo](https://github.com/lamberto15/nestjs-monorepo)
4
+ template: a NestJS 12 API, the web app it serves and a typed client, in one
5
+ pnpm workspace.
6
+
7
+ ```bash
8
+ pnpm create nestjs-monorepo my-app
9
+ ```
10
+
11
+ It clones the newest release of the template with its history, keeps the
12
+ template as the `template` git remote, names the project after its folder,
13
+ commits that on `main` and runs `pnpm install`. A later release is merged
14
+ like a branch: the template's
15
+ [docs/upgrading.md](https://github.com/lamberto15/nestjs-monorepo/blob/main/docs/upgrading.md).
16
+
17
+ | Option | What |
18
+ | ------------------- | -------------------------------------------------- |
19
+ | `--version <x.y.z>` | a release of the template (default: the newest) |
20
+ | `--from <git url>` | the template's repository (default: the one above) |
21
+ | `--no-install` | skip `pnpm install` |
22
+
23
+ Needs Node 26 or later, git and pnpm. The version in `package.json` is set
24
+ by the release workflow from the tag (`template.json`'s version): the
25
+ package and the template are released together.
26
+
27
+ Licence: [PolyForm Noncommercial 1.0.0](https://github.com/lamberto15/nestjs-monorepo/blob/main/LICENSE),
28
+ the template's.
package/index.mjs ADDED
@@ -0,0 +1,154 @@
1
+ #!/usr/bin/env node
2
+ // `pnpm create nestjs-monorepo <dir>`: a project from a release of the
3
+ // template. A clone of the release's tag with its history, the template's
4
+ // remote kept as `template`, so a later release is a `git merge` of its tag
5
+ // (docs/upgrading.md). A download without history (degit, "Use this
6
+ // template") would leave nothing to merge against. Node's standard library
7
+ // and git only: nothing to install before the template is there.
8
+ import { spawnSync } from 'node:child_process';
9
+ import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
10
+ import path from 'node:path';
11
+ import { parseArgs } from 'node:util';
12
+
13
+ const manifest = JSON.parse(
14
+ readFileSync(new URL('./package.json', import.meta.url), 'utf8'),
15
+ );
16
+ /** The template's repository: this package's own (package.json). */
17
+ const TEMPLATE = manifest.repository.url.replace(/^git\+/, '');
18
+
19
+ const USAGE = `Usage: pnpm create nestjs-monorepo <dir> [--version <x.y.z>] [--from <git url>] [--no-install]
20
+
21
+ --version a release of the template (default: the newest)
22
+ --from the template's repository (default: ${TEMPLATE})
23
+ --no-install skip pnpm install`;
24
+
25
+ function run(command, args, options = {}) {
26
+ const result = spawnSync(command, args, { encoding: 'utf8', ...options });
27
+ if (result.error) throw new Error(`${command}: ${result.error.message}`);
28
+ if (result.status !== 0) {
29
+ throw new Error(
30
+ `${command} ${args.join(' ')} failed${result.stderr ? `:\n${result.stderr.trim()}` : ''}`,
31
+ );
32
+ }
33
+ return result.stdout ?? '';
34
+ }
35
+
36
+ const compare = (a, b) => {
37
+ for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] - b[i];
38
+ return 0;
39
+ };
40
+
41
+ /** The newest release: the highest `vX.Y.Z` tag of the repository (pre-releases left out). */
42
+ export function newestVersion(from) {
43
+ const versions = run('git', ['ls-remote', '--tags', '--refs', from, 'v*'])
44
+ .split('\n')
45
+ .map((line) => /refs\/tags\/v(\d+)\.(\d+)\.(\d+)$/.exec(line))
46
+ .filter(Boolean)
47
+ .map((match) => match.slice(1).map(Number));
48
+ if (versions.length === 0)
49
+ throw new Error(`${from} has no release tag (v1.2.3).`);
50
+ return versions.sort(compare).at(-1).join('.');
51
+ }
52
+
53
+ /** A package name from the folder's: lower case, letters, digits and dashes. */
54
+ export const projectName = (dir) =>
55
+ path
56
+ .basename(path.resolve(dir))
57
+ .toLowerCase()
58
+ .replace(/[^a-z0-9-]+/g, '-')
59
+ .replace(/^-+|-+$/g, '') || 'app';
60
+
61
+ function replaceIn(dir, file, from, to) {
62
+ const target = path.join(dir, file);
63
+ if (!existsSync(target)) return;
64
+ const text = readFileSync(target, 'utf8');
65
+ writeFileSync(target, text.replace(from, to));
66
+ }
67
+
68
+ /**
69
+ * The copy: cloned at `v<version>`, on a `main` branch of its own, the
70
+ * project's name where the template carries its own (the root package, the
71
+ * development services' name, the README's title: docs/getting-started.md,
72
+ * "Renaming"), committed.
73
+ */
74
+ export function create(dir, { version, from = TEMPLATE } = {}) {
75
+ if (existsSync(dir) && readdirSync(dir).length > 0) {
76
+ throw new Error(`${dir} exists and is not empty.`);
77
+ }
78
+ const release = version ?? newestVersion(from);
79
+ const name = projectName(dir);
80
+ run('git', [
81
+ 'clone',
82
+ '--quiet',
83
+ '--origin',
84
+ 'template',
85
+ '--branch',
86
+ `v${release}`,
87
+ from,
88
+ dir,
89
+ ]);
90
+ run('git', ['switch', '--quiet', '-c', 'main'], { cwd: dir });
91
+ replaceIn(dir, 'package.json', /"name": "[^"]*"/, `"name": "${name}"`);
92
+ replaceIn(dir, 'compose.dev.yaml', /^name: .*$/m, `name: ${name}-dev`);
93
+ replaceIn(dir, 'README.md', /^# .*$/m, `# ${name}`);
94
+ run(
95
+ 'git',
96
+ [
97
+ 'commit',
98
+ '--quiet',
99
+ '--all',
100
+ '-m',
101
+ `chore: start ${name} from nestjs-monorepo v${release}`,
102
+ ],
103
+ {
104
+ cwd: dir,
105
+ },
106
+ );
107
+ return { name, release };
108
+ }
109
+
110
+ if (import.meta.main) {
111
+ let parsed;
112
+ try {
113
+ parsed = parseArgs({
114
+ allowPositionals: true,
115
+ options: {
116
+ version: { type: 'string' },
117
+ from: { type: 'string' },
118
+ 'no-install': { type: 'boolean' },
119
+ help: { type: 'boolean', short: 'h' },
120
+ },
121
+ });
122
+ } catch (error) {
123
+ console.error(`${error.message}\n\n${USAGE}`);
124
+ process.exit(2);
125
+ }
126
+ const { values, positionals } = parsed;
127
+ if (values.help || positionals.length !== 1) {
128
+ console.log(USAGE);
129
+ process.exit(values.help ? 0 : 2);
130
+ }
131
+ const [dir] = positionals;
132
+ try {
133
+ const { name, release } = create(dir, {
134
+ version: values.version,
135
+ from: values.from,
136
+ });
137
+ console.log(`${name}: nestjs-monorepo v${release}, in ${dir}.`);
138
+ if (!values['no-install']) {
139
+ spawnSync('pnpm', ['install'], { cwd: dir, stdio: 'inherit' });
140
+ }
141
+ console.log(`
142
+ Next:
143
+ cd ${dir}
144
+ pnpm services:up # PostgreSQL and Redis
145
+ pnpm run setup # apps/api/.env, with generated keys
146
+ pnpm dev # http://localhost:5173
147
+
148
+ Your own repository: git remote add origin <url> && git push -u origin main
149
+ A newer release of the template: docs/upgrading.md (pnpm template:check first).`);
150
+ } catch (error) {
151
+ console.error(error.message);
152
+ process.exit(1);
153
+ }
154
+ }
package/package.json CHANGED
@@ -1,6 +1,24 @@
1
1
  {
2
2
  "name": "create-nestjs-monorepo",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Starts a project from the nestjs-monorepo template: pnpm create nestjs-monorepo my-app",
5
+ "license": "PolyForm-Noncommercial-1.0.0",
6
+ "type": "module",
7
+ "bin": {
8
+ "create-nestjs-monorepo": "index.mjs"
9
+ },
10
+ "files": [
11
+ "index.mjs"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/lamberto15/nestjs-monorepo.git",
16
+ "directory": "packages/create-nestjs-monorepo"
17
+ },
18
+ "engines": {
19
+ "node": ">=26"
20
+ },
21
+ "scripts": {
22
+ "test": "node --test"
23
+ }
24
+ }