UNPKG

5.34 kBJavaScriptView Raw
1'use strict';
2
3process.title = 'angular-build';
4
5const startTime = Date.now();
6
7const fs = require('fs');
8const path = require('path');
9const supportsColor = require('supports-color');
10const SemVer = require('semver').SemVer;
11
12let forceExit = process.argv.indexOf('--force-exit') > -1;
13const g = typeof global !== 'undefined' ? global : {};
14
15function _colorize(str, key) {
16 if (!supportsColor) {
17 return str;
18 }
19
20 const buf = [];
21 buf.push(key);
22 buf.push(str);
23 buf.push('\u001b[37m');
24 buf.push('\u001b[39m\u001b[22m');
25 return buf.join('');
26}
27
28function _yellow(str) {
29 return _colorize(str, '\u001b[1m\u001b[33m');
30}
31
32function _exit(code) {
33 if (process.platform === 'win32' && process.stdout.bufferSize) {
34 process.stdout.once('drain', function () {
35 process.exit(code);
36 });
37
38 return;
39 }
40
41 process.exit(code);
42}
43
44function _invokeCli(cli, cliOptions) {
45 if ('default' in cli) {
46 cli = cli.default;
47 }
48
49 cli(cliOptions)
50 .then((exitCode) => {
51 process.exitCode = typeof exitCode === 'number' ? exitCode : 0;
52 if (g._telemetryFlushStartTime) {
53 const flushDuration = Date.now() - g._telemetryFlushStartTime;
54 forceExit = forceExit || flushDuration > 1500;
55 }
56
57 if (forceExit) {
58 _exit(process.exitCode);
59 }
60 })
61 .catch(err => {
62 process.exitCode = -1;
63 console.error(`${err.stack || err.message}`);
64 _exit(process.exitCode);
65 });
66}
67
68function _cliGlobal() {
69 let cliRootPath = path.resolve(__dirname, '..');
70 if (!fs.existsSync(path.resolve(cliRootPath, 'node_modules'))) {
71 cliRootPath = path.dirname(cliRootPath);
72 }
73
74 const packageJson = require(path.resolve(cliRootPath, './package.json'));
75 const cliVersion = packageJson['version'];
76
77 const updateNotifier = require('update-notifier');
78 updateNotifier({
79 pkg: packageJson
80 }).notify({
81 defer: false
82 });
83
84 let cli;
85 if (fs.existsSync(path.resolve(__dirname, '../src/cli/index.js'))) {
86 cli = require('../src/cli');
87 } else {
88 cli = require('../dist/src/cli');
89 }
90
91 const cliOptions = {
92 args: process.argv.slice(2),
93 cliVersion: cliVersion,
94 cliIsGlobal: true,
95 cliRootPath: cliRootPath,
96 startTime: startTime
97 };
98
99 _invokeCli(cli, cliOptions);
100
101}
102
103function _cliLocal(projectRoot) {
104 const resolve = require('resolve');
105
106 resolve('@bizappframework/angular-build', {
107 basedir: projectRoot
108 }, (error, projectLocalCli) => {
109 if (error) {
110 _cliGlobal();
111
112 return;
113 }
114
115 let cliIsLink = false;
116 const projectLocalCliRealPath = fs.realpathSync(projectLocalCli);
117 if (projectLocalCliRealPath !== projectLocalCli) {
118 cliIsLink = true;
119 }
120
121 // projectLocalCli -> node_modules\@bizappframework\angular-build\index.js for locally installed
122 // projectLocalCli -> node_modules\@bizappframework\angular-build\dist\index.js for link
123 const cliPath = path.dirname(projectLocalCli);
124 let packageJsonPath = path.resolve(cliPath, './package.json');
125 if ((!fs.existsSync(packageJsonPath) || !fs.existsSync(path.resolve(cliPath, 'node_modules'))) &&
126 fs.existsSync(path.resolve(cliPath, '..', './package.json'))) {
127 packageJsonPath = path.resolve(cliPath, '..', './package.json');
128 }
129
130 const packageJson = require(packageJsonPath);
131 const cliVersion = packageJson['version'];
132
133 const localCliPath = path.resolve(cliPath, './cli');
134 const cli = require(localCliPath);
135
136 const cliOptions = {
137 args: process.argv.slice(2),
138 cliVersion: cliVersion,
139 cliIsGlobal: false,
140 cliIsLink: cliIsLink,
141 cliRootPath: path.dirname(packageJsonPath),
142 startTime: startTime
143 };
144
145 _invokeCli(cli, cliOptions);
146 });
147}
148
149// main
150const _version = new SemVer(process.version);
151if (_version.compare(new SemVer('8.9.0')) < 0) {
152 console.warn(_yellow(`You are running version ${_version.version} of Node, which will not be supported by the angular-build cli.\n` +
153 'The official Node version that will be supported is 8.9.0 and greater.'));
154}
155
156let _projectRoot = process.cwd();
157const _args = process.argv.slice(2);
158let forceUseLocalCli = false;
159if (_args.length >= 2 && _args[0] === 'build') {
160 const argv = require('yargs')
161 .option('config', {
162 alias: 'c',
163 type: 'string'
164 })
165 .option('forceUseLocalCli', {
166 type: 'boolean',
167 boolean: true
168 })
169 .argv;
170
171 if (argv.config) {
172 let configPath = argv.config;
173 configPath = path.isAbsolute(configPath) ? path.resolve(configPath) : path.resolve(_projectRoot, configPath);
174 _projectRoot = path.dirname(configPath);
175 }
176
177 forceUseLocalCli = argv.forceUseLocalCli;
178}
179
180const _localCliPath = path.resolve(forceUseLocalCli ? _projectRoot : process.cwd(), 'node_modules/@bizappframework/angular-build');
181const _localCliPathExists = fs.existsSync(_localCliPath);
182_localCliPathExists ? _cliLocal(_projectRoot) : _cliGlobal();