|
| 1 | +import * as path from 'path'; |
| 2 | +import * as fs from 'fs-extra'; |
| 3 | +import * as _ from 'lodash'; |
| 4 | +import { spawn as spawnAsync, SpawnOptions } from 'child_process'; |
| 5 | + |
| 6 | +const OUTPUT_DIR = path.join(__dirname, 'build'); |
| 7 | + |
| 8 | +const pjson = require(path.join(__dirname, './package.json')); |
| 9 | + |
| 10 | +const spawn = (command: string, args: string[] = [], options?: SpawnOptions) => { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + const proc = spawnAsync(command, args, options); |
| 13 | + proc.on('exit', (code) => { |
| 14 | + if (code === 0) resolve(); |
| 15 | + else reject(new Error( |
| 16 | + `Spawn ${command} ${args.join(' ')} exited with ${code}` |
| 17 | + )); |
| 18 | + }); |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +const updateRegistryJs = async (platform: string) => { |
| 23 | + // Install registry JS for a given target platform, and copy its native |
| 24 | + // module into our bundle. |
| 25 | + const registryJsDir = path.join(OUTPUT_DIR, 'node_modules', 'registry-js'); |
| 26 | + await fs.rmdir(registryJsDir).catch(() => {}); |
| 27 | + await spawn('npm', [ |
| 28 | + 'install', |
| 29 | + 'registry-js', |
| 30 | + '--no-save' |
| 31 | + ], { |
| 32 | + cwd: OUTPUT_DIR, |
| 33 | + stdio: 'inherit', |
| 34 | + env: Object.assign({}, process.env, { 'npm_config_platform': platform }) |
| 35 | + }); |
| 36 | + await fs.copyFile( |
| 37 | + path.join(registryJsDir, 'build', 'Release', 'registry.node'), |
| 38 | + path.join(OUTPUT_DIR, 'bundle', 'registry.node'), |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +const packageApp = async () => { |
| 43 | + console.log('Preparing packaging directory'); |
| 44 | + await fs.emptyDir(OUTPUT_DIR); |
| 45 | + |
| 46 | + // Copy all normally deployable files: |
| 47 | + const filesToCopy = pjson.files; |
| 48 | + await Promise.all(filesToCopy.map((file: string) => |
| 49 | + fs.copy( |
| 50 | + path.join(__dirname, file), |
| 51 | + path.join('build', file) |
| 52 | + ) |
| 53 | + )); |
| 54 | + |
| 55 | + await Promise.all([ |
| 56 | + // Include the packaging & build scripts: |
| 57 | + 'build-release.sh', |
| 58 | + // Include package-lock.json, to keep dependencies locked: |
| 59 | + 'package-lock.json', |
| 60 | + // Add the fully bundled source (not normally packaged by npm): |
| 61 | + 'bundle' |
| 62 | + ].map((extraFile) => |
| 63 | + fs.copy(path.join(__dirname, extraFile), path.join(OUTPUT_DIR, extraFile)) |
| 64 | + )); |
| 65 | + |
| 66 | + // Edit the package to replace deps with the bundle: |
| 67 | + pjson.files.push('/bundle'); |
| 68 | + pjson.dependencies = _.pick(pjson.dependencies, pjson.oclif.dependenciesToPackage); |
| 69 | + delete pjson.scripts.prepack; // We don't want to rebuild |
| 70 | + await fs.writeJson(path.join(OUTPUT_DIR, 'package.json'), pjson); |
| 71 | + |
| 72 | + const buildScript = path.join(OUTPUT_DIR, 'build-release.sh'); |
| 73 | + |
| 74 | + // Run build-release in this folder, for each platform: |
| 75 | + console.log('Building for Linux'); |
| 76 | + await updateRegistryJs('linux'); |
| 77 | + await spawn(buildScript, ['linux'], { cwd: OUTPUT_DIR, stdio: 'inherit' }); |
| 78 | + |
| 79 | + console.log('Building for Darwin'); |
| 80 | + await updateRegistryJs('darwin'); |
| 81 | + await spawn(buildScript, ['darwin'], { cwd: OUTPUT_DIR, stdio: 'inherit' }); |
| 82 | + |
| 83 | + console.log('Building for Win32'); |
| 84 | + await updateRegistryJs('win32'); |
| 85 | + await spawn(buildScript, ['win32'], { cwd: OUTPUT_DIR, stdio: 'inherit' }); |
| 86 | +} |
| 87 | + |
| 88 | +packageApp().catch((error: any) => { |
| 89 | + console.error(error.message); |
| 90 | +}); |
0 commit comments