-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathpublish.mjs
More file actions
124 lines (105 loc) · 3.22 KB
/
publish.mjs
File metadata and controls
124 lines (105 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Publish script for CI: rewrites @agentscript/* → @sf-agentscript/* in all
* package.json files and built dist files at publish time, then runs
* changeset publish.
*
* This allows the codebase to use @agentscript/* internally while publishing
* under the @sf-agentscript npm scope.
*/
import { execFileSync } from 'node:child_process';
import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
const ROOT = fileURLToPath(new URL('..', import.meta.url));
const INTERNAL_SCOPE = '@agentscript/';
const PUBLISH_SCOPE = '@sf-agentscript/';
const DIST_FILE_SUFFIXES = [
'.js',
'.mjs',
'.cjs',
'.d.ts',
'.d.mts',
'.d.cts',
'.map',
];
export function shouldRewriteDistFile(fileName) {
return DIST_FILE_SUFFIXES.some(suffix => fileName.endsWith(suffix));
}
function rewriteScopeInFile(filePath) {
const raw = readFileSync(filePath, 'utf8');
const rewritten = raw.replaceAll(INTERNAL_SCOPE, PUBLISH_SCOPE);
if (rewritten === raw) {
return false;
}
writeFileSync(filePath, rewritten);
return true;
}
export function rewriteDistFiles(directory) {
if (!existsSync(directory)) {
return 0;
}
let count = 0;
for (const entry of readdirSync(directory, { withFileTypes: true })) {
const entryPath = join(directory, entry.name);
if (entry.isDirectory()) {
count += rewriteDistFiles(entryPath);
} else if (entry.isFile() && shouldRewriteDistFile(entry.name)) {
count += rewriteScopeInFile(entryPath) ? 1 : 0;
}
}
return count;
}
function main() {
// Step 1: Discover all workspace packages
const output = execFileSync(
'pnpm',
['-r', 'list', '--json', '--depth', '-1'],
{
cwd: ROOT,
encoding: 'utf8',
}
);
const packages = JSON.parse(output);
// Step 2: Rewrite package.json files to publish scope
let packageJsonCount = 0;
for (const pkg of packages) {
const pkgJsonPath = join(pkg.path, 'package.json');
if (rewriteScopeInFile(pkgJsonPath)) {
packageJsonCount++;
console.log(
` ✓ ${pkg.name} → ${pkg.name.replace(INTERNAL_SCOPE, PUBLISH_SCOPE)}`
);
}
}
// Step 3: Rewrite changeset config so it recognizes the new package names
const changesetConfigPath = join(ROOT, '.changeset', 'config.json');
const changesetRaw = readFileSync(changesetConfigPath, 'utf8');
writeFileSync(
changesetConfigPath,
changesetRaw.replaceAll(INTERNAL_SCOPE, PUBLISH_SCOPE)
);
// Step 4: Rewrite built package outputs to publish scope
let distFileCount = 0;
for (const pkg of packages) {
distFileCount += rewriteDistFiles(join(pkg.path, 'dist'));
}
console.log(
`\nRewrote ${packageJsonCount} package.json files and ${distFileCount} dist files to ${PUBLISH_SCOPE}* scope\n`
);
// Step 5: Re-install so pnpm resolves workspace: references with new names
execFileSync('pnpm', ['install', '--no-frozen-lockfile'], {
cwd: ROOT,
stdio: 'inherit',
});
// Step 6: Publish via changeset
execFileSync('pnpm', ['changeset', 'publish'], {
cwd: ROOT,
stdio: 'inherit',
});
}
if (
process.argv[1] &&
import.meta.url === pathToFileURL(process.argv[1]).href
) {
main();
}