UNPKG

3.55 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 resolve = require('resolve');
10
11const exitImmediate = process.argv.indexOf('--exit-immediate') > -1 || process.argv.indexOf('--exitImmediate') > -1;
12
13function _exit(code) {
14 if (process.platform === 'win32' && process.stdout.bufferSize) {
15 process.stdout.once('drain', function () {
16 process.exit(code);
17 });
18
19 return;
20 }
21
22 process.exit(code);
23}
24
25function _invokeCli(cli, cliOptions) {
26 if ('default' in cli) {
27 cli = cli.default;
28 }
29
30 cli(cliOptions)
31 .then((exitCode) => {
32 process.exitCode = typeof exitCode === 'number' ? exitCode : 0;
33
34 if (exitImmediate) {
35 _exit(process.exitCode);
36 }
37 })
38 .catch(err => {
39 process.exitCode = -1;
40 console.error(`${err.stack || err.message}`);
41 _exit(process.exitCode);
42 });
43}
44
45function _cliGlobal() {
46 let cliRootPath = path.resolve(__dirname, '..');
47 if (!fs.existsSync(path.resolve(cliRootPath, 'node_modules'))) {
48 cliRootPath = path.dirname(cliRootPath);
49 }
50
51 const packageJson = require(path.resolve(cliRootPath, './package.json'));
52 const cliVersion = packageJson['version'];
53
54 const updateNotifier = require('update-notifier');
55 updateNotifier({
56 pkg: packageJson
57 }).notify({
58 defer: false
59 });
60
61 let cli;
62 if (fs.existsSync(path.resolve(__dirname, '../src/cli/index.js'))) {
63 cli = require('../src/cli');
64 } else {
65 cli = require('../dist/src/cli');
66 }
67
68 const cliOptions = {
69 cliVersion: cliVersion,
70 cliIsGlobal: true,
71 cliRootPath: cliRootPath,
72 startTime: startTime
73 };
74
75 _invokeCli(cli, cliOptions);
76
77}
78
79// main
80let _workspaceRoot = process.cwd();
81const _args = process.argv.slice(2);
82if (_args.length >= 2 && _args[0] === 'build') {
83 const argv = require('yargs')
84 .option('config', {
85 alias: 'c',
86 type: 'string'
87 })
88 .argv;
89
90 if (argv.config) {
91 let configPath = argv.config;
92 configPath = path.isAbsolute(configPath) ? path.resolve(configPath) : path.resolve(process.cwd(), configPath);
93 _workspaceRoot = path.dirname(configPath);
94 }
95}
96resolve('@bizappframework/angular-build', {
97 basedir: _workspaceRoot
98}, (error, projectLocalCli) => {
99 if (error) {
100 _cliGlobal();
101
102 return;
103 }
104
105 let cliIsLink = false;
106 const projectLocalCliRealPath = fs.realpathSync(projectLocalCli);
107 if (projectLocalCliRealPath !== projectLocalCli) {
108 cliIsLink = true;
109 }
110
111 const cliPath = path.dirname(projectLocalCli);
112 let packageJsonPath = path.resolve(cliPath, './package.json');
113 if ((!fs.existsSync(packageJsonPath) || !fs.existsSync(path.resolve(cliPath, 'node_modules'))) &&
114 fs.existsSync(path.resolve(cliPath, '..', './package.json'))) {
115 packageJsonPath = path.resolve(cliPath, '..', './package.json');
116 }
117
118 const packageJson = require(packageJsonPath);
119 const cliVersion = packageJson['version'];
120
121 const localCliPath = path.resolve(cliPath, './cli');
122 const cli = require(localCliPath);
123
124 const cliOptions = {
125 cliVersion: cliVersion,
126 cliIsGlobal: false,
127 cliIsLink: cliIsLink,
128 cliRootPath: path.dirname(packageJsonPath),
129 startTime: startTime
130 };
131
132 _invokeCli(cli, cliOptions);
133});