@smoothbricks/cli 0.1.0 → 0.2.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 (148) hide show
  1. package/README.md +338 -88
  2. package/dist/cli.d.ts.map +1 -1
  3. package/dist/cli.js +136 -22
  4. package/dist/devenv/index.d.ts +10 -0
  5. package/dist/devenv/index.d.ts.map +1 -0
  6. package/dist/devenv/index.js +27 -0
  7. package/dist/lib/cli-package.d.ts +4 -0
  8. package/dist/lib/cli-package.d.ts.map +1 -0
  9. package/dist/lib/cli-package.js +15 -0
  10. package/dist/lib/devenv.d.ts +6 -0
  11. package/dist/lib/devenv.d.ts.map +1 -0
  12. package/dist/lib/devenv.js +82 -0
  13. package/dist/lib/run.d.ts +5 -0
  14. package/dist/lib/run.d.ts.map +1 -1
  15. package/dist/lib/run.js +13 -0
  16. package/dist/lib/workspace.d.ts +20 -1
  17. package/dist/lib/workspace.d.ts.map +1 -1
  18. package/dist/lib/workspace.js +87 -7
  19. package/dist/monorepo/commit-msg.d.ts +8 -1
  20. package/dist/monorepo/commit-msg.d.ts.map +1 -1
  21. package/dist/monorepo/commit-msg.js +85 -7
  22. package/dist/monorepo/index.d.ts +24 -2
  23. package/dist/monorepo/index.d.ts.map +1 -1
  24. package/dist/monorepo/index.js +136 -15
  25. package/dist/monorepo/lockfile.d.ts +5 -1
  26. package/dist/monorepo/lockfile.d.ts.map +1 -1
  27. package/dist/monorepo/lockfile.js +45 -5
  28. package/dist/monorepo/managed-files.d.ts +1 -1
  29. package/dist/monorepo/managed-files.d.ts.map +1 -1
  30. package/dist/monorepo/managed-files.js +28 -11
  31. package/dist/monorepo/nx-sync.d.ts +2 -1
  32. package/dist/monorepo/nx-sync.d.ts.map +1 -1
  33. package/dist/monorepo/nx-sync.js +8 -2
  34. package/dist/monorepo/package-hygiene.d.ts +6 -2
  35. package/dist/monorepo/package-hygiene.d.ts.map +1 -1
  36. package/dist/monorepo/package-hygiene.js +12 -4
  37. package/dist/monorepo/package-policy.d.ts +25 -2
  38. package/dist/monorepo/package-policy.d.ts.map +1 -1
  39. package/dist/monorepo/package-policy.js +1289 -27
  40. package/dist/monorepo/packed-manifest.d.ts +4 -0
  41. package/dist/monorepo/packed-manifest.d.ts.map +1 -0
  42. package/dist/monorepo/packed-manifest.js +51 -0
  43. package/dist/monorepo/packed-package.d.ts +3 -0
  44. package/dist/monorepo/packed-package.d.ts.map +1 -1
  45. package/dist/monorepo/packed-package.js +69 -31
  46. package/dist/monorepo/packs/index.d.ts +20 -1
  47. package/dist/monorepo/packs/index.d.ts.map +1 -1
  48. package/dist/monorepo/packs/index.js +240 -18
  49. package/dist/monorepo/publish-workflow.d.ts +82 -0
  50. package/dist/monorepo/publish-workflow.d.ts.map +1 -0
  51. package/dist/monorepo/publish-workflow.js +336 -0
  52. package/dist/monorepo/tool-validation.d.ts +22 -0
  53. package/dist/monorepo/tool-validation.d.ts.map +1 -0
  54. package/dist/monorepo/tool-validation.js +399 -0
  55. package/dist/nx/index.d.ts +24 -0
  56. package/dist/nx/index.d.ts.map +1 -0
  57. package/dist/nx/index.js +94 -0
  58. package/dist/nx-version-actions.cjs +25 -0
  59. package/dist/nx-version-actions.d.cts +6 -0
  60. package/dist/nx-version-actions.d.cts.map +1 -0
  61. package/dist/release/bootstrap-npm-packages.d.ts +19 -0
  62. package/dist/release/bootstrap-npm-packages.d.ts.map +1 -0
  63. package/dist/release/bootstrap-npm-packages.js +58 -0
  64. package/dist/release/candidates.d.ts +11 -0
  65. package/dist/release/candidates.d.ts.map +1 -0
  66. package/dist/release/candidates.js +181 -0
  67. package/dist/release/core.d.ts +51 -0
  68. package/dist/release/core.d.ts.map +1 -0
  69. package/dist/release/core.js +97 -0
  70. package/dist/release/github-release.d.ts +46 -0
  71. package/dist/release/github-release.d.ts.map +1 -0
  72. package/dist/release/github-release.js +97 -0
  73. package/dist/release/index.d.ts +39 -5
  74. package/dist/release/index.d.ts.map +1 -1
  75. package/dist/release/index.js +763 -130
  76. package/dist/release/npm-auth.d.ts +16 -0
  77. package/dist/release/npm-auth.d.ts.map +1 -0
  78. package/dist/release/npm-auth.js +39 -0
  79. package/dist/release/orchestration.d.ts +49 -0
  80. package/dist/release/orchestration.d.ts.map +1 -0
  81. package/dist/release/orchestration.js +113 -0
  82. package/dist/release/publish-plan.d.ts +17 -0
  83. package/dist/release/publish-plan.d.ts.map +1 -0
  84. package/dist/release/publish-plan.js +15 -0
  85. package/dist/release/retag-unpublished.d.ts +34 -0
  86. package/dist/release/retag-unpublished.d.ts.map +1 -0
  87. package/dist/release/retag-unpublished.js +77 -0
  88. package/managed/raw/git-format-staged.yml +54 -0
  89. package/managed/raw/tooling/git-hooks/commit-msg.sh +1 -1
  90. package/managed/raw/tooling/git-hooks/pre-commit.sh +2 -7
  91. package/managed/templates/github/actions/setup-devenv/action.yml +1 -7
  92. package/managed/templates/github/workflows/ci.yml +3 -0
  93. package/package.json +37 -4
  94. package/src/cli.ts +193 -40
  95. package/src/devenv/index.test.ts +49 -0
  96. package/src/devenv/index.ts +38 -0
  97. package/src/lib/cli-package.ts +18 -0
  98. package/src/lib/devenv.test.ts +28 -0
  99. package/src/lib/devenv.ts +89 -0
  100. package/src/lib/run.ts +19 -0
  101. package/src/lib/workspace.ts +117 -7
  102. package/src/monorepo/__tests__/nx-version-actions.test.ts +75 -0
  103. package/src/monorepo/__tests__/publish-workflow.test.ts +322 -0
  104. package/src/monorepo/commit-msg.test.ts +45 -0
  105. package/src/monorepo/commit-msg.ts +107 -7
  106. package/src/monorepo/index.ts +181 -15
  107. package/src/monorepo/lockfile.ts +52 -7
  108. package/src/monorepo/managed-files.ts +43 -13
  109. package/src/monorepo/nx-sync.ts +9 -2
  110. package/src/monorepo/package-hygiene.test.ts +34 -0
  111. package/src/monorepo/package-hygiene.ts +26 -4
  112. package/src/monorepo/package-policy.test.ts +902 -0
  113. package/src/monorepo/package-policy.ts +1527 -25
  114. package/src/monorepo/packed-manifest.ts +67 -0
  115. package/src/monorepo/packed-package.ts +90 -31
  116. package/src/monorepo/packs/index.test.ts +189 -0
  117. package/src/monorepo/packs/index.ts +314 -19
  118. package/src/monorepo/publish-workflow.ts +422 -0
  119. package/src/monorepo/setup-test-tracing.test.ts +120 -0
  120. package/src/monorepo/tool-validation.test.ts +261 -0
  121. package/src/monorepo/tool-validation.ts +462 -0
  122. package/src/nx/index.test.ts +64 -0
  123. package/src/nx/index.ts +119 -0
  124. package/src/nx-version-actions.cts +36 -0
  125. package/src/release/__tests__/bootstrap-npm-packages.test.ts +127 -0
  126. package/src/release/__tests__/candidates.test.ts +217 -0
  127. package/src/release/__tests__/core-properties.test.ts +149 -0
  128. package/src/release/__tests__/core-scenarios.test.ts +187 -0
  129. package/src/release/__tests__/core.test.ts +73 -0
  130. package/src/release/__tests__/fixture-repo.test.ts +314 -0
  131. package/src/release/__tests__/github-release.test.ts +149 -0
  132. package/src/release/__tests__/helpers/fixture-repo.ts +114 -0
  133. package/src/release/__tests__/npm-auth.test.ts +120 -0
  134. package/src/release/__tests__/orchestration.test.ts +246 -0
  135. package/src/release/__tests__/publish-plan.test.ts +67 -0
  136. package/src/release/__tests__/retag-unpublished.test.ts +160 -0
  137. package/src/release/__tests__/trust-publisher.test.ts +133 -0
  138. package/src/release/bootstrap-npm-packages.ts +88 -0
  139. package/src/release/candidates.ts +225 -0
  140. package/src/release/core.ts +171 -0
  141. package/src/release/github-release.ts +134 -0
  142. package/src/release/index.ts +974 -138
  143. package/src/release/npm-auth.ts +93 -0
  144. package/src/release/orchestration.ts +196 -0
  145. package/src/release/publish-plan.ts +37 -0
  146. package/src/release/retag-unpublished.ts +122 -0
  147. package/managed/raw/.git-format-staged.yml +0 -47
  148. package/managed/templates/github/workflows/publish.yml +0 -136
@@ -0,0 +1,133 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import type { BootstrapNpmPackagesOptions } from '../bootstrap-npm-packages.js';
3
+ import type { ReleasePackageInfo } from '../core.js';
4
+ import { configureTrustedPublishers, type TrustPublisherShell } from '../index.js';
5
+
6
+ const stable: ReleasePackageInfo = {
7
+ name: '@scope/stable',
8
+ projectName: 'stable',
9
+ path: 'packages/stable',
10
+ version: '1.2.3',
11
+ };
12
+ const missing: ReleasePackageInfo = {
13
+ name: '@scope/missing',
14
+ projectName: 'missing',
15
+ path: 'packages/missing',
16
+ version: '2.0.0',
17
+ };
18
+
19
+ describe('trusted publisher setup', () => {
20
+ it('bootstraps missing npm packages before configuring trust', async () => {
21
+ const shell = new RecordingTrustPublisherShell({ packages: [stable, missing], existing: [stable.name] });
22
+
23
+ await configureTrustedPublishers(shell, { bootstrap: true, skipLogin: false, otp: '123456' });
24
+
25
+ expect(shell.events).toEqual([
26
+ 'bootstrap:false:false',
27
+ `exists:${stable.name}`,
28
+ `exists:${missing.name}`,
29
+ `trust:${stable.name}:false:123456`,
30
+ `trust:${missing.name}:false:123456`,
31
+ ]);
32
+ expect(shell.logins).toBe(0);
33
+ });
34
+
35
+ it('directs missing packages to trust-publisher --bootstrap', async () => {
36
+ const shell = new RecordingTrustPublisherShell({ packages: [missing], existing: [] });
37
+
38
+ await expect(configureTrustedPublishers(shell, { otp: '123456' })).rejects.toThrow(
39
+ 'Run smoo release trust-publisher --bootstrap locally',
40
+ );
41
+ expect(shell.events).toEqual([`exists:${missing.name}`]);
42
+ });
43
+
44
+ it('still logs in for trust when bootstrap finds no missing packages', async () => {
45
+ const shell = new RecordingTrustPublisherShell({ packages: [stable], existing: [stable.name] });
46
+
47
+ await configureTrustedPublishers(shell, { bootstrap: true, otp: '123456' });
48
+
49
+ expect(shell.events).toEqual([
50
+ 'bootstrap:false:false',
51
+ `exists:${stable.name}`,
52
+ `trust:${stable.name}:false:123456`,
53
+ ]);
54
+ expect(shell.logins).toBe(1);
55
+ });
56
+
57
+ it('treats npm trust conflicts as already configured', async () => {
58
+ const shell = new RecordingTrustPublisherShell({
59
+ packages: [stable],
60
+ existing: [stable.name],
61
+ alreadyTrusted: [stable.name],
62
+ });
63
+
64
+ await configureTrustedPublishers(shell, { skipLogin: true, otp: '123456' });
65
+
66
+ expect(shell.events).toEqual([`exists:${stable.name}`, `trust:${stable.name}:false:123456`]);
67
+ expect(shell.logs).toContain(`${stable.name}: npm trusted publisher is already configured; skipping.`);
68
+ });
69
+ });
70
+
71
+ class RecordingTrustPublisherShell implements TrustPublisherShell<ReleasePackageInfo> {
72
+ readonly repository = 'scope/repo';
73
+ readonly workflow = 'publish.yml';
74
+ readonly events: string[] = [];
75
+ readonly logs: string[] = [];
76
+ readonly errors: string[] = [];
77
+ logins = 0;
78
+ private readonly packages: ReleasePackageInfo[];
79
+ private readonly existing: Set<string>;
80
+ private readonly alreadyTrusted: Set<string>;
81
+
82
+ constructor(options: { packages: ReleasePackageInfo[]; existing: string[]; alreadyTrusted?: string[] }) {
83
+ this.packages = options.packages;
84
+ this.existing = new Set(options.existing);
85
+ this.alreadyTrusted = new Set(options.alreadyTrusted ?? []);
86
+ }
87
+
88
+ listReleasePackages(): ReleasePackageInfo[] {
89
+ return this.packages;
90
+ }
91
+
92
+ async packageExists(name: string): Promise<boolean> {
93
+ this.events.push(`exists:${name}`);
94
+ return this.existing.has(name);
95
+ }
96
+
97
+ async bootstrapNpmPackages(options: BootstrapNpmPackagesOptions): Promise<ReleasePackageInfo[]> {
98
+ this.events.push(`bootstrap:${options.dryRun}:${options.skipLogin}`);
99
+ const missingPackages = this.packages.filter((pkg) => !this.existing.has(pkg.name));
100
+ for (const pkg of missingPackages) {
101
+ this.existing.add(pkg.name);
102
+ }
103
+ return missingPackages;
104
+ }
105
+
106
+ async login(): Promise<void> {
107
+ this.logins += 1;
108
+ }
109
+
110
+ async trustPublisher(
111
+ pkg: ReleasePackageInfo,
112
+ dryRun: boolean,
113
+ env?: Record<string, string>,
114
+ ): Promise<'configured' | 'already-configured'> {
115
+ this.events.push(`trust:${pkg.name}:${dryRun}:${env?.NPM_CONFIG_OTP ?? ''}`);
116
+ if (this.alreadyTrusted.has(pkg.name)) {
117
+ return 'already-configured';
118
+ }
119
+ return 'configured';
120
+ }
121
+
122
+ async promptOtp(packageName: string): Promise<string> {
123
+ throw new Error(`unexpected OTP prompt for ${packageName}`);
124
+ }
125
+
126
+ log(message: string): void {
127
+ this.logs.push(message);
128
+ }
129
+
130
+ error(message: string): void {
131
+ this.errors.push(message);
132
+ }
133
+ }
@@ -0,0 +1,88 @@
1
+ import type { ReleasePackageInfo } from './core.js';
2
+
3
+ export const NPM_BOOTSTRAP_VERSION = '0.0.0-bootstrap.0';
4
+ export const NPM_BOOTSTRAP_DIST_TAG = 'bootstrap';
5
+
6
+ export interface BootstrapNpmPackagesOptions {
7
+ dryRun: boolean;
8
+ skipLogin: boolean;
9
+ packages: string[];
10
+ otp?: string;
11
+ }
12
+
13
+ export interface BootstrapNpmPackagesShell<Package extends ReleasePackageInfo = ReleasePackageInfo> {
14
+ listReleasePackages(): Package[];
15
+ packageExists(name: string): Promise<boolean>;
16
+ login(): Promise<void>;
17
+ publishPlaceholder(pkg: Package, env?: Record<string, string>): Promise<void>;
18
+ promptOtp(packageName: string): Promise<string>;
19
+ log(message: string): void;
20
+ }
21
+
22
+ export async function bootstrapNpmPackages<Package extends ReleasePackageInfo>(
23
+ shell: BootstrapNpmPackagesShell<Package>,
24
+ options: BootstrapNpmPackagesOptions,
25
+ ): Promise<Package[]> {
26
+ const packages = selectedReleasePackages(shell.listReleasePackages(), options.packages);
27
+ if (packages.length === 0) {
28
+ throw new Error('No owned release packages found.');
29
+ }
30
+
31
+ const missing: Package[] = [];
32
+ for (const pkg of packages) {
33
+ if (await shell.packageExists(pkg.name)) {
34
+ shell.log(`${pkg.name}: already exists on npm; skipping placeholder bootstrap.`);
35
+ } else {
36
+ missing.push(pkg);
37
+ }
38
+ }
39
+
40
+ if (missing.length === 0) {
41
+ shell.log('All selected owned release packages already exist on npm.');
42
+ return [];
43
+ }
44
+
45
+ shell.log(
46
+ `Bootstrap npm placeholders (${NPM_BOOTSTRAP_VERSION}, dist-tag ${NPM_BOOTSTRAP_DIST_TAG}): ${missing
47
+ .map((pkg) => pkg.name)
48
+ .join(', ')}`,
49
+ );
50
+ if (options.dryRun) {
51
+ return missing;
52
+ }
53
+
54
+ if (!options.skipLogin) {
55
+ await shell.login();
56
+ }
57
+ for (const pkg of missing) {
58
+ shell.log(`${pkg.name}: publishing npm placeholder.`);
59
+ const otp = options.otp ?? (await shell.promptOtp(pkg.name));
60
+ await shell.publishPlaceholder(pkg, { NPM_CONFIG_OTP: otp });
61
+ }
62
+ shell.log('Bootstrap complete. Run smoo release trust-publisher before the first CI publish.');
63
+ return missing;
64
+ }
65
+
66
+ function selectedReleasePackages<Package extends ReleasePackageInfo>(
67
+ packages: Package[],
68
+ selections: string[],
69
+ ): Package[] {
70
+ if (selections.length === 0) {
71
+ return packages;
72
+ }
73
+ const byName = new Map(packages.map((pkg) => [pkg.name, pkg]));
74
+ const selected: Package[] = [];
75
+ const unknown: string[] = [];
76
+ for (const name of selections) {
77
+ const pkg = byName.get(name);
78
+ if (pkg) {
79
+ selected.push(pkg);
80
+ } else {
81
+ unknown.push(name);
82
+ }
83
+ }
84
+ if (unknown.length > 0) {
85
+ throw new Error(`Unknown owned release package selection: ${unknown.join(', ')}`);
86
+ }
87
+ return selected;
88
+ }
@@ -0,0 +1,225 @@
1
+ import { isRecord } from '../lib/json.js';
2
+ import type { ReleasePackageInfo } from './core.js';
3
+ import { releaseTag } from './core.js';
4
+
5
+ export interface AutoReleaseCandidateShell {
6
+ gitRefExists(ref: string): Promise<boolean>;
7
+ packageChangedFilesSince(ref: string, packagePath: string): Promise<string[]>;
8
+ packageJsonAtRef(ref: string, packagePath: string): Promise<Record<string, unknown> | null>;
9
+ currentPackageJson(packagePath: string): Promise<Record<string, unknown> | null>;
10
+ packageBuildInputPatterns(projectName: string, packagePath: string): Promise<string[]>;
11
+ packageHasHistory(packagePath: string): Promise<boolean>;
12
+ }
13
+
14
+ const releasableManifestKeys = [
15
+ 'author',
16
+ 'bin',
17
+ 'browser',
18
+ 'bugs',
19
+ 'cpu',
20
+ 'dependencies',
21
+ 'description',
22
+ 'engines',
23
+ 'exports',
24
+ 'files',
25
+ 'funding',
26
+ 'homepage',
27
+ 'imports',
28
+ 'keywords',
29
+ 'license',
30
+ 'main',
31
+ 'module',
32
+ 'name',
33
+ 'optionalDependencies',
34
+ 'os',
35
+ 'peerDependencies',
36
+ 'peerDependenciesMeta',
37
+ 'publishConfig',
38
+ 'repository',
39
+ 'sideEffects',
40
+ 'types',
41
+ 'typesVersions',
42
+ ] as const;
43
+
44
+ export async function autoReleaseCandidatePackages<Package extends ReleasePackageInfo>(
45
+ shell: AutoReleaseCandidateShell,
46
+ packages: Package[],
47
+ ): Promise<Package[]> {
48
+ const candidates: Package[] = [];
49
+ for (const pkg of packages) {
50
+ if (await isAutoReleaseCandidate(shell, pkg)) {
51
+ candidates.push(pkg);
52
+ }
53
+ }
54
+ return candidates;
55
+ }
56
+
57
+ async function isAutoReleaseCandidate<Package extends ReleasePackageInfo>(
58
+ shell: AutoReleaseCandidateShell,
59
+ pkg: Package,
60
+ ): Promise<boolean> {
61
+ const tagRef = `refs/tags/${releaseTag(pkg)}`;
62
+ if (await shell.gitRefExists(tagRef)) {
63
+ return packageHasReleasableChangesSince(shell, tagRef, pkg);
64
+ }
65
+ return shell.packageHasHistory(pkg.path);
66
+ }
67
+
68
+ async function packageHasReleasableChangesSince<Package extends ReleasePackageInfo>(
69
+ shell: AutoReleaseCandidateShell,
70
+ ref: string,
71
+ pkg: Package,
72
+ ): Promise<boolean> {
73
+ const changedFiles = await shell.packageChangedFilesSince(ref, pkg.path);
74
+ if (changedFiles.length === 0) {
75
+ return false;
76
+ }
77
+ const currentManifest = await shell.currentPackageJson(pkg.path);
78
+ const buildInputPatterns = await shell.packageBuildInputPatterns(pkg.projectName, pkg.path);
79
+ for (const changedFile of changedFiles) {
80
+ if (changedFile === 'package.json') {
81
+ const previousManifest = await shell.packageJsonAtRef(ref, pkg.path);
82
+ if (!previousManifest || !currentManifest || releasableManifestChanged(previousManifest, currentManifest)) {
83
+ return true;
84
+ }
85
+ continue;
86
+ }
87
+ if (isReleasablePackagePath(changedFile, currentManifest, buildInputPatterns)) {
88
+ return true;
89
+ }
90
+ }
91
+ return false;
92
+ }
93
+
94
+ function releasableManifestChanged(
95
+ previousManifest: Record<string, unknown>,
96
+ currentManifest: Record<string, unknown>,
97
+ ): boolean {
98
+ for (const key of releasableManifestKeys) {
99
+ if (!stableJsonEqual(previousManifest[key], currentManifest[key])) {
100
+ return true;
101
+ }
102
+ }
103
+ return false;
104
+ }
105
+
106
+ function isReleasablePackagePath(
107
+ path: string,
108
+ manifest: Record<string, unknown> | null,
109
+ buildInputPatterns: string[],
110
+ ): boolean {
111
+ return (
112
+ isBuildInputPath(path, buildInputPatterns) || isPackageMetadataPath(path) || isManifestFilesPath(path, manifest)
113
+ );
114
+ }
115
+
116
+ function isBuildInputPath(path: string, patterns: string[]): boolean {
117
+ if (isReleaseIgnoredBuildInputPath(path)) {
118
+ return false;
119
+ }
120
+ let matched = false;
121
+ for (const pattern of patterns) {
122
+ const excluded = pattern.startsWith('!');
123
+ const rawPattern = excluded ? pattern.slice(1) : pattern;
124
+ if (matchesBuildInputPattern(path, rawPattern)) {
125
+ matched = !excluded;
126
+ }
127
+ }
128
+ return matched;
129
+ }
130
+
131
+ function matchesBuildInputPattern(path: string, pattern: string): boolean {
132
+ const normalized = pattern.replace(/^\.\//, '').replace(/\/$/, '');
133
+ if (!normalized) {
134
+ return false;
135
+ }
136
+ return globPatternToRegExp(normalized).test(path);
137
+ }
138
+
139
+ function globPatternToRegExp(pattern: string): RegExp {
140
+ let source = '^';
141
+ for (let index = 0; index < pattern.length; index += 1) {
142
+ const char = pattern[index];
143
+ if (char === '*') {
144
+ if (pattern[index + 1] === '*') {
145
+ if (pattern[index + 2] === '/') {
146
+ source += '(?:.*/)?';
147
+ index += 2;
148
+ } else {
149
+ source += '.*';
150
+ index += 1;
151
+ }
152
+ } else {
153
+ source += '[^/]*';
154
+ }
155
+ } else {
156
+ source += escapeRegExpChar(char);
157
+ }
158
+ }
159
+ return new RegExp(`${source}$`);
160
+ }
161
+
162
+ function escapeRegExpChar(char: string | undefined): string {
163
+ if (!char) {
164
+ return '';
165
+ }
166
+ return /[\\^$+?.()|[\]{}]/.test(char) ? `\\${char}` : char;
167
+ }
168
+
169
+ function isReleaseIgnoredBuildInputPath(path: string): boolean {
170
+ return (
171
+ path === 'package.json' ||
172
+ path === 'tsconfig.test.json' ||
173
+ path.includes('/__tests__/') ||
174
+ path.endsWith('.test.ts') ||
175
+ path.endsWith('.test.tsx') ||
176
+ path.endsWith('.spec.ts') ||
177
+ path.endsWith('.spec.tsx')
178
+ );
179
+ }
180
+
181
+ function isPackageMetadataPath(path: string): boolean {
182
+ return /^(README|LICENSE|CHANGELOG)(\.|$)/.test(path);
183
+ }
184
+
185
+ function isManifestFilesPath(path: string, manifest: Record<string, unknown> | null): boolean {
186
+ const files = manifest?.files;
187
+ if (!Array.isArray(files)) {
188
+ return false;
189
+ }
190
+ for (const entry of files) {
191
+ if (typeof entry !== 'string' || entry.length === 0 || entry.startsWith('!')) {
192
+ continue;
193
+ }
194
+ if (matchesManifestFilesEntry(path, entry)) {
195
+ return true;
196
+ }
197
+ }
198
+ return false;
199
+ }
200
+
201
+ function matchesManifestFilesEntry(path: string, entry: string): boolean {
202
+ const normalized = entry.replace(/^\.\//, '').replace(/\/$/, '');
203
+ if (!normalized || normalized.includes('*')) {
204
+ return false;
205
+ }
206
+ return path === normalized || path.startsWith(`${normalized}/`);
207
+ }
208
+
209
+ function stableJsonEqual(left: unknown, right: unknown): boolean {
210
+ return JSON.stringify(stableJson(left)) === JSON.stringify(stableJson(right));
211
+ }
212
+
213
+ function stableJson(value: unknown): unknown {
214
+ if (Array.isArray(value)) {
215
+ return value.map(stableJson);
216
+ }
217
+ if (isRecord(value)) {
218
+ const normalized: Record<string, unknown> = {};
219
+ for (const key of Object.keys(value).sort()) {
220
+ normalized[key] = stableJson(value[key]);
221
+ }
222
+ return normalized;
223
+ }
224
+ return value;
225
+ }
@@ -0,0 +1,171 @@
1
+ export interface ReleasePackageInfo {
2
+ name: string;
3
+ projectName: string;
4
+ path: string;
5
+ version: string;
6
+ }
7
+
8
+ export interface GitReleaseTagInfo {
9
+ name: string;
10
+ sha: string;
11
+ timestamp: number;
12
+ }
13
+
14
+ export interface MatchedReleaseTag<Package extends ReleasePackageInfo = ReleasePackageInfo> {
15
+ tag: string;
16
+ sha: string;
17
+ timestamp: number;
18
+ pkg: Package;
19
+ }
20
+
21
+ export interface ReleaseTagRecord<Package extends ReleasePackageInfo = ReleasePackageInfo>
22
+ extends MatchedReleaseTag<Package> {
23
+ needsNpmPublish: boolean;
24
+ needsGithubRelease: boolean;
25
+ }
26
+
27
+ export interface ReleaseTarget<Package extends ReleasePackageInfo = ReleasePackageInfo> {
28
+ sha: string;
29
+ timestamp: number;
30
+ packages: Package[];
31
+ npmPackages: Package[];
32
+ githubPackages: Package[];
33
+ }
34
+
35
+ export interface DurableTagState {
36
+ npmPublished: boolean;
37
+ githubReleaseExists: boolean;
38
+ }
39
+
40
+ export interface ReleasePlanningShell {
41
+ listReleaseTagsByCreatorDate(): Promise<GitReleaseTagInfo[]>;
42
+ isAncestor(ancestor: string, descendant: string): Promise<boolean>;
43
+ packageVersionAtRef(packagePath: string, ref: string): Promise<string | null>;
44
+ durableTagState(pkg: ReleasePackageInfo, tag: string): Promise<DurableTagState>;
45
+ }
46
+
47
+ export function releaseTag(pkg: Pick<ReleasePackageInfo, 'name' | 'version'>): string {
48
+ return `${pkg.name}@${pkg.version}`;
49
+ }
50
+
51
+ export function npmDistTagForVersion(version: string): string {
52
+ return version.includes('-') ? 'next' : 'latest';
53
+ }
54
+
55
+ export function releasePackageForTag<Package extends Omit<ReleasePackageInfo, 'version'>>(
56
+ packages: Package[],
57
+ tag: string,
58
+ ): { pkg: Package; version: string } | null {
59
+ for (const pkg of packages) {
60
+ const prefix = `${pkg.name}@`;
61
+ if (tag.startsWith(prefix)) {
62
+ const version = tag.slice(prefix.length);
63
+ if (!version) {
64
+ throw new Error(`Release tag ${tag} does not include a version.`);
65
+ }
66
+ return { pkg, version };
67
+ }
68
+ }
69
+ return null;
70
+ }
71
+
72
+ export function classifyReleaseTag<Package extends ReleasePackageInfo>(
73
+ tag: MatchedReleaseTag<Package>,
74
+ state: DurableTagState,
75
+ ): ReleaseTagRecord<Package> {
76
+ return {
77
+ ...tag,
78
+ needsNpmPublish: !state.npmPublished,
79
+ needsGithubRelease: !state.githubReleaseExists,
80
+ };
81
+ }
82
+
83
+ export async function collectOwnedReleaseTagRecords<Package extends Omit<ReleasePackageInfo, 'version'>>(
84
+ packages: Package[],
85
+ ref: string,
86
+ shell: ReleasePlanningShell,
87
+ ): Promise<Array<ReleaseTagRecord<Package & { version: string }>>> {
88
+ const records: Array<ReleaseTagRecord<Package & { version: string }>> = [];
89
+ const completedPackages = new Set<string>();
90
+ for (const tag of await shell.listReleaseTagsByCreatorDate()) {
91
+ const match = releasePackageForTag(packages, tag.name);
92
+ if (match && completedPackages.has(match.pkg.name)) {
93
+ continue;
94
+ }
95
+ if (!match || !(await shell.isAncestor(tag.sha, ref))) {
96
+ continue;
97
+ }
98
+ const versionAtTag = await shell.packageVersionAtRef(match.pkg.path, tag.sha);
99
+ if (versionAtTag !== match.version) {
100
+ throw new Error(
101
+ `Release tag ${tag.name} points at ${tag.sha.slice(0, 12)}, but ${match.pkg.path}/package.json has version ${
102
+ versionAtTag ?? 'missing'
103
+ }.`,
104
+ );
105
+ }
106
+ const pkg = { ...match.pkg, version: match.version };
107
+ const state = await shell.durableTagState(pkg, tag.name);
108
+ records.push(classifyReleaseTag({ tag: tag.name, sha: tag.sha, timestamp: tag.timestamp, pkg }, state));
109
+ if (state.npmPublished && state.githubReleaseExists) {
110
+ completedPackages.add(pkg.name);
111
+ }
112
+ }
113
+ return records;
114
+ }
115
+
116
+ export function groupReleaseTargets<Package extends ReleasePackageInfo>(
117
+ records: ReleaseTagRecord<Package>[],
118
+ ): ReleaseTarget<Package>[] {
119
+ const targets = new Map<string, ReleaseTarget<Package>>();
120
+ const packageNamesByTarget = new Map<string, Set<string>>();
121
+ for (const record of records) {
122
+ let target = targets.get(record.sha);
123
+ let packageNames = packageNamesByTarget.get(record.sha);
124
+ if (!target) {
125
+ target = { sha: record.sha, timestamp: record.timestamp, packages: [], npmPackages: [], githubPackages: [] };
126
+ targets.set(record.sha, target);
127
+ packageNames = new Set<string>();
128
+ packageNamesByTarget.set(record.sha, packageNames);
129
+ } else if (record.timestamp > target.timestamp) {
130
+ target.timestamp = record.timestamp;
131
+ }
132
+ if (!packageNames) {
133
+ throw new Error(`Release target ${record.sha.slice(0, 12)} lost package tracking state.`);
134
+ }
135
+ if (packageNames.has(record.pkg.name)) {
136
+ throw new Error(
137
+ `Release target ${record.sha.slice(0, 12)} has more than one release tag for ${record.pkg.name}.`,
138
+ );
139
+ }
140
+ packageNames.add(record.pkg.name);
141
+ if (record.needsNpmPublish || record.needsGithubRelease) {
142
+ target.packages.push(record.pkg);
143
+ }
144
+ if (record.needsNpmPublish) {
145
+ target.npmPackages.push(record.pkg);
146
+ }
147
+ if (record.needsGithubRelease) {
148
+ target.githubPackages.push(record.pkg);
149
+ }
150
+ }
151
+ return [...targets.values()].sort(
152
+ (left, right) => right.timestamp - left.timestamp || right.sha.localeCompare(left.sha),
153
+ );
154
+ }
155
+
156
+ export function pendingReleaseTargets<Package extends ReleasePackageInfo>(
157
+ records: ReleaseTagRecord<Package>[],
158
+ headSha: string,
159
+ ): ReleaseTarget<Package>[] {
160
+ const pending: ReleaseTarget<Package>[] = [];
161
+ for (const target of groupReleaseTargets(records)) {
162
+ if (target.sha === headSha) {
163
+ continue;
164
+ }
165
+ if (target.npmPackages.length === 0 && target.githubPackages.length === 0) {
166
+ break;
167
+ }
168
+ pending.push(target);
169
+ }
170
+ return pending.reverse();
171
+ }