UNPKG

3.21 kBJavaScriptView Raw
1const shell = require('shelljs');
2const tar = require('tar');
3const path = require('path');
4const fs = require('fs-extra');
5const configs = require('../../configs/app-configs');
6const exec = require('../util/exec');
7const nginxConfTemplate = require('../../templates/nginx.conf.template');
8const startScript = require('../../templates/start.template');
9
10const tempDirName = '.archive-build';
11const nginxConfFileName = 'nginx.conf';
12const startScriptFileName = 'start.sh';
13const nodeModulesDirName = 'node_modules';
14const packageJsonFileName = 'package.json';
15const pathToTempDir = path.join(configs.cwd, tempDirName);
16const nginxConfPath = path.join(pathToTempDir, nginxConfFileName);
17const startScriptPath = path.join(pathToTempDir, startScriptFileName);
18const nodeModulesPath = path.join(configs.cwd, nodeModulesDirName);
19const packageJsonPath = path.join(configs.cwd, packageJsonFileName);
20
21(async () => {
22 try {
23 console.time('Total time');
24 console.time('Setting up time');
25
26 await fs.emptyDir(pathToTempDir);
27
28 const nginxConf = configs.localNginxConf
29 ? await fs.readFile(configs.localNginxConf, 'utf8')
30 : nginxConfTemplate;
31
32 await Promise.all([
33 fs.writeFile(nginxConfPath, nginxConf, 'utf8'),
34 fs.writeFile(startScriptPath, startScript, { encoding: 'utf8', mode: 0o555 }),
35 fs.remove(configs.buildPath),
36 ]);
37
38 console.timeEnd('Setting up time');
39 console.time('Build application time');
40 // run build script
41 await exec('npm run build');
42
43 console.timeEnd('Build application time');
44 console.time('Remove build dependencies time');
45 // if yarn is available prune dev dependencies with yarn, otherwise use npm
46 if (configs.useYarn && shell.which('yarn')) {
47 await exec('yarn install --production --ignore-optional --frozen-lockfile --ignore-scripts --prefer-offline');
48 } else {
49 await exec('npm prune --production');
50 }
51
52 console.timeEnd('Remove build dependencies time');
53 console.time('Archive build time');
54 await Promise.all([
55 fs.copy(configs.buildPath, path.join(pathToTempDir, configs.buildPath)),
56 fs.copy(nodeModulesPath, path.join(pathToTempDir, nodeModulesDirName)),
57 fs.copy(packageJsonPath, path.join(pathToTempDir, packageJsonFileName)),
58 ...configs.additionalBuildPath.map(additionalPath => (
59 fs.copy(path.join(configs.cwd, additionalPath), path.join(pathToTempDir, additionalPath))
60 ))
61 ]);
62 await tar.c(
63 {
64 file: configs.archiveName,
65 cwd: pathToTempDir
66 },
67 fs.readdirSync(pathToTempDir)
68 );
69
70 console.timeEnd('Archive build time');
71 console.time('Cleanup time');
72
73 // remove temp directory
74 await fs.remove(pathToTempDir);
75
76 console.timeEnd('Cleanup time');
77 console.timeEnd('Total time');
78 } catch (err) {
79 console.error('Error during archive-build.');
80 if (configs.debug) {
81 console.error(err);
82 }
83 process.exit(1);
84 }
85})();