| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | 'use strict'; |
| 4 | |
| 5 | const {join} = require('path'); |
| 6 | const {exec} = require('child-process-promise'); |
| 7 | const {readdirSync} = require('fs'); |
| 8 | const {readJsonSync} = require('fs-extra'); |
| 9 | const {logPromise} = require('../utils'); |
| 10 | |
| 11 | const run = async ({cwd, dry, tempDirectory}) => { |
| 12 | // Cleanup from previous build. |
| 13 | await exec(`rm -rf ./build`, {cwd}); |
| 14 | |
| 15 | // NPM pack all built packages. |
| 16 | // We do this to ensure that the package.json files array is correct. |
| 17 | const builtPackages = readdirSync(join(tempDirectory, 'build/node_modules/')); |
| 18 | for (let i = 0; i < builtPackages.length; i++) { |
| 19 | await exec(`npm pack ./${builtPackages[i]}`, { |
| 20 | cwd: `${tempDirectory}/build/node_modules/`, |
| 21 | }); |
| 22 | } |
| 23 | |
| 24 | await exec('mkdir build', {cwd}); |
| 25 | await exec('mkdir build/node_modules', {cwd}); |
| 26 | await exec( |
| 27 | `cp -r ${tempDirectory}/build/node_modules/*.tgz ./build/node_modules/`, |
| 28 | {cwd} |
| 29 | ); |
| 30 | |
| 31 | // Unpack packages and prepare to publish. |
| 32 | const compressedPackages = readdirSync(join(cwd, 'build/node_modules/')); |
| 33 | for (let i = 0; i < compressedPackages.length; i++) { |
| 34 | await exec( |
| 35 | `tar -zxvf ./build/node_modules/${compressedPackages[i]} -C ./build/node_modules/`, |
| 36 | {cwd} |
| 37 | ); |
| 38 | const packageJSON = readJsonSync( |
| 39 | join(cwd, `./build/node_modules/package/package.json`) |
| 40 | ); |
| 41 | await exec( |
| 42 | `mv ./build/node_modules/package ./build/node_modules/${packageJSON.name}`, |
| 43 | {cwd} |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | // Cleanup. |
| 48 | await exec(`rm ./build/node_modules/*.tgz`, {cwd}); |
| 49 | }; |
| 50 | |
| 51 | module.exports = async params => { |
| 52 | return logPromise(run(params), 'Packing artifacts'); |
| 53 | }; |