1 | const glob = require("glob");
|
2 | const _get = require("lodash/get");
|
3 | const main = require("../main");
|
4 | const parseTSConfig = require("./parse-ts-config");
|
5 | const parseBabelConfig = require("./parse-babel-config");
|
6 | const getResolveConfig = require("./get-resolve-config");
|
7 | const validateFileExistence = require("./utl/validate-file-existence");
|
8 | const normalizeOptions = require("./normalize-options");
|
9 | const initConfig = require("./init-config");
|
10 | const io = require("./utl/io");
|
11 | const formatMetaInfo = require("./format-meta-info");
|
12 |
|
13 | function extractResolveOptions(pCruiseOptions) {
|
14 | let lResolveOptions = {};
|
15 | const lWebPackConfigFileName = _get(
|
16 | pCruiseOptions,
|
17 | "ruleSet.options.webpackConfig.fileName",
|
18 | null
|
19 | );
|
20 |
|
21 | if (lWebPackConfigFileName) {
|
22 | lResolveOptions = getResolveConfig(
|
23 | lWebPackConfigFileName,
|
24 | _get(pCruiseOptions, "ruleSet.options.webpackConfig.env", null),
|
25 | _get(pCruiseOptions, "ruleSet.options.webpackConfig.arguments", null)
|
26 | );
|
27 | }
|
28 | return lResolveOptions;
|
29 | }
|
30 |
|
31 | function extractTSConfigOptions(pCruiseOptions) {
|
32 | let lReturnValue = {};
|
33 | const lTSConfigFileName = _get(
|
34 | pCruiseOptions,
|
35 | "ruleSet.options.tsConfig.fileName",
|
36 | null
|
37 | );
|
38 |
|
39 | if (lTSConfigFileName) {
|
40 | lReturnValue = parseTSConfig(lTSConfigFileName);
|
41 | }
|
42 |
|
43 | return lReturnValue;
|
44 | }
|
45 |
|
46 | function extractBabelConfigOptions(pCruiseOptions) {
|
47 | let lReturnValue = {};
|
48 | const lBabelConfigFileName = _get(
|
49 | pCruiseOptions,
|
50 | "ruleSet.options.babelConfig.fileName",
|
51 | null
|
52 | );
|
53 |
|
54 | if (lBabelConfigFileName) {
|
55 | lReturnValue = parseBabelConfig(lBabelConfigFileName);
|
56 | }
|
57 |
|
58 | return lReturnValue;
|
59 | }
|
60 |
|
61 | function runCruise(pFileDirectoryArray, pCruiseOptions) {
|
62 | pFileDirectoryArray
|
63 | .filter((pFileOrDirectory) => !glob.hasMagic(pFileOrDirectory))
|
64 | .forEach(validateFileExistence);
|
65 |
|
66 | pCruiseOptions = normalizeOptions(pCruiseOptions);
|
67 |
|
68 | const lReportingResult = main.futureCruise(
|
69 | pFileDirectoryArray,
|
70 | pCruiseOptions,
|
71 | extractResolveOptions(pCruiseOptions),
|
72 | {
|
73 | tsConfig: extractTSConfigOptions(pCruiseOptions),
|
74 | babelConfig: extractBabelConfigOptions(pCruiseOptions),
|
75 | }
|
76 | );
|
77 |
|
78 | io.write(pCruiseOptions.outputTo, lReportingResult.output);
|
79 |
|
80 | return lReportingResult.exitCode;
|
81 | }
|
82 |
|
83 | module.exports = (pFileDirectoryArray, pCruiseOptions) => {
|
84 | pCruiseOptions = pCruiseOptions || {};
|
85 | let lExitCode = 0;
|
86 |
|
87 | try {
|
88 | if (pCruiseOptions.info === true) {
|
89 | process.stdout.write(formatMetaInfo());
|
90 | } else if (pCruiseOptions.init) {
|
91 | initConfig(pCruiseOptions.init);
|
92 | } else {
|
93 | lExitCode = runCruise(pFileDirectoryArray, pCruiseOptions);
|
94 | }
|
95 | } catch (pError) {
|
96 | process.stderr.write(`\n ERROR: ${pError.message}\n`);
|
97 | lExitCode = 1;
|
98 | }
|
99 | return lExitCode;
|
100 | };
|