1import * as stream from 'stream';
2import * as path from 'path';
3import * as fs from 'fs';
4import { promisify } from 'util';
5
6import * as tar from 'tar';
7import { execa, execaNode } from 'execa';
8import Arborist from '@npmcli/arborist';
9import packlist from 'npm-packlist';
10
11import { workspaceRoot, require } from './constants.mjs';
12import { getPackageManifest, getPackageArtifact } from './packages.mjs';
13
14const pipeline = promisify(stream.pipeline);
15
16const buildPackage = async cwd => {
17 const manifest = getPackageManifest(cwd);
18 console.log('> Building', manifest.name);
19
20 try {
21 await execa('run-s', ['build'], {
22 preferLocal: true,
23 localDir: workspaceRoot,
24 cwd,
25 });
26 } catch (error) {
27 console.error('> Build failed', manifest.name);
28 throw error;
29 }
30};
31
32const preparePackage = async cwd => {
33 const manifest = getPackageManifest(cwd);
34 console.log('> Preparing', manifest.name);
35
36 try {
37 await execaNode(require.resolve('../../prepare/index.js'), { cwd });
38 } catch (error) {
39 console.error('> Preparing failed', manifest.name);
40 throw error;
41 }
42};
43
44const packPackage = async cwd => {
45 const manifest = getPackageManifest(cwd);
46 const artifact = getPackageArtifact(cwd);
47 console.log('> Packing', manifest.name);
48
49 const arborist = new Arborist({ path: cwd });
50 const tree = await arborist.loadActual();
51
52 try {
53 await pipeline(
54 tar.create(
55 {
56 cwd,
57 prefix: 'package/',
58 portable: true,
59 gzip: true,
60 },
61 (await packlist(tree)).map(f => `./${f}`)
62 ),
63 fs.createWriteStream(path.resolve(cwd, artifact))
64 );
65 } catch (error) {
66 console.error('> Packing failed', manifest.name);
67 throw error;
68 }
69};
70
71export { buildPackage, preparePackage, packPackage };