-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ts
More file actions
executable file
·193 lines (165 loc) · 6.33 KB
/
run.ts
File metadata and controls
executable file
·193 lines (165 loc) · 6.33 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env ./bootstrap.sh
import {$, file, Glob, write} from "bun";
import {dirname, join} from "path";
process.env.FORCE_COLOR = "1";
process.env.CLAUDECODE = "1";
export async function version() {
const branch = process.env.GITHUB_REF_NAME || (await $`git rev-parse --abbrev-ref HEAD`.quiet()).text().trim();
const buildNumber = process.env.GITHUB_RUN_NUMBER || new Date().toISOString().replace(/[-:T]/g, '').split('.')[0];
const revisions = (await $`git rev-list --count ${branch}`.quiet()).text().trim();
const version = `0.${revisions}.${buildNumber}`;
console.log(`version: ${version}`);
return version;
}
export async function tag() {
const ver = await version();
await $`git config --global user.name "Server"`.quiet();
await $`git config --global user.email "server@bodar.com"`.quiet();
await $`git tag -a ${ver} -m "Release ${ver}"`.quiet();
await $`git push origin ${ver}`.quiet();
}
export async function clean() {
await $`rm -rf artifacts`;
await $`bun install --ignore-scripts`.quiet();
}
export async function check() {
await $`bun run --bun tsgo --noEmit`;
}
export async function test(...args: string[]) {
await $`bun test ${args}`;
}
export async function coverage() {
await test('--coverage');
}
export async function dev() {
await test('--watch');
}
export async function demo() {
await $`rm -rf packages/dataflow/out`;
await $`bun run --watch packages/dataflow/server.ts`;
}
export async function build() {
await clean();
await exports();
await check();
await test();
}
export async function exports(packageGlob: string = "packages/*/package.json") {
for await (const f of new Glob(packageGlob).scan(".")) {
const packageJsonFile = file(f!);
const packageJson = await packageJsonFile.json();
const parent = dirname(f!);
const srcDir = join(parent, 'src');
const exports: Record<string, string> = {};
for await (const ts of new Glob("./**/*.ts").scan(srcDir)) {
const content = await file(join(srcDir, ts)).text();
if(content.includes('@module')) exports[ts] = ts.replace('./', './src/');
}
packageJson.exports = Object.fromEntries(Object.entries(exports).sort((a, b) => a[0].localeCompare(b[0])));
packageJson.files = [
"src",
"README.md",
"package.json"
]
await write(packageJsonFile, JSON.stringify(packageJson, null, 2));
console.log(`Updated exports and files for ${packageJson.name} (${Object.keys(exports).length} files)`);
}
}
export async function docs() {
await $`rm -rf packages/dataflow/out`;
await $`bun run packages/dataflow/docs.ts`;
}
/**
* Prepares packages for JSR publishing by:
* 1. Replacing workspace: dependencies with actual version numbers
* 2. Creating jsr.json config files
*
* Returns info needed to restore workspace dependencies after publish.
*/
export async function jsr(): Promise<{ packageFile: string; depName: string; originalVersion: string }[]> {
const v = await version();
const modifications: { packageFile: string; depName: string; originalVersion: string }[] = [];
for await (const f of new Glob("packages/*/package.json").scan(".")) {
const packageJsonFile = file(f);
const packageJson = await packageJsonFile.json();
const parent = dirname(f!);
const jsrFile = file(join(parent, 'jsr.json'));
if (packageJson.dependencies) {
for (const [depName, depVersion] of Object.entries(packageJson.dependencies)) {
if (typeof depVersion === 'string' && depVersion.startsWith('workspace:')) {
modifications.push({ packageFile: f, depName, originalVersion: depVersion });
packageJson.dependencies[depName] = v;
}
}
await write(packageJsonFile, JSON.stringify(packageJson, null, 2));
}
const jsrConfig: any = {
name: packageJson.name,
version: v,
exports: packageJson.exports,
license: 'Apache-2.0'
};
if (packageJson.files) {
jsrConfig.publish = {
include: packageJson.files
};
}
await write(jsrFile, JSON.stringify(jsrConfig, null, 2));
}
return modifications;
}
/**
* Restores workspace: dependencies and removes jsr.json files after publishing.
*/
async function cleanupAfterPublish(modifications: { packageFile: string; depName: string; originalVersion: string }[]) {
// Restore workspace: dependency versions
const byFile = new Map<string, { depName: string; originalVersion: string }[]>();
for (const mod of modifications) {
if (!byFile.has(mod.packageFile)) byFile.set(mod.packageFile, []);
byFile.get(mod.packageFile)!.push(mod);
}
for (const [packageFile, mods] of byFile) {
const packageJsonFile = file(packageFile);
const packageJson = await packageJsonFile.json();
for (const { depName, originalVersion } of mods) {
packageJson.dependencies[depName] = originalVersion;
}
await write(packageJsonFile, JSON.stringify(packageJson, null, 2));
}
// Remove jsr.json files
for await (const f of new Glob("packages/*/jsr.json").scan(".")) {
await $`rm -f ${f}`.quiet();
}
}
export async function publish(dryRun: string = "") {
const modifications = await jsr();
const isDryRun = dryRun === "--dry-run" || dryRun === "dry-run";
const dryRunFlag = isDryRun ? ["--dry-run"] : [];
try {
if (process.env.JSR_TOKEN) {
await $`bunx jsr publish --allow-dirty --verbose ${dryRunFlag} --token ${process.env.JSR_TOKEN}`;
} else {
await $`bunx jsr publish --allow-dirty --verbose ${dryRunFlag}`;
}
} finally {
await cleanupAfterPublish(modifications);
}
}
export async function ci() {
await build();
await docs();
await publish();
}
const command = process.argv[2] || 'build';
const args = process.argv.slice(3);
try {
await eval(command)(...args);
} catch (e: any) {
if (e instanceof ReferenceError) {
const { exitCode } = await $`${command} ${args}`.nothrow();
process.exit(exitCode);
} else {
console.error('Command failed:', command, ...args, e.message);
process.exit(1);
}
}