-
Notifications
You must be signed in to change notification settings - Fork 48.4k
/
Copy pathnpm-pack-and-unpack.js
53 lines (45 loc) · 1.57 KB
/
npm-pack-and-unpack.js
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
#!/usr/bin/env node
'use strict';
const {join} = require('path');
const {exec} = require('child-process-promise');
const {readdirSync} = require('fs');
const {readJsonSync} = require('fs-extra');
const {logPromise} = require('../utils');
const run = async ({cwd, dry, tempDirectory}) => {
// Cleanup from previous build.
await exec(`rm -rf ./build`, {cwd});
// NPM pack all built packages.
// We do this to ensure that the package.json files array is correct.
const builtPackages = readdirSync(join(tempDirectory, 'build/node_modules/'));
for (let i = 0; i < builtPackages.length; i++) {
await exec(`npm pack ./${builtPackages[i]}`, {
cwd: `${tempDirectory}/build/node_modules/`,
});
}
await exec('mkdir build', {cwd});
await exec('mkdir build/node_modules', {cwd});
await exec(
`cp -r ${tempDirectory}/build/node_modules/*.tgz ./build/node_modules/`,
{cwd}
);
// Unpack packages and prepare to publish.
const compressedPackages = readdirSync(join(cwd, 'build/node_modules/'));
for (let i = 0; i < compressedPackages.length; i++) {
await exec(
`tar -zxvf ./build/node_modules/${compressedPackages[i]} -C ./build/node_modules/`,
{cwd}
);
const packageJSON = readJsonSync(
join(cwd, `./build/node_modules/package/package.json`)
);
await exec(
`mv ./build/node_modules/package ./build/node_modules/${packageJSON.name}`,
{cwd}
);
}
// Cleanup.
await exec(`rm ./build/node_modules/*.tgz`, {cwd});
};
module.exports = async params => {
return logPromise(run(params), 'Packing artifacts');
};