-
-
Notifications
You must be signed in to change notification settings - Fork 601
/
modify-config-helper.ts
136 lines (125 loc) · 3.67 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
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 Generator = require("yeoman-generator");
import runTransform from "./scaffold";
import { IGenerator, IYeoman } from "./types/Yeoman";
export interface IConfig extends Object {
item?: {
name: string;
};
topScope?: string[];
configName?: string;
merge: object;
webpackOptions: object;
}
export interface ITransformConfig extends Object {
configPath?: string;
configFile?: string;
config?: IConfig;
}
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: IGenerator,
configFile: string = DEFAULT_WEBPACK_CONFIG_FILENAME,
packages?: string[])
: Function {
let configPath: string | null = null;
if (action !== "init") {
configPath = path.resolve(process.cwd(), configFile);
const webpackConfigExists: boolean = fs.existsSync(configPath);
if (webpackConfigExists) {
process.stdout.write(
"\n" +
logSymbols.success +
chalk.green(" SUCCESS ") +
"Found config " +
chalk.cyan(configFile + "\n") +
"\n",
);
} else {
process.stdout.write(
"\n" +
logSymbols.error +
chalk.red(" ERROR ") +
chalk.cyan(configFile) +
" not found. Please specify a valid path to your webpack config like " +
chalk.white("$ ") +
chalk.cyan(`webpack-cli ${action} webpack.dev.js`) +
"\n",
);
return;
}
}
const env = yeoman.createEnv("webpack", null);
const generatorName: string = `webpack-${action}-generator`;
if (!generator) {
generator = class extends Generator {
public initializing() {
packages.forEach((pkgPath: string) => {
return (this as IGenerator).composeWith(require.resolve(pkgPath));
});
}
};
}
env.registerStub(generator, generatorName);
env.run(generatorName).then((_: void) => {
let configModule: object;
try {
const confPath: string = 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: string[] = 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: ITransformConfig = Object.assign(
{
configFile: !configPath ? null : fs.readFileSync(configPath, "utf8"),
configPath,
},
configModule,
);
return runTransform(transformConfig, action);
}).catch((err) => {
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);
});
}