-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathinit.ts
82 lines (69 loc) · 2.91 KB
/
init.ts
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
import chalk from "chalk";
import * as j from "jscodeshift";
import pEachSeries = require("p-each-series");
import * as path from "path";
import propTypes from "@webpack-cli/utils/prop-types";
import astTransform from "@webpack-cli/utils/recursive-parser";
import runPrettier from "@webpack-cli/utils/run-prettier";
import { Node } from "@webpack-cli/utils/types";
import { Error } from "./types";
import { Configuration, WebpackProperties } from "./types/Transform";
/**
*
* Maps back transforms that needs to be run using the configuration
* provided.
*
* @param {Object} transformObject - An Object with all transformations
* @param {Object} config - Configuration to transform
* @returns {Array} - An array with the transformations to be run
*/
const mapOptionsToTransform = (config: Configuration): string[] =>
Object.keys(config.webpackOptions).filter((key: string): boolean => propTypes.has(key));
/**
*
* Runs the transformations from an object we get from yeoman
*
* @param {Object} webpackProperties - Configuration to transform
* @param {String} action - Action to be done on the given ast
* @returns {Promise} - A promise that writes each transform, runs prettier
* and writes the file
*/
export default function runTransform(webpackProperties: WebpackProperties, action: string): void {
// webpackOptions.name sent to nameTransform if match
const webpackConfig: string[] = Object.keys(webpackProperties).filter(
(p: string): boolean => p !== "configFile" && p !== "configPath"
);
const initActionNotDefined = (action && action !== "init") || false;
webpackConfig.forEach(
(scaffoldPiece: string): Promise<void> => {
const config: Configuration = webpackProperties[scaffoldPiece];
const transformations = mapOptionsToTransform(config);
const ast = j(initActionNotDefined ? webpackProperties.configFile : "module.exports = {}");
const transformAction: string | null = action || null;
return pEachSeries(transformations, (f: string): boolean | Node => {
return astTransform(j, ast, config.webpackOptions[f], transformAction, f);
})
.then((): void | PromiseLike<void> => {
let configurationName = "webpack.config.js";
if (config.configName) {
configurationName = `webpack.${config.configName}.js`;
}
const outputPath = initActionNotDefined
? webpackProperties.configPath
: path.join(process.cwd(), configurationName);
const source: string = ast.toSource({
quote: "single"
});
runPrettier(outputPath, source);
})
.catch((err: Error): void => {
console.error(err.message ? err.message : err);
});
}
);
let successMessage = `Congratulations! Your new webpack configuration file has been created!`;
if (initActionNotDefined && webpackProperties.config.item) {
successMessage = `Congratulations! ${webpackProperties.config.item} has been ${action}ed!`;
}
process.stdout.write("\n" + chalk.green(`${successMessage}\n`));
}