-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Expand file tree
/
Copy pathreleasePack.mts
More file actions
107 lines (95 loc) · 3.1 KB
/
releasePack.mts
File metadata and controls
107 lines (95 loc) · 3.1 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
/* eslint-disable no-console */
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { $ } from 'execa';
import * as path from 'path';
import * as fs from 'fs/promises';
interface WorkspaceDefinition {
name: string;
version: string;
path: string;
private: boolean;
}
interface Manifest {
packages: Record<string, string>;
}
interface RunOptions {
packages?: string[];
outDir: string;
concurrency: number;
}
async function packWorkspace(workspace: WorkspaceDefinition, outDir: string): Promise<string> {
const packages: Record<string, string> = {};
const { stdout: zipFilePath } = await $({
cwd: workspace.path,
})`pnpm pack --pack-destination ${outDir}`;
packages[workspace.name] = zipFilePath;
return zipFilePath;
}
async function run({ packages, outDir, concurrency }: RunOptions) {
const allWorkspaces: WorkspaceDefinition[] = await $`pnpm -r ls --depth -1 --json`.then(
(result) => JSON.parse(result.stdout),
);
const workspacesMap = new Map(allWorkspaces.map((workspace) => [workspace.name, workspace]));
const publicPackages = allWorkspaces
.filter((workspace) => !workspace.private)
.map((workspace) => workspace.name);
const packagesToPack = packages || publicPackages;
const workspacesToPack = packagesToPack.map((name) => {
const workspace = workspacesMap.get(name);
if (!workspace) {
throw new Error(`Workspace ${name} not found`);
}
return workspace;
});
const absoluteDestination = path.resolve(outDir);
const workspacesIterator = workspacesToPack.values();
const manifest: Manifest = { packages: {} };
const workers = Array.from({ length: concurrency }).map(async () => {
for (const workspace of workspacesIterator) {
/* eslint-disable no-await-in-loop */
console.log(`packing "${workspace.name}"`);
const zipFilePath = await packWorkspace(workspace, absoluteDestination);
const newName = path.join(absoluteDestination, `${workspace.name}.tgz`);
await fs.mkdir(path.dirname(newName), { recursive: true });
await fs.rename(zipFilePath, newName);
const relativeZipFilePath = path.relative(absoluteDestination, newName);
manifest.packages[workspace.name] = relativeZipFilePath;
console.log(`packed "${zipFilePath}"`);
/* eslint-enable no-await-in-loop */
}
});
await Promise.all(workers);
await fs.writeFile(
path.join(absoluteDestination, 'manifest.json'),
JSON.stringify(manifest, null, 2),
);
}
yargs(hideBin(process.argv))
.command<RunOptions>(
'$0',
'Pack workspaces.',
(command) => {
return command
.option('packages', {
describe: 'Workspace Packages to pack, defaults to public packages',
type: 'array',
alias: 'p',
})
.option('outDir', {
default: './packed',
describe: 'Destination folder',
type: 'string',
})
.option('concurrency', {
default: 5,
describe: 'Number of concurrent packing processes',
type: 'number',
});
},
run,
)
.help()
.strict(true)
.version(false)
.parse();