1 | const shell = require('shelljs');
|
2 | const tar = require('tar');
|
3 | const path = require('path');
|
4 | const fs = require('fs-extra');
|
5 | const configs = require('../../configs/app-configs');
|
6 | const exec = require('../util/exec');
|
7 | const nginxConfTemplate = require('../../templates/nginx.conf.template');
|
8 | const startScript = require('../../templates/start.template');
|
9 |
|
10 | const tempDirName = '.archive-build';
|
11 | const nginxConfFileName = 'nginx.conf';
|
12 | const startScriptFileName = 'start.sh';
|
13 | const nodeModulesDirName = 'node_modules';
|
14 | const packageJsonFileName = 'package.json';
|
15 | const pathToTempDir = path.join(configs.cwd, tempDirName);
|
16 | const nginxConfPath = path.join(pathToTempDir, nginxConfFileName);
|
17 | const startScriptPath = path.join(pathToTempDir, startScriptFileName);
|
18 | const nodeModulesPath = path.join(configs.cwd, nodeModulesDirName);
|
19 | const 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 |
|
41 | await exec('npm run build');
|
42 |
|
43 | console.timeEnd('Build application time');
|
44 | console.time('Remove build dependencies time');
|
45 |
|
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 |
|
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 | })();
|