-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.js
246 lines (211 loc) · 7.07 KB
/
bootstrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import ora from 'ora';
import Actinium from 'parse/node.js';
import handlebars from 'handlebars';
import * as SDK from '@atomic-reactor/reactium-sdk-core/core';
import path from 'node:path';
import crypto from 'node:crypto';
import { fileURLToPath } from 'node:url';
import ChildProcess from 'node:child_process';
import _ from 'underscore';
import axios from 'axios';
import op from 'object-path';
import chalk from 'chalk';
import fs from 'fs-extra';
import { globbySync as globby } from './globby-patch.js';
import moment from 'moment';
import os from 'os';
import semver from 'semver';
import prettier from 'prettier';
import portscanner from 'portscanner';
import generator from './lib/generator.js';
import table from 'text-table';
import * as tar from 'tar';
import slugify from 'slugify';
import DeleteEmpty from 'delete-empty';
import ActionSequence from 'action-sequence';
import inquirer from 'inquirer';
import prompt from 'prompt';
import decompress from '@atomic-reactor/decompress';
import { error, message } from './lib/messenger.js';
import pad from './lib/pad.js';
import * as AuthSDK from './lib/auth.js';
import { useSpinner } from './lib/useSpinner.js';
import targetApp from './lib/targetApp.js';
const Hook = SDK.Hook;
const normalizePath = (...args) => path.normalize(path.join(...args));
const root = path.dirname(fileURLToPath(import.meta.url));
const config = fs.readJsonSync(normalizePath(root, 'config.json'));
const packagejson = fs.readJsonSync(normalizePath(root, 'package.json'));
const importer = (path, options) => {
options = options || {};
if (String(path).endsWith('.json')) {
return fs.readJsonSync(path);
}
return import(`file://${path}`, options);
}
const bootstrap = async () => {
const { spawn } = ChildProcess;
const deleteEmpty = DeleteEmpty.sync;
const cwd = path.resolve(process.cwd());
const homedir = os.homedir();
const ver = op.get(packagejson, 'version');
global.Actinium = Actinium;
global.Hook = Hook;
global.Spinner = ora({ spinner: 'dots', color: 'cyan' });
// Extend Spinner
Spinner.message = (...args) => {
Spinner.start();
Spinner.text = args.join(' ');
};
let props = { config, cwd, homedir, root, ver };
const runCommand = (cmd, args = [], opt = {}) =>
new Promise((resolve, reject) => {
const options = { shell: true, stdio: 'inherit', ...opt };
const ps = spawn(cmd, args, options);
ps.on('error', err => {
console.log(`Error executing ${cmd}`);
console.error(err);
reject(err);
return;
});
ps.on('close', code => {
if (code !== 0) {
console.error(`Error executing ${cmd}`);
reject();
} else {
resolve(ps);
}
});
});
const mergeActions = (...args) =>
args.reduce((output, actions, i) => {
Object.keys(actions).forEach(key =>
op.set(output, `${key}-${i}`, op.get(actions, key)),
);
return output;
}, {});
const initialize = async props => {
// import arlic-hooks.js files
const hooks = globby([path.join(cwd, '/**/arcli-hooks.js')]);
while (hooks.length > 0) {
const hook = hooks.shift();
await importer(hook);
}
// Get application config
const appConfigFile = normalizePath(cwd, '.core', '.cli', 'config.json');
if (fs.existsSync(appConfigFile)) {
const appConfig = await importer(appConfigFile);
props.config = Object.assign(props.config, appConfig);
}
// Get local config
const localConfigFile = normalizePath(homedir, '.arcli', 'config.json');
props.localConfigFile = localConfigFile;
if (fs.existsSync(localConfigFile)) {
const localConfig = await importer(localConfigFile);
props.config = Object.assign(props.config, localConfig);
} else {
// Create the localized config if it doesn't exist
const contents = JSON.stringify(props.config, null, 2);
fs.ensureFileSync(localConfigFile);
fs.writeFileSync(localConfigFile, contents);
}
// Get project config
const projConfigFile = normalizePath(cwd, '.cli', 'config.json');
if (fs.existsSync(projConfigFile)) {
const projConfig = await importer(projConfigFile);
props.config = Object.assign(props.config, projConfig);
}
return props;
};
const normalizeCommandPath = dir =>
path
.normalize(
String(`${dir}/**/*index.js`)
.replace(/\[root\]/gi, global.arcli.props.root)
.replace(/\[cwd\]/gi, global.arcli.props.cwd),
)
.split(/[\\\/]/g)
.join(path.posix.sep);
const rootCommands = () => globby(normalizeCommandPath('[root]/commands'))
// fix volume paths on Windows
.map(p => p.replace(/^([a-z]{1}):/i, '/$1:'));
const flagsToParams = ({ opt = {}, flags }) =>
flags.reduce((obj, key) => {
let val = opt[key];
val = typeof val === 'function' ? undefined : val;
if (val) {
obj[key] = val;
}
return obj;
}, {});
const isEmpty = p =>
Boolean(fs.existsSync(p) ? fs.readdirSync(p).length < 1 : true);
// Glob the functions
const commands = () => {
// Find commands
const dirs = config.commands || [];
const globs = dirs
.filter(dir => dir !== '[root]/commands')
.map(dir => normalizeCommandPath(dir));
const deep = op.get(config, 'depth', 25);
return globby(globs, { deep })
// fix volume paths on Windows
.map(p => p.replace(/^([a-z]{1}):/i, '/$1:'));
};
global.arcli = {
_,
...AuthSDK,
ActionSequence,
Actinium,
Spinner,
axios,
chalk,
commands,
crypto,
decompress,
deleteEmpty,
error,
fileURLToPath,
flagsToParams,
fs,
path,
generator,
globby,
handlebars,
importer,
inquirer,
isEmpty,
homedir,
mergeActions,
message,
moment,
normalizePath,
op,
ora,
pad,
portscanner,
prettier,
prompt,
props,
root,
rootCommands,
runCommand,
semver,
slugify,
table,
tar,
targetApp,
tmp: normalizePath(homedir, 'tmp'),
useSpinner,
SDK,
};
// Build the props object
global.arcli.props = await initialize(global.arcli.props);
global.arcli.props.inquirer = inquirer;
global.arcli.props.prompt = prompt;
global.arcli.prefix = chalk.cyan(
String(arcli.props.config.prompt.prefix).trim(),
);
return global.arcli;
};
export default bootstrap;