Skip to content

Commit ce51a0a

Browse files
committedMar 20, 2019
fix(add): add handling of merge option
1 parent 42ecb48 commit ce51a0a

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed
 

‎packages/generators/add-generator.ts

+40-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import Generator = require("yeoman-generator");
2-
3-
import * as glob from "glob-all";
4-
import * as autoComplete from "inquirer-autocomplete-prompt";
5-
import * as path from "path";
6-
71
import npmExists from "@webpack-cli/utils/npm-exists";
82
import { getPackageManager } from "@webpack-cli/utils/package-manager";
93
import PROP_TYPES from "@webpack-cli/utils/prop-types";
104
import {
115
AutoComplete,
126
Confirm,
137
IInquirerInput,
14-
IInquirerList,
158
Input,
9+
InputValidate,
1610
List,
1711
} from "@webpack-cli/webpack-scaffold";
18-
12+
import { existsSync } from "fs";
13+
import * as glob from "glob-all";
14+
import * as autoComplete from "inquirer-autocomplete-prompt";
15+
import { resolve } from "path";
16+
import Generator = require("yeoman-generator");
1917
import { ISchemaProperties, IWebpackOptions } from "./types";
2018
import entryQuestions from "./utils/entry";
19+
import validate from "./utils/validate";
2120

2221
// tslint:disable:no-var-requires
2322
const webpackDevServerSchema = require("webpack-dev-server/lib/options.json");
@@ -94,7 +93,8 @@ export default class AddGenerator extends Generator {
9493
config: {
9594
configName?: string,
9695
topScope?: string[],
97-
item?: string;
96+
item?: string,
97+
merge?: object,
9898
webpackOptions?: IWebpackOptions,
9999
},
100100
};
@@ -173,6 +173,36 @@ export default class AddGenerator extends Generator {
173173
done();
174174
});
175175
}
176+
if (action === "merge") {
177+
const validatePath = (path: string) => {
178+
const resolvedPath = resolve(process.env.PWD, path);
179+
if (existsSync(resolvedPath)) {
180+
if (/\.js$/.test(path)) {
181+
if (typeof require(resolvedPath) !== "object") {
182+
return "Given file doesn't export an Object";
183+
}
184+
return true;
185+
} else {
186+
return "Path doesn't corresponds to a javascript file";
187+
}
188+
}
189+
return "Invalid path provided";
190+
};
191+
return this.prompt([
192+
InputValidate(
193+
"mergeFile",
194+
"What is the location of webpack configuration file with which you want to merge current configuration?",
195+
validatePath,
196+
),
197+
])
198+
.then((mergeFileAnswer: {
199+
mergeFile: string;
200+
}) => {
201+
const resolvedPath = resolve(process.env.PWD, mergeFileAnswer.mergeFile);
202+
const mergeConfig = require(resolvedPath);
203+
this.configuration.config.merge = mergeConfig;
204+
});
205+
}
176206
}
177207
const temp: string = action;
178208
if (action === "resolveLoader") {
@@ -367,7 +397,7 @@ export default class AddGenerator extends Generator {
367397
pluginsSchemaPath.indexOf("optimize") >= 0
368398
? "webpack.optimize"
369399
: "webpack";
370-
const resolvePluginsPath: string = path.resolve(pluginsSchemaPath);
400+
const resolvePluginsPath: string = resolve(pluginsSchemaPath);
371401
const pluginSchema: object = resolvePluginsPath
372402
? require(resolvePluginsPath)
373403
: null;

‎packages/utils/scaffold.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default function runTransform(transformConfig: ITransformConfig, action:
4848
transformations.push("topScope");
4949
}
5050

51-
if (config.merge) {
51+
if (config.merge && transformations.indexOf("merge") === -1) {
5252
transformations.push("merge");
5353
}
5454

0 commit comments

Comments
 (0)
Please sign in to comment.