|
1 | | -import { existsSync, readdirSync, readFileSync } from 'node:fs' |
2 | | -import { join } from 'node:path' |
3 | | -import { spawnSync } from 'node:child_process' |
| 1 | +import { existsSync, readdirSync, readFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { spawnSync } from "node:child_process"; |
4 | 4 |
|
5 | 5 | if (!process.env.NPM_TOKEN) { |
6 | | - console.error('NPM_TOKEN is required for first package releases.') |
7 | | - process.exit(1) |
| 6 | + console.error("NPM_TOKEN is required for first package releases."); |
| 7 | + process.exit(1); |
8 | 8 | } |
9 | 9 |
|
10 | | -const root = JSON.parse(readFileSync('package.json', 'utf8')) |
| 10 | +const root = JSON.parse(readFileSync("package.json", "utf8")); |
11 | 11 | const workspaces = Array.isArray(root.workspaces) |
12 | 12 | ? root.workspaces |
13 | | - : (root.workspaces?.packages ?? []) |
| 13 | + : (root.workspaces?.packages ?? []); |
14 | 14 |
|
15 | | -const packages = [] |
| 15 | +const packages = []; |
16 | 16 |
|
17 | 17 | for (const workspace of workspaces) { |
18 | | - if (workspace !== '*/') { |
19 | | - continue |
| 18 | + if (workspace !== "*/") { |
| 19 | + continue; |
20 | 20 | } |
21 | 21 |
|
22 | | - for (const entry of readdirSync('.', { withFileTypes: true })) { |
23 | | - if (!entry.isDirectory() || entry.name.startsWith('.')) { |
24 | | - continue |
| 22 | + for (const entry of readdirSync(".", { withFileTypes: true })) { |
| 23 | + if (!entry.isDirectory() || entry.name.startsWith(".")) { |
| 24 | + continue; |
25 | 25 | } |
26 | 26 |
|
27 | 27 | try { |
28 | 28 | const manifest = JSON.parse( |
29 | | - readFileSync(join(entry.name, 'package.json'), 'utf8'), |
30 | | - ) |
| 29 | + readFileSync(join(entry.name, "package.json"), "utf8"), |
| 30 | + ); |
31 | 31 | if (!manifest.private && manifest.name && manifest.version) { |
32 | 32 | packages.push({ |
33 | 33 | dir: entry.name, |
34 | 34 | name: manifest.name, |
35 | 35 | version: manifest.version, |
36 | | - }) |
| 36 | + }); |
37 | 37 | } |
38 | 38 | } catch { |
39 | 39 | // Not a workspace package. |
40 | 40 | } |
41 | 41 | } |
42 | 42 | } |
43 | 43 |
|
44 | | -const missingPackages = [] |
| 44 | +const missingPackages = []; |
45 | 45 |
|
46 | 46 | for (const pkg of packages) { |
47 | | - const result = spawnSync('npm', ['view', pkg.name, 'version'], { |
48 | | - encoding: 'utf8', |
49 | | - }) |
| 47 | + const result = spawnSync("npm", ["view", pkg.name, "version"], { |
| 48 | + encoding: "utf8", |
| 49 | + }); |
50 | 50 |
|
51 | 51 | if (result.status === 0) { |
52 | | - continue |
| 52 | + continue; |
53 | 53 | } |
54 | 54 |
|
55 | | - const error = `${result.stderr}\n${result.stdout}` |
| 55 | + const error = `${result.stderr}\n${result.stdout}`; |
56 | 56 | if (/E404|404 Not Found/.test(error)) { |
57 | | - console.log(`${pkg.name} is not published yet.`) |
58 | | - missingPackages.push(pkg) |
59 | | - continue |
| 57 | + console.log(`${pkg.name} is not published yet.`); |
| 58 | + missingPackages.push(pkg); |
| 59 | + continue; |
60 | 60 | } |
61 | 61 |
|
62 | | - process.stderr.write(error) |
63 | | - process.exit(result.status ?? 1) |
| 62 | + process.stderr.write(error); |
| 63 | + process.exit(result.status ?? 1); |
64 | 64 | } |
65 | 65 |
|
66 | 66 | if (missingPackages.length === 0) { |
67 | | - console.error('No unpublished public workspace packages found.') |
68 | | - process.exit(1) |
| 67 | + console.error("No unpublished public workspace packages found."); |
| 68 | + process.exit(1); |
69 | 69 | } |
70 | 70 |
|
71 | 71 | for (const pkg of missingPackages) { |
72 | | - const changelogPath = join(pkg.dir, 'CHANGELOG.md') |
| 72 | + const changelogPath = join(pkg.dir, "CHANGELOG.md"); |
73 | 73 | if (!existsSync(changelogPath)) { |
74 | 74 | console.error( |
75 | 75 | `${pkg.name} is missing CHANGELOG.md. Create the first version changelog before publishing so changesets/action can create the GitHub Release.`, |
76 | | - ) |
77 | | - process.exit(1) |
| 76 | + ); |
| 77 | + process.exit(1); |
78 | 78 | } |
79 | 79 |
|
80 | | - const changelog = readFileSync(changelogPath, 'utf8') |
81 | | - const versionHeading = new RegExp(`^#{1,6}\\s+${pkg.version}\\s*$`, 'm') |
| 80 | + const changelog = readFileSync(changelogPath, "utf8"); |
| 81 | + const versionHeading = new RegExp(`^#{1,6}\\s+${pkg.version}\\s*$`, "m"); |
82 | 82 | if (!versionHeading.test(changelog)) { |
83 | 83 | console.error( |
84 | 84 | `${pkg.name} CHANGELOG.md is missing a ${pkg.version} entry. Create the first version changelog before publishing so changesets/action can create the GitHub Release.`, |
85 | | - ) |
86 | | - process.exit(1) |
| 85 | + ); |
| 86 | + process.exit(1); |
87 | 87 | } |
88 | 88 | } |
89 | 89 |
|
90 | 90 | for (const pkg of missingPackages) { |
91 | | - const result = spawnSync('npm', ['publish', pkg.dir, '--provenance'], { |
92 | | - encoding: 'utf8', |
93 | | - stdio: ['inherit', 'pipe', 'pipe'], |
94 | | - }) |
| 91 | + const result = spawnSync("npm", ["publish", pkg.dir, "--provenance"], { |
| 92 | + encoding: "utf8", |
| 93 | + stdio: ["inherit", "pipe", "pipe"], |
| 94 | + }); |
95 | 95 |
|
96 | | - process.stdout.write(result.stdout) |
97 | | - process.stderr.write(result.stderr) |
| 96 | + process.stdout.write(result.stdout); |
| 97 | + process.stderr.write(result.stderr); |
98 | 98 |
|
99 | 99 | if (result.status !== 0) { |
100 | | - process.exit(result.status ?? 1) |
| 100 | + process.exit(result.status ?? 1); |
101 | 101 | } |
102 | 102 |
|
103 | | - console.log(`New tag: ${pkg.name}@${pkg.version}`) |
| 103 | + console.log(`New tag: ${pkg.name}@${pkg.version}`); |
104 | 104 | } |
0 commit comments