@nx-devkit/prepare-for-release 0.0.0 → 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.
- package/README.md +90 -0
- package/dist/executors/publish-placeholder/executor.d.mts +32 -0
- package/dist/executors/publish-placeholder/executor.mjs +284 -0
- package/dist/generators/init/generator.d.mts +10 -0
- package/dist/generators/init/generator.mjs +73 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.mjs +4 -0
- package/dist/plugin.d.mts +23 -0
- package/dist/plugin.mjs +52 -0
- package/executors.json +9 -0
- package/package.json +48 -8
- package/src/executors/publish-placeholder/schema.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# @nx-devkit/prepare-for-release
|
|
2
|
+
|
|
3
|
+
Bootstraps a workspace of packages onto the npm registry by publishing minimal `0.0.0` placeholders. Built so any Nx monorepo can use it.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
| Surface | Kind | Purpose |
|
|
8
|
+
|---|---|---|
|
|
9
|
+
| `@nx-devkit/prepare-for-release:publish-placeholder` | Executor | Scans `packages/*`, publishes a `0.0.0` placeholder for any package not yet on the registry. |
|
|
10
|
+
| `@nx-devkit/prepare-for-release:init` | Generator | Adds the plugin to `nx.json`, creates a `tools` project with a `prepare-for-release` target, prints the post-setup checklist. |
|
|
11
|
+
| `@nx-devkit/prepare-for-release` (createNodesV2) | Plugin | Detects a `tools/project.json` referencing the executor and re-affirms the `prepare-for-release` target. |
|
|
12
|
+
|
|
13
|
+
The executor is idempotent — already-published packages are skipped — and never modifies the source `package.json` (the placeholder tarball is built in a temp directory). Use `dryRun: true` to preview.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bun add -D @nx-devkit/prepare-for-release
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Register in nx.json
|
|
22
|
+
|
|
23
|
+
```jsonc
|
|
24
|
+
{
|
|
25
|
+
"plugins": ["@nx-devkit/prepare-for-release"]
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Or run the generator:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
bunx nx g @nx-devkit/prepare-for-release:init
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Run
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
bunx nx run tools:prepare-for-release
|
|
39
|
+
# or with options
|
|
40
|
+
bunx nx run tools:prepare-for-release --placeholderTag=alpha --placeholderVersion=0.0.1 --dryRun
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Options
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
export interface NxPrepareForReleaseOptions {
|
|
47
|
+
scope?: string[]; // default: derived from packages/* names
|
|
48
|
+
placeholderTag?: string; // default: "placeholder"
|
|
49
|
+
placeholderVersion?: string; // default: "0.0.0"
|
|
50
|
+
registry?: string; // default: "https://registry.npmjs.org/"
|
|
51
|
+
dryRun?: boolean; // default: false
|
|
52
|
+
trust?: boolean; // default: false — run `npm trust github` for all packages including already-published (requires MFA)
|
|
53
|
+
trustRepo?: string; // default: process.env.NPM_TRUST_REPO or process.env.GITHUB_REPOSITORY or "nx-devkit/nx.ts"
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Example output
|
|
58
|
+
|
|
59
|
+
```text
|
|
60
|
+
> nx run tools:prepare-for-release
|
|
61
|
+
|
|
62
|
+
[@nx-devkit/prepare-for-release] Checking 4 packages
|
|
63
|
+
[@nx-devkit/prepare-for-release] @nx-devkit/tsdown: 404 → publishing 0.0.0 placeholder
|
|
64
|
+
[@nx-devkit/prepare-for-release] @nx-devkit/oxlint: 404 → publishing 0.0.0 placeholder
|
|
65
|
+
[@nx-devkit/prepare-for-release] @nx-devkit/biome: 0.5.0 already on registry, skipping
|
|
66
|
+
[@nx-devkit/prepare-for-release] @nx-devkit/typescript: 404 → publishing 0.0.0 placeholder
|
|
67
|
+
|
|
68
|
+
Published: @nx-devkit/tsdown, @nx-devkit/oxlint, @nx-devkit/typescript
|
|
69
|
+
Skipped: @nx-devkit/biome
|
|
70
|
+
|
|
71
|
+
Run these locally (requires MFA) to enable GitHub OIDC trusted publishing:
|
|
72
|
+
|
|
73
|
+
npm trust github @nx-devkit/tsdown --file release.yml --repo your-org/your-repo --allow-publish
|
|
74
|
+
npm trust github @nx-devkit/oxlint --file release.yml --repo your-org/your-repo --allow-publish
|
|
75
|
+
npm trust github @nx-devkit/typescript --file release.yml --repo your-org/your-repo --allow-publish
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## When to use
|
|
79
|
+
|
|
80
|
+
- Adding a brand new Nx plugin package to a workspace that hasn't shipped to npm yet.
|
|
81
|
+
- Bootstrapping a multi-package monorepo so that downstream `nx release` + OIDC trusted publishing can take over from there.
|
|
82
|
+
- You want the placeholder publish to be idempotent and offline-friendly (it just shells out to `npm view` and `npm publish`).
|
|
83
|
+
|
|
84
|
+
## Why not `bun publish`?
|
|
85
|
+
|
|
86
|
+
`bun publish` does not yet support npm OIDC trusted publishing. The CI release workflow uses `npx nx release publish` (which uses the npm CLI). The executor also calls the npm CLI directly to keep the bootstrap flow consistent.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/executors/publish-placeholder/executor.d.ts
|
|
2
|
+
interface NxPrepareForReleaseOptions {
|
|
3
|
+
/** Package scopes to check. Default: derived from packages/* names in the workspace. */
|
|
4
|
+
scope?: string[];
|
|
5
|
+
/** Npm dist-tag applied to the placeholder publish. Default: "placeholder". */
|
|
6
|
+
placeholderTag?: string;
|
|
7
|
+
/** Version written into the temporary placeholder package.json. Default: "0.0.0". */
|
|
8
|
+
placeholderVersion?: string;
|
|
9
|
+
/** Npm registry URL. Default: https://registry.npmjs.org/. */
|
|
10
|
+
registry?: string;
|
|
11
|
+
/** If true, do not actually publish or pack; just report what would happen. Default: false. */
|
|
12
|
+
dryRun?: boolean;
|
|
13
|
+
/** If true, run `npm trust github` for all packages including already-published (requires MFA). Default: false. */
|
|
14
|
+
trust?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* `owner/repo` slug used to build the `npm trust github` command.
|
|
17
|
+
* Falls back to `NPM_TRUST_REPO` env var, then `GITHUB_REPOSITORY` env var.
|
|
18
|
+
* Throws if none are set.
|
|
19
|
+
*/
|
|
20
|
+
trustRepo?: string;
|
|
21
|
+
}
|
|
22
|
+
interface PublishPlaceholderResult {
|
|
23
|
+
success: boolean;
|
|
24
|
+
published: string[];
|
|
25
|
+
skipped: string[];
|
|
26
|
+
trustCommands: string[];
|
|
27
|
+
}
|
|
28
|
+
declare function publishPlaceholderExecutor(options: NxPrepareForReleaseOptions, context: {
|
|
29
|
+
root: string;
|
|
30
|
+
}): Promise<PublishPlaceholderResult>;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { NxPrepareForReleaseOptions, PublishPlaceholderResult, publishPlaceholderExecutor as default, publishPlaceholderExecutor };
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
6
|
+
import { glob } from "tinyglobby";
|
|
7
|
+
//#region src/executors/publish-placeholder/executor.ts
|
|
8
|
+
const DEFAULT_REGISTRY = "https://registry.npmjs.org/";
|
|
9
|
+
const DEFAULT_TAG = "placeholder";
|
|
10
|
+
const DEFAULT_VERSION = "0.0.0";
|
|
11
|
+
const TRUST_REPO_RE = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
|
|
12
|
+
const NPM_SUBPROCESS_TIMEOUT_MS = 12e4;
|
|
13
|
+
function resolveOptions(options) {
|
|
14
|
+
return {
|
|
15
|
+
dryRun: options.dryRun ?? false,
|
|
16
|
+
placeholderTag: options.placeholderTag ?? DEFAULT_TAG,
|
|
17
|
+
placeholderVersion: options.placeholderVersion ?? DEFAULT_VERSION,
|
|
18
|
+
registry: options.registry ?? DEFAULT_REGISTRY,
|
|
19
|
+
scope: options.scope,
|
|
20
|
+
trust: options.trust ?? false,
|
|
21
|
+
trustRepo: resolveTrustRepo(options.trustRepo)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function resolveNpmCommand() {
|
|
25
|
+
return "npm";
|
|
26
|
+
}
|
|
27
|
+
function resolveTrustRepo(option) {
|
|
28
|
+
const raw = option ?? process.env.NPM_TRUST_REPO ?? process.env.GITHUB_REPOSITORY ?? "nx-devkit/nx.ts";
|
|
29
|
+
if (!TRUST_REPO_RE.test(raw)) throw new Error(`Invalid trustRepo "${raw}": expected "owner/repo" slug (e.g. nx-devkit/nx.ts). Override via options.trustRepo or NPM_TRUST_REPO env var.`);
|
|
30
|
+
return raw;
|
|
31
|
+
}
|
|
32
|
+
function viewArgs(pkgName, registry) {
|
|
33
|
+
return [
|
|
34
|
+
"view",
|
|
35
|
+
pkgName,
|
|
36
|
+
"version",
|
|
37
|
+
"--registry",
|
|
38
|
+
registry,
|
|
39
|
+
"--json"
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
function packArgs(packDestination) {
|
|
43
|
+
return [
|
|
44
|
+
"pack",
|
|
45
|
+
"--pack-destination",
|
|
46
|
+
packDestination
|
|
47
|
+
];
|
|
48
|
+
}
|
|
49
|
+
function publishArgs(tarball, registry, tag) {
|
|
50
|
+
return [
|
|
51
|
+
"publish",
|
|
52
|
+
tarball,
|
|
53
|
+
"--access",
|
|
54
|
+
"public",
|
|
55
|
+
"--tag",
|
|
56
|
+
tag,
|
|
57
|
+
"--registry",
|
|
58
|
+
registry
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
function spawnWithTimeout(command, args, options) {
|
|
62
|
+
return spawnSync(command, args, {
|
|
63
|
+
...options,
|
|
64
|
+
timeout: NPM_SUBPROCESS_TIMEOUT_MS
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
async function readPackageJson(pkgRoot) {
|
|
68
|
+
const raw = await readFile(join(pkgRoot, "package.json"), "utf8");
|
|
69
|
+
return JSON.parse(raw);
|
|
70
|
+
}
|
|
71
|
+
function readPackageJsonSafe(pkgJsonPath) {
|
|
72
|
+
let fileContent;
|
|
73
|
+
try {
|
|
74
|
+
fileContent = readFileSync(pkgJsonPath, "utf8");
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (error instanceof SyntaxError) {
|
|
77
|
+
console.warn(`Skipping ${pkgJsonPath}: invalid JSON (${error.message})`);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(fileContent);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error instanceof SyntaxError) {
|
|
86
|
+
console.warn(`Skipping ${pkgJsonPath}: invalid JSON (${error.message})`);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function matchesScope(name, scope) {
|
|
93
|
+
if (!scope || scope.length === 0) return true;
|
|
94
|
+
return scope.some((s) => name.startsWith(s));
|
|
95
|
+
}
|
|
96
|
+
function isNotFoundStderr(stderr) {
|
|
97
|
+
return /\b(E404|404|not\s*found|ENOTFOUND_NOT_PUBLISHED)\b/i.test(stderr);
|
|
98
|
+
}
|
|
99
|
+
async function buildPlaceholderTarball(pkgRoot, pkgName, placeholderVersion, registry) {
|
|
100
|
+
const tempDir = await mkdtemp(join(tmpdir(), "nx-prepare-placeholder-"));
|
|
101
|
+
const stagedPkgRoot = join(tempDir, pkgName);
|
|
102
|
+
try {
|
|
103
|
+
await mkdir(stagedPkgRoot, { recursive: true });
|
|
104
|
+
const original = await readPackageJson(pkgRoot);
|
|
105
|
+
const placeholder = {
|
|
106
|
+
author: original.author,
|
|
107
|
+
bugs: original.bugs,
|
|
108
|
+
description: `Placeholder for ${pkgName} published by @nx-devkit/prepare-for-release.`,
|
|
109
|
+
homepage: original.homepage,
|
|
110
|
+
license: typeof original.license === "string" ? original.license : "MIT",
|
|
111
|
+
name: pkgName,
|
|
112
|
+
publishConfig: {
|
|
113
|
+
access: "public",
|
|
114
|
+
registry
|
|
115
|
+
},
|
|
116
|
+
repository: original.repository,
|
|
117
|
+
type: typeof original.type === "string" ? original.type : void 0,
|
|
118
|
+
version: placeholderVersion
|
|
119
|
+
};
|
|
120
|
+
const placeholderJson = JSON.stringify(placeholder, null, 2);
|
|
121
|
+
await writeFile(join(stagedPkgRoot, "package.json"), placeholderJson, "utf8");
|
|
122
|
+
const packResult = spawnWithTimeout(resolveNpmCommand(), packArgs(tempDir), {
|
|
123
|
+
cwd: stagedPkgRoot,
|
|
124
|
+
encoding: "utf8"
|
|
125
|
+
});
|
|
126
|
+
if (packResult.status !== 0) throw new Error(`npm pack failed for ${pkgName} (exit ${packResult.status}): ${packResult.stderr ?? ""}`);
|
|
127
|
+
const tarballName = (packResult.stdout ?? "").toString().trim().split("\n").pop()?.trim();
|
|
128
|
+
if (!tarballName) throw new Error(`npm pack produced no tarball name for ${pkgName}`);
|
|
129
|
+
return {
|
|
130
|
+
tarballPath: join(tempDir, tarballName),
|
|
131
|
+
tempDir
|
|
132
|
+
};
|
|
133
|
+
} catch (error) {
|
|
134
|
+
await rm(tempDir, {
|
|
135
|
+
force: true,
|
|
136
|
+
recursive: true
|
|
137
|
+
}).catch(() => void 0);
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function isPublished(pkgName, registry) {
|
|
142
|
+
const result = spawnWithTimeout(resolveNpmCommand(), viewArgs(pkgName, registry), { encoding: "utf8" });
|
|
143
|
+
if (result.status !== 0) {
|
|
144
|
+
const stderr = (result.stderr ?? "").toString();
|
|
145
|
+
if (isNotFoundStderr(stderr)) return false;
|
|
146
|
+
throw new Error(`npm view failed for ${pkgName} (exit ${result.status}): ${stderr || "<no stderr>"}`);
|
|
147
|
+
}
|
|
148
|
+
const stdout = (result.stdout ?? "").toString().trim();
|
|
149
|
+
if (!stdout) return false;
|
|
150
|
+
try {
|
|
151
|
+
const parsed = JSON.parse(stdout);
|
|
152
|
+
if (typeof parsed === "string") return parsed.length > 0;
|
|
153
|
+
if (parsed && typeof parsed === "object" && "version" in parsed) return Boolean(parsed.version);
|
|
154
|
+
} catch {
|
|
155
|
+
return stdout.length > 0;
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
function trustCommandFor(pkgName, trustRepo, registry) {
|
|
160
|
+
const base = `npm trust github ${pkgName} --file release.yml --repo ${trustRepo} --allow-publish --yes`;
|
|
161
|
+
return registry && registry !== DEFAULT_REGISTRY ? `${base} --registry ${registry}` : base;
|
|
162
|
+
}
|
|
163
|
+
function trustArgs(pkgName, trustRepo, registry) {
|
|
164
|
+
const args = [
|
|
165
|
+
"trust",
|
|
166
|
+
"github",
|
|
167
|
+
pkgName,
|
|
168
|
+
"--file",
|
|
169
|
+
"release.yml",
|
|
170
|
+
"--repo",
|
|
171
|
+
trustRepo,
|
|
172
|
+
"--allow-publish",
|
|
173
|
+
"--yes"
|
|
174
|
+
];
|
|
175
|
+
if (registry && registry !== DEFAULT_REGISTRY) args.push("--registry", registry);
|
|
176
|
+
return args;
|
|
177
|
+
}
|
|
178
|
+
function runTrustFor(pkgName, trustRepo, registry) {
|
|
179
|
+
const npmCmd = resolveNpmCommand();
|
|
180
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
181
|
+
const child = spawn(npmCmd, trustArgs(pkgName, trustRepo, registry), { stdio: [
|
|
182
|
+
"inherit",
|
|
183
|
+
"inherit",
|
|
184
|
+
"pipe"
|
|
185
|
+
] });
|
|
186
|
+
let stderr = "";
|
|
187
|
+
const timer = setTimeout(() => {
|
|
188
|
+
child.kill();
|
|
189
|
+
rejectPromise(/* @__PURE__ */ new Error(`npm trust github timed out for ${pkgName}`));
|
|
190
|
+
}, NPM_SUBPROCESS_TIMEOUT_MS);
|
|
191
|
+
child.stderr?.on("data", (chunk) => {
|
|
192
|
+
stderr += chunk.toString();
|
|
193
|
+
process.stderr.write(chunk);
|
|
194
|
+
});
|
|
195
|
+
child.on("error", (err) => {
|
|
196
|
+
clearTimeout(timer);
|
|
197
|
+
rejectPromise(err);
|
|
198
|
+
});
|
|
199
|
+
child.on("close", (code) => {
|
|
200
|
+
clearTimeout(timer);
|
|
201
|
+
if (code !== 0) {
|
|
202
|
+
rejectPromise(/* @__PURE__ */ new Error(`npm trust github failed for ${pkgName} (exit ${code})${stderr ? `: ${stderr}` : ""}`));
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
console.log(` ✓ ${pkgName}`);
|
|
206
|
+
resolvePromise();
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
async function publishOnePackage(pkgRoot, name, resolved) {
|
|
211
|
+
const { tarballPath, tempDir } = await buildPlaceholderTarball(pkgRoot, name, resolved.placeholderVersion, resolved.registry);
|
|
212
|
+
try {
|
|
213
|
+
const publishResult = spawnWithTimeout(resolveNpmCommand(), publishArgs(tarballPath, resolved.registry, resolved.placeholderTag), {
|
|
214
|
+
encoding: "utf8",
|
|
215
|
+
stdio: "inherit"
|
|
216
|
+
});
|
|
217
|
+
if (publishResult.status !== 0) throw new Error(`npm publish failed for ${name} (exit ${publishResult.status})`);
|
|
218
|
+
} finally {
|
|
219
|
+
await rm(tempDir, {
|
|
220
|
+
force: true,
|
|
221
|
+
recursive: true
|
|
222
|
+
}).catch(() => void 0);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async function processPackage(pkgJsonPath, resolved, acc) {
|
|
226
|
+
const parsed = readPackageJsonSafe(pkgJsonPath);
|
|
227
|
+
if (!parsed) return "ignored";
|
|
228
|
+
const name = typeof parsed.name === "string" ? parsed.name : null;
|
|
229
|
+
if (!name) return "ignored";
|
|
230
|
+
if (!matchesScope(name, resolved.scope)) return "ignored";
|
|
231
|
+
if (isPublished(name, resolved.registry)) {
|
|
232
|
+
acc.skipped.push(name);
|
|
233
|
+
return "skipped";
|
|
234
|
+
}
|
|
235
|
+
if (resolved.dryRun) {
|
|
236
|
+
acc.published.push(name);
|
|
237
|
+
acc.trustCommands.push(trustCommandFor(name, resolved.trustRepo, resolved.registry));
|
|
238
|
+
return "published";
|
|
239
|
+
}
|
|
240
|
+
await publishOnePackage(dirname(pkgJsonPath), name, resolved);
|
|
241
|
+
acc.published.push(name);
|
|
242
|
+
acc.trustCommands.push(trustCommandFor(name, resolved.trustRepo, resolved.registry));
|
|
243
|
+
return "published";
|
|
244
|
+
}
|
|
245
|
+
async function publishPlaceholderExecutor(options, context) {
|
|
246
|
+
const resolved = resolveOptions(options);
|
|
247
|
+
const pkgDirs = await glob(["packages/*/package.json"], {
|
|
248
|
+
absolute: true,
|
|
249
|
+
cwd: context.root,
|
|
250
|
+
onlyFiles: true
|
|
251
|
+
});
|
|
252
|
+
const acc = {
|
|
253
|
+
published: [],
|
|
254
|
+
skipped: [],
|
|
255
|
+
trustCommands: []
|
|
256
|
+
};
|
|
257
|
+
for (const pkgJsonPath of pkgDirs) await processPackage(pkgJsonPath, resolved, acc);
|
|
258
|
+
if (existsSync(join(context.root, "scripts/trust-github.sh"))) {}
|
|
259
|
+
if (acc.published.length > 0) console.log(`\nPublished: ${acc.published.join(", ")}`);
|
|
260
|
+
if (acc.skipped.length > 0) console.log(`Skipped: ${acc.skipped.join(", ")}`);
|
|
261
|
+
if (acc.trustCommands.length > 0 || resolved.trust && acc.skipped.length > 0) if (resolved.trust && !resolved.dryRun) {
|
|
262
|
+
const trustTargets = [...acc.published, ...acc.skipped];
|
|
263
|
+
console.log(`\nConfiguring GitHub OIDC trusted publishing for ${trustTargets.length} package(s) (requires MFA)...\n`);
|
|
264
|
+
for (const pkgName of trustTargets) try {
|
|
265
|
+
await runTrustFor(pkgName, resolved.trustRepo, resolved.registry);
|
|
266
|
+
} catch (error) {
|
|
267
|
+
if (error instanceof Error && /already.*trust|conflict|exists/i.test(error.message)) {
|
|
268
|
+
console.log(` ⊙ ${pkgName} (trust already configured)`);
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
console.log("\nRun these locally (requires MFA) to enable GitHub OIDC trusted publishing:\n");
|
|
275
|
+
for (const cmd of acc.trustCommands) console.log(` ${cmd}`);
|
|
276
|
+
for (const pkgName of acc.skipped) console.log(` ${trustCommandFor(pkgName, resolved.trustRepo, resolved.registry)}`);
|
|
277
|
+
}
|
|
278
|
+
return {
|
|
279
|
+
success: true,
|
|
280
|
+
...acc
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
//#endregion
|
|
284
|
+
export { publishPlaceholderExecutor as default, publishPlaceholderExecutor };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { GeneratorCallback, Tree } from "@nx/devkit";
|
|
2
|
+
|
|
3
|
+
//#region src/generators/init/generator.d.ts
|
|
4
|
+
interface NxPrepareForReleaseInitOptions {
|
|
5
|
+
projectName?: string;
|
|
6
|
+
pluginPath?: string;
|
|
7
|
+
}
|
|
8
|
+
declare function initGenerator(tree: Tree, options?: NxPrepareForReleaseInitOptions): Promise<GeneratorCallback>;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { NxPrepareForReleaseInitOptions, initGenerator as default, initGenerator };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { joinPathFragments } from "@nx/devkit";
|
|
2
|
+
//#region src/generators/init/generator.ts
|
|
3
|
+
const DEFAULT_PROJECT_NAME = "tools";
|
|
4
|
+
const DEFAULT_PLUGIN_PATH = "@nx-devkit/prepare-for-release";
|
|
5
|
+
function readJson(tree, path) {
|
|
6
|
+
if (!tree.exists(path)) return null;
|
|
7
|
+
try {
|
|
8
|
+
return JSON.parse(tree.read(path, "utf8") ?? "{}");
|
|
9
|
+
} catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function writeJson(tree, path, value) {
|
|
14
|
+
tree.write(path, `${JSON.stringify(value, null, 2)}\n`);
|
|
15
|
+
}
|
|
16
|
+
function ensureToolsProject(tree, projectName, pluginPath) {
|
|
17
|
+
const projectJsonPath = joinPathFragments(projectName, "project.json");
|
|
18
|
+
if (tree.exists(projectJsonPath)) {
|
|
19
|
+
const existing = readJson(tree, projectJsonPath);
|
|
20
|
+
if (existing) {
|
|
21
|
+
const targets = existing.targets ?? {};
|
|
22
|
+
if (!targets["prepare-for-release"]) {
|
|
23
|
+
targets["prepare-for-release"] = {
|
|
24
|
+
executor: `${pluginPath}:publish-placeholder`,
|
|
25
|
+
options: {}
|
|
26
|
+
};
|
|
27
|
+
existing.targets = targets;
|
|
28
|
+
writeJson(tree, projectJsonPath, existing);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
writeJson(tree, projectJsonPath, {
|
|
34
|
+
$schema: "../../node_modules/nx/schemas/project-schema.json",
|
|
35
|
+
name: projectName,
|
|
36
|
+
sourceRoot: `${projectName}`,
|
|
37
|
+
targets: { "prepare-for-release": {
|
|
38
|
+
executor: `${pluginPath}:publish-placeholder`,
|
|
39
|
+
options: {}
|
|
40
|
+
} }
|
|
41
|
+
});
|
|
42
|
+
tree.write(joinPathFragments(projectName, ".gitkeep"), "");
|
|
43
|
+
}
|
|
44
|
+
function registerPlugin(tree, pluginPath) {
|
|
45
|
+
const nxJson = readJson(tree, "nx.json") ?? {};
|
|
46
|
+
const plugins = Array.isArray(nxJson.plugins) ? nxJson.plugins : [];
|
|
47
|
+
if (plugins.some((entry) => typeof entry === "string" && entry === pluginPath || typeof entry === "object" && entry !== null && entry.plugin === pluginPath)) return;
|
|
48
|
+
plugins.push({
|
|
49
|
+
options: {},
|
|
50
|
+
plugin: pluginPath
|
|
51
|
+
});
|
|
52
|
+
nxJson.plugins = plugins;
|
|
53
|
+
writeJson(tree, "nx.json", nxJson);
|
|
54
|
+
}
|
|
55
|
+
async function initGenerator(tree, options = {}) {
|
|
56
|
+
const projectName = options.projectName ?? DEFAULT_PROJECT_NAME;
|
|
57
|
+
const pluginPath = options.pluginPath ?? DEFAULT_PLUGIN_PATH;
|
|
58
|
+
registerPlugin(tree, pluginPath);
|
|
59
|
+
ensureToolsProject(tree, projectName, pluginPath);
|
|
60
|
+
const checklist = [
|
|
61
|
+
"1. Install the plugin in the consuming workspace:",
|
|
62
|
+
" bun add -D @nx-devkit/prepare-for-release",
|
|
63
|
+
"2. Run the bootstrap target to publish 0.0.0 placeholders:",
|
|
64
|
+
` bunx nx run ${projectName}:prepare-for-release`,
|
|
65
|
+
"3. For each published placeholder, run the corresponding `npm trust github` command printed by the executor (requires MFA).",
|
|
66
|
+
"4. Wire OIDC trusted publishing in .github/workflows/release.yml (see the template shipped with this plugin).",
|
|
67
|
+
"5. From then on, releases are automated via `bunx nx release`."
|
|
68
|
+
];
|
|
69
|
+
for (const line of checklist) console.log(line);
|
|
70
|
+
return () => {};
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
export { initGenerator as default, initGenerator };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import publishPlaceholderExecutor, { NxPrepareForReleaseOptions, PublishPlaceholderResult } from "./executors/publish-placeholder/executor.mjs";
|
|
2
|
+
import initGenerator, { NxPrepareForReleaseInitOptions } from "./generators/init/generator.mjs";
|
|
3
|
+
import { createNodesV2, isReleaseBootstrapProject } from "./plugin.mjs";
|
|
4
|
+
export { type NxPrepareForReleaseInitOptions, type NxPrepareForReleaseOptions, type PublishPlaceholderResult, createNodesV2, initGenerator, isReleaseBootstrapProject, publishPlaceholderExecutor };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import publishPlaceholderExecutor from "./executors/publish-placeholder/executor.mjs";
|
|
2
|
+
import initGenerator from "./generators/init/generator.mjs";
|
|
3
|
+
import { createNodesV2, isReleaseBootstrapProject } from "./plugin.mjs";
|
|
4
|
+
export { createNodesV2, initGenerator, isReleaseBootstrapProject, publishPlaceholderExecutor };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CreateNodesV2 } from "@nx/devkit";
|
|
2
|
+
|
|
3
|
+
//#region src/plugin.d.ts
|
|
4
|
+
interface NxPrepareForReleasePluginOptions {
|
|
5
|
+
/** Path to the tools project that hosts the prepare-for-release target. */
|
|
6
|
+
toolsProject?: string;
|
|
7
|
+
/** Target name inferred on the tools project. */
|
|
8
|
+
targetName?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Detect whether a directory is a release-bootstrap site: it contains a
|
|
12
|
+
* `project.json` referencing the executor shipped by this plugin. Used by
|
|
13
|
+
* `createNodesV2` to auto-wire a `prepare-for-release` target on the
|
|
14
|
+
* `tools` project created by the init generator.
|
|
15
|
+
*/
|
|
16
|
+
declare function isReleaseBootstrapProject(configFile: string, workspaceRoot: string, executor?: string): boolean;
|
|
17
|
+
declare const createNodesV2: CreateNodesV2<NxPrepareForReleasePluginOptions>;
|
|
18
|
+
declare const plugin: {
|
|
19
|
+
createNodesV2: CreateNodesV2<NxPrepareForReleasePluginOptions>;
|
|
20
|
+
name: string;
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
export { NxPrepareForReleasePluginOptions, createNodesV2, plugin as default, isReleaseBootstrapProject };
|
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
3
|
+
//#region src/plugin.ts
|
|
4
|
+
const PLUGIN_NAME = "@nx-devkit/prepare-for-release";
|
|
5
|
+
/**
|
|
6
|
+
* Detect whether a directory is a release-bootstrap site: it contains a
|
|
7
|
+
* `project.json` referencing the executor shipped by this plugin. Used by
|
|
8
|
+
* `createNodesV2` to auto-wire a `prepare-for-release` target on the
|
|
9
|
+
* `tools` project created by the init generator.
|
|
10
|
+
*/
|
|
11
|
+
function isReleaseBootstrapProject(configFile, workspaceRoot, executor = `${PLUGIN_NAME}:publish-placeholder`) {
|
|
12
|
+
const configDir = dirname(configFile);
|
|
13
|
+
const projectJsonPath = join(isAbsolute(configDir) ? configDir : join(workspaceRoot, configDir), "project.json");
|
|
14
|
+
if (!existsSync(projectJsonPath)) return false;
|
|
15
|
+
let contents;
|
|
16
|
+
try {
|
|
17
|
+
contents = readFileSync(projectJsonPath, "utf8");
|
|
18
|
+
} catch (error) {
|
|
19
|
+
if (error.code === "ENOENT") return false;
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const targets = JSON.parse(contents).targets ?? {};
|
|
24
|
+
return Object.values(targets).some((t) => typeof t.executor === "string" && t.executor === executor);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (error instanceof SyntaxError) return false;
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const createNodesV2 = ["**/project.json", (configFiles, options = {}, context) => {
|
|
31
|
+
const targetName = options.targetName ?? "prepare-for-release";
|
|
32
|
+
const toolsProjectRoot = options.toolsProject?.replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, "");
|
|
33
|
+
const results = [];
|
|
34
|
+
for (const configFile of configFiles) {
|
|
35
|
+
const normalized = configFile.replace(/\\/g, "/");
|
|
36
|
+
if (!isReleaseBootstrapProject(normalized, context.workspaceRoot)) continue;
|
|
37
|
+
const projectRoot = dirname(normalized).replace(/\/+$/, "");
|
|
38
|
+
if (projectRoot === "" || projectRoot === "." || projectRoot === context.workspaceRoot) continue;
|
|
39
|
+
if (toolsProjectRoot && projectRoot !== toolsProjectRoot) continue;
|
|
40
|
+
results.push([configFile, { projects: { [projectRoot]: { targets: { [targetName]: {
|
|
41
|
+
executor: `${PLUGIN_NAME}:publish-placeholder`,
|
|
42
|
+
options: {}
|
|
43
|
+
} } } } }]);
|
|
44
|
+
}
|
|
45
|
+
return results;
|
|
46
|
+
}];
|
|
47
|
+
const plugin = {
|
|
48
|
+
createNodesV2,
|
|
49
|
+
name: PLUGIN_NAME
|
|
50
|
+
};
|
|
51
|
+
//#endregion
|
|
52
|
+
export { createNodesV2, plugin as default, isReleaseBootstrapProject };
|
package/executors.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"executors": {
|
|
3
|
+
"publish-placeholder": {
|
|
4
|
+
"implementation": "./dist/executors/publish-placeholder/executor.mjs",
|
|
5
|
+
"schema": "./src/executors/publish-placeholder/schema.json",
|
|
6
|
+
"description": "Publishes a 0.0.0 placeholder tarball to the npm registry for every workspace package that is not yet published. Idempotent."
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,51 @@
|
|
|
1
1
|
{
|
|
2
|
-
"description": "Placeholder for @nx-devkit/prepare-for-release published by @nx-devkit/prepare-for-release.",
|
|
3
|
-
"license": "MIT",
|
|
4
2
|
"name": "@nx-devkit/prepare-for-release",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
},
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"private": false,
|
|
9
6
|
"type": "module",
|
|
10
|
-
"
|
|
11
|
-
|
|
7
|
+
"main": "./dist/index.mjs",
|
|
8
|
+
"module": "./dist/index.mjs",
|
|
9
|
+
"types": "./dist/index.d.mts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.mts",
|
|
13
|
+
"default": "./dist/index.mjs"
|
|
14
|
+
},
|
|
15
|
+
"./plugin": {
|
|
16
|
+
"types": "./dist/plugin.d.mts",
|
|
17
|
+
"default": "./dist/plugin.mjs"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"executors": "./executors.json",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"executors.json",
|
|
25
|
+
"src/executors/publish-placeholder/schema.json",
|
|
26
|
+
"README.md"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsdown",
|
|
30
|
+
"test": "vitest run"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@nx/devkit": "^22.0.0 || ^23.0.0"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"tinyglobby": "^0.2.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@nx/devkit": "23.2.1",
|
|
40
|
+
"@types/node": "^20.19.0",
|
|
41
|
+
"memfs": "^4.50.0",
|
|
42
|
+
"tsdown": "^0.22.3",
|
|
43
|
+
"typescript": "^6.0.3",
|
|
44
|
+
"vitest": "^4.1.9"
|
|
45
|
+
},
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "https://github.com/nx-devkit/nx.ts",
|
|
49
|
+
"directory": "packages/prepare-for-release"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/schema",
|
|
3
|
+
"version": 2,
|
|
4
|
+
"title": "publish-placeholder executor",
|
|
5
|
+
"description": "Publishes a 0.0.0 placeholder tarball to the npm registry for every workspace package that is not yet published. Idempotent.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"scope": {
|
|
9
|
+
"type": "array",
|
|
10
|
+
"items": { "type": "string" },
|
|
11
|
+
"description": "Package scope prefixes to check. Default: every package in packages/*."
|
|
12
|
+
},
|
|
13
|
+
"placeholderTag": {
|
|
14
|
+
"type": "string",
|
|
15
|
+
"description": "npm dist-tag applied to the placeholder publish.",
|
|
16
|
+
"default": "placeholder"
|
|
17
|
+
},
|
|
18
|
+
"placeholderVersion": {
|
|
19
|
+
"type": "string",
|
|
20
|
+
"description": "Version written into the temporary placeholder package.json.",
|
|
21
|
+
"default": "0.0.0"
|
|
22
|
+
},
|
|
23
|
+
"registry": {
|
|
24
|
+
"type": "string",
|
|
25
|
+
"description": "npm registry URL.",
|
|
26
|
+
"default": "https://registry.npmjs.org/"
|
|
27
|
+
},
|
|
28
|
+
"dryRun": {
|
|
29
|
+
"type": "boolean",
|
|
30
|
+
"description": "If true, do not actually publish or pack; just report what would happen.",
|
|
31
|
+
"default": false
|
|
32
|
+
},
|
|
33
|
+
"trust": {
|
|
34
|
+
"type": "boolean",
|
|
35
|
+
"description": "If true, run `npm trust github` for each published package (requires MFA).",
|
|
36
|
+
"default": false
|
|
37
|
+
},
|
|
38
|
+
"trustRepo": {
|
|
39
|
+
"type": "string",
|
|
40
|
+
"pattern": "^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$",
|
|
41
|
+
"description": "`owner/repo` slug used to build the `npm trust github` command. Falls back to NPM_TRUST_REPO or GITHUB_REPOSITORY env vars."
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"additionalProperties": false
|
|
45
|
+
}
|