1 | import path from 'path';
|
2 | import glob from 'glob';
|
3 | import Report from '@feflow/report';
|
4 | import commandLineUsage from 'command-line-usage';
|
5 |
|
6 | import Commander from './commander';
|
7 | import Hook from './hook';
|
8 | import Binp from './universal-pkg/binp';
|
9 | import logger from './logger';
|
10 | import loadPlugins from './plugin/loadPlugins';
|
11 | import loadUniversalPlugin from './plugin/loadUniversalPlugin';
|
12 | import loadDevkits from './devkit/loadDevkits';
|
13 | import getCommandLine from './devkit/commandOptions';
|
14 | import {
|
15 | FEFLOW_HOME,
|
16 | FEFLOW_BIN,
|
17 | FEFLOW_LIB,
|
18 | UNIVERSAL_PKG_JSON,
|
19 | UNIVERSAL_MODULES,
|
20 | HOOK_TYPE_ON_COMMAND_REGISTERED,
|
21 | LOG_FILE
|
22 | } from '../shared/constant';
|
23 | import { safeDump, parseYaml } from '../shared/yaml';
|
24 | import { FefError } from '../shared/fefError';
|
25 | import { setServerUrl } from '../shared/git';
|
26 | import { UniversalPkg } from './universal-pkg/dep/pkg';
|
27 | import CommandPicker, {
|
28 | LOAD_UNIVERSAL_PLUGIN,
|
29 | LOAD_PLUGIN,
|
30 | LOAD_DEVKIT,
|
31 | LOAD_ALL
|
32 | } from './command-picker';
|
33 | import { checkUpdate } from './resident';
|
34 | import {
|
35 | mkdirAsync,
|
36 | statAsync,
|
37 | unlinkAsync,
|
38 | writeFileAsync,
|
39 | readFileAsync
|
40 | } from '../shared/fs';
|
41 | import { isInstalledPM } from '../shared/npm';
|
42 |
|
43 | const pkg = require('../../package.json');
|
44 |
|
45 | export default class Feflow {
|
46 | public args: any;
|
47 | public cmd: any;
|
48 | public projectConfig: any;
|
49 | public projectPath: any;
|
50 | public version: string;
|
51 | public logger: any;
|
52 | public loggerPath: any;
|
53 | public commander: any;
|
54 | public hook: any;
|
55 | public root: any;
|
56 | public rootPkg: any;
|
57 | public universalPkgPath: string;
|
58 | public universalModules: string;
|
59 | public config: any;
|
60 | public configPath: any;
|
61 | public bin: string;
|
62 | public lib: string;
|
63 | public universalPkg: UniversalPkg;
|
64 | public reporter: any;
|
65 | public commandPick: CommandPicker | null;
|
66 | public fefError: FefError;
|
67 |
|
68 | constructor(args: any) {
|
69 | args = args || {};
|
70 | const configPath = path.join(FEFLOW_HOME, '.feflowrc.yml');
|
71 | this.root = FEFLOW_HOME;
|
72 | this.bin = path.join(FEFLOW_HOME, FEFLOW_BIN);
|
73 | this.lib = path.join(FEFLOW_HOME, FEFLOW_LIB);
|
74 | this.rootPkg = path.join(FEFLOW_HOME, 'package.json');
|
75 | this.loggerPath = path.join(FEFLOW_HOME, LOG_FILE);
|
76 | this.universalPkgPath = path.join(FEFLOW_HOME, UNIVERSAL_PKG_JSON);
|
77 | this.universalModules = path.join(FEFLOW_HOME, UNIVERSAL_MODULES);
|
78 | this.args = args;
|
79 | this.version = pkg.version;
|
80 | this.config = parseYaml(configPath);
|
81 | setServerUrl(this.config?.serverUrl);
|
82 | this.configPath = configPath;
|
83 | this.hook = new Hook();
|
84 | this.commander = new Commander((cmdName: string) => {
|
85 | this.hook.emit(HOOK_TYPE_ON_COMMAND_REGISTERED, cmdName);
|
86 | });
|
87 | this.logger = logger({
|
88 | debug: Boolean(args.debug),
|
89 | silent: Boolean(args.silent)
|
90 | });
|
91 | this.reporter = new Report(this);
|
92 | this.universalPkg = new UniversalPkg(this.universalPkgPath);
|
93 | this.commandPick = null;
|
94 | this.fefError = new FefError(this);
|
95 | }
|
96 |
|
97 | async init(cmd: string | undefined) {
|
98 | this.reporter.init(cmd);
|
99 |
|
100 | await Promise.all([
|
101 | this.initClient(),
|
102 | this.initPackageManager(),
|
103 | this.initBinPath()
|
104 | ]);
|
105 |
|
106 | const disableCheck =
|
107 | this.args['disable-check'] || this.config?.disableCheck;
|
108 | if (!disableCheck) {
|
109 | checkUpdate(this);
|
110 | }
|
111 |
|
112 | this.commandPick = new CommandPicker(this, cmd);
|
113 |
|
114 | if (this.commandPick.isAvailable()) {
|
115 |
|
116 | this.logger.debug('find cmd in cache');
|
117 | this.commandPick.pickCommand();
|
118 | await this.loadCommands(LOAD_DEVKIT);
|
119 | } else {
|
120 |
|
121 | this.logger.debug('not find cmd in cache');
|
122 | await this.loadCommands(this.commandPick.getLoadOrder());
|
123 |
|
124 | this.commandPick.checkCommand();
|
125 | }
|
126 | }
|
127 |
|
128 | async initClient() {
|
129 | const { rootPkg } = this;
|
130 | let pkgInfo = null;
|
131 | try {
|
132 | await statAsync(rootPkg);
|
133 | pkgInfo = await readFileAsync(rootPkg);
|
134 | } catch (e) {}
|
135 | if (!pkgInfo) {
|
136 | await writeFileAsync(
|
137 | rootPkg,
|
138 | JSON.stringify(
|
139 | {
|
140 | name: 'feflow-home',
|
141 | version: '0.0.0',
|
142 | private: true
|
143 | },
|
144 | null,
|
145 | 2
|
146 | )
|
147 | );
|
148 | }
|
149 | }
|
150 |
|
151 | async initBinPath() {
|
152 | const { bin } = this;
|
153 | try {
|
154 | const stats = await statAsync(bin);
|
155 | if (!stats.isDirectory()) {
|
156 | await unlinkAsync(bin);
|
157 | }
|
158 | } catch (e) {
|
159 | await mkdirAsync(bin);
|
160 | }
|
161 | new Binp().register(bin);
|
162 | }
|
163 |
|
164 | initPackageManager() {
|
165 | const { root, logger } = this;
|
166 | return new Promise<any>((resolve, reject) => {
|
167 | if (!this.config?.packageManager) {
|
168 | const packageManagers = ['npm', 'tnpm', 'yarn', 'cnpm'];
|
169 | const defaultPackageManager = packageManagers.find(packageManager =>
|
170 | isInstalledPM(packageManager)
|
171 | );
|
172 | if (!defaultPackageManager) {
|
173 |
|
174 | logger.error('You must installed a package manager');
|
175 | return;
|
176 | } else {
|
177 | const configPath = path.join(root, '.feflowrc.yml');
|
178 | safeDump(
|
179 | Object.assign({}, parseYaml(configPath), {
|
180 | packageManager: defaultPackageManager
|
181 | }),
|
182 | configPath
|
183 | );
|
184 | this.config = parseYaml(configPath);
|
185 | }
|
186 | } else {
|
187 | logger.debug('Use packageManager is: ', this.config.packageManager);
|
188 | }
|
189 | resolve();
|
190 | });
|
191 | }
|
192 |
|
193 | loadNative() {
|
194 | return new Promise<any>((resolve, reject) => {
|
195 | const nativePath = path.join(__dirname, './native |
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 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
\ | No newline at end of file |