forked from qelos-io/qelos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack-apps.mjs
More file actions
78 lines (66 loc) · 2.7 KB
/
pack-apps.mjs
File metadata and controls
78 lines (66 loc) · 2.7 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
import { readdirSync, readFileSync, writeFileSync, existsSync, rmSync, mkdirSync } from "node:fs";
import { execSync } from "node:child_process";
import { join } from "node:path";
const ignoredApps = ['db', 'redis', 'local-mcp'];
// Replace "workspace:^" references with "*" in a package.json file
function replaceWorkspaceRefs(pkgPath) {
const raw = readFileSync(pkgPath, 'utf8');
if (!raw.includes('workspace:')) return;
const pkg = JSON.parse(raw);
let changed = false;
for (const field of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
if (!pkg[field]) continue;
for (const [dep, version] of Object.entries(pkg[field])) {
if (typeof version === 'string' && version.startsWith('workspace:')) {
pkg[field][dep] = '*';
changed = true;
}
}
}
if (changed) {
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
}
}
// Replace workspace refs in the deployed directory (app + all @qelos/* packages in node_modules)
function replaceAllWorkspaceRefs(deployDir) {
// App's own package.json
replaceWorkspaceRefs(join(deployDir, 'package.json'));
// @qelos/* packages in node_modules
const qelosDir = join(deployDir, 'node_modules', '@qelos');
if (existsSync(qelosDir)) {
for (const pkg of readdirSync(qelosDir)) {
const pkgJson = join(qelosDir, pkg, 'package.json');
if (existsSync(pkgJson)) {
replaceWorkspaceRefs(pkgJson);
}
}
}
}
const apps = readdirSync('./apps').filter(
folder => !ignoredApps.includes(folder) && !folder.startsWith('.')
);
for (const folder of apps) {
const pkg = JSON.parse(readFileSync(`apps/${folder}/package.json`, 'utf8'));
const deployDir = `deploy/${folder}`;
// Clean previous deploy
if (existsSync(deployDir)) rmSync(deployDir, { recursive: true });
mkdirSync(deployDir, { recursive: true });
// pnpm deploy: copies app + installs prod deps + resolves workspace deps locally
console.log(`\n=== Deploying ${pkg.name} ===`);
execSync(`pnpm --filter ${pkg.name} deploy ${deployDir} --prod --legacy`, {
stdio: 'inherit',
timeout: 300000
});
// Replace workspace:^ with * so npm commands work in Docker
console.log(`Replacing workspace references in ${pkg.name}...`);
replaceAllWorkspaceRefs(deployDir);
// Create tarball
const tgzName = pkg.name.replace('@', '').replace('/', '-') + '.tgz';
execSync(`tar -czf apps/${folder}/${tgzName} -C ${deployDir} .`, { stdio: 'inherit' });
// Clean up
rmSync(deployDir, { recursive: true });
console.log(`Packed ${pkg.name} -> apps/${folder}/${tgzName}`);
}
// Clean top-level deploy dir
if (existsSync('deploy')) rmSync('deploy', { recursive: true });
console.log('\nAll apps packed successfully!');