/
modify-config-helper.ts
144 lines (134 loc) · 3.74 KB
/
modify-config-helper.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
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
import chalk from "chalk";
import * as fs from "fs";
import * as logSymbols from "log-symbols";
import * as path from "path";
import * as yeoman from "yeoman-environment";
import * as Generator from "yeoman-generator";
import runTransform from "./scaffold";
export interface Config extends Object {
item?: {
name: string;
};
topScope?: string[];
configName?: string;
merge: object;
webpackOptions: object;
}
export interface TransformConfig extends Object {
configPath?: string;
configFile?: string;
config?: Config;
}
const DEFAULT_WEBPACK_CONFIG_FILENAME = "webpack.config.js";
/**
*
* Looks up the webpack.config in the user's path and runs a given
* generator scaffold followed up by a transform
*
* @param {String} action — action to be done (add, remove, update, init)
* @param {Class} generator - Yeoman generator class
* @param {String} configFile - Name of the existing/default webpack configuration file
* @param {Array} packages - List of packages to resolve
* @returns {Function} runTransform - Returns a transformation instance
*/
export default function modifyHelperUtil(
action: string,
generator: typeof Generator,
configFile: string = DEFAULT_WEBPACK_CONFIG_FILENAME,
packages?: string[]
): typeof Generator {
let configPath: string | null = null;
if (action !== "init") {
configPath = path.resolve(process.cwd(), configFile);
const webpackConfigExists: boolean = fs.existsSync(configPath);
let outputMessage =
"\n" +
logSymbols.error +
chalk.red(" ERROR ") +
chalk.cyan(configFile) +
" not found. Please specify a valid path to your webpack config like \n " +
chalk.white("$ ") +
chalk.cyan(`webpack-cli ${action} webpack.dev.js`) +
"\n";
if (webpackConfigExists) {
outputMessage =
"\n" +
logSymbols.success +
chalk.green(" SUCCESS ") +
"Found config " +
chalk.cyan(configFile + "\n") +
"\n";
}
process.stdout.write(outputMessage);
return;
}
const env = yeoman.createEnv("webpack", null);
const generatorName = `webpack-${action}-generator`;
if (!generator) {
generator = class extends Generator {
public initializing(): void {
packages.forEach(
(pkgPath: string): Generator => {
return this.composeWith(require.resolve(pkgPath), {});
}
);
}
};
}
env.registerStub(generator, generatorName);
env.run(generatorName, {
configFile
})
.then(
(): void => {
let configModule: object;
try {
const confPath = path.resolve(process.cwd(), ".yo-rc.json");
configModule = require(confPath);
// Change structure of the config to be transformed
const tmpConfig: object = {};
Object.keys(configModule).forEach(
(prop: string): void => {
const configs = Object.keys(configModule[prop].configuration);
configs.forEach(
(conf: string): void => {
tmpConfig[conf] = configModule[prop].configuration[conf];
}
);
}
);
configModule = tmpConfig;
} catch (err) {
console.error(chalk.red("\nCould not find a yeoman configuration file.\n"));
console.error(
chalk.red(
"\nPlease make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n"
)
);
Error.stackTraceLimit = 0;
process.exitCode = -1;
}
const transformConfig: TransformConfig = Object.assign(
{
configFile: !configPath ? null : fs.readFileSync(configPath, "utf8"),
configPath
},
configModule
);
return runTransform(transformConfig, action);
}
)
.catch(
(err): void => {
console.error(
chalk.red(
`
Unexpected Error
please file an issue here https://github.com/webpack/webpack-cli/issues/new?template=Bug_report.md
`
)
);
console.error(err);
}
);
}