Skip to content

Commit

Permalink
fix(add): add handling of merge option
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 committed Mar 20, 2019
1 parent 473ff4a commit eb43443
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
49 changes: 40 additions & 9 deletions packages/generators/add-generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import Generator = require("yeoman-generator");

import * as glob from "glob-all";
import * as autoComplete from "inquirer-autocomplete-prompt";
import * as path from "path";

import npmExists from "@webpack-cli/utils/npm-exists";
import { getPackageManager } from "@webpack-cli/utils/package-manager";
import PROP_TYPES from "@webpack-cli/utils/prop-types";
Expand All @@ -12,11 +6,17 @@ import {
Confirm,
IInquirerInput,
Input,
InputValidate,
List,
} from "@webpack-cli/webpack-scaffold";

import { existsSync } from "fs";
import * as glob from "glob-all";
import * as autoComplete from "inquirer-autocomplete-prompt";
import { resolve } from "path";
import Generator = require("yeoman-generator");
import { ISchemaProperties, IWebpackOptions } from "./types";
import entryQuestions from "./utils/entry";
import validate from "./utils/validate";

// tslint:disable:no-var-requires
const webpackDevServerSchema = require("webpack-dev-server/lib/options.json");
Expand Down Expand Up @@ -93,7 +93,8 @@ export default class AddGenerator extends Generator {
config: {
configName?: string,
topScope?: string[],
item?: string;
item?: string,
merge?: object,
webpackOptions?: IWebpackOptions,
},
};
Expand Down Expand Up @@ -172,6 +173,36 @@ export default class AddGenerator extends Generator {
done();
});
}
if (action === "merge") {
const validatePath = (path: string) => {
const resolvedPath = resolve(process.env.PWD, path);
if (existsSync(resolvedPath)) {
if (/\.js$/.test(path)) {
if (typeof require(resolvedPath) !== "object") {
return "Given file doesn't export an Object";
}
return true;
} else {
return "Path doesn't corresponds to a javascript file";
}
}
return "Invalid path provided";
};
return this.prompt([
InputValidate(
"mergeFile",
"What is the location of webpack configuration file with which you want to merge current configuration?",
validatePath,
),
])
.then((mergeFileAnswer: {
mergeFile: string;
}) => {
const resolvedPath = resolve(process.env.PWD, mergeFileAnswer.mergeFile);
const mergeConfig = require(resolvedPath);
this.configuration.config.merge = mergeConfig;
});
}
}
const temp = action;
if (action === "resolveLoader") {
Expand Down Expand Up @@ -366,7 +397,7 @@ export default class AddGenerator extends Generator {
pluginsSchemaPath.indexOf("optimize") >= 0
? "webpack.optimize"
: "webpack";
const resolvePluginsPath: string = path.resolve(pluginsSchemaPath);
const resolvePluginsPath: string = resolve(pluginsSchemaPath);
const pluginSchema: object = resolvePluginsPath
? require(resolvePluginsPath)
: null;
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function runTransform(transformConfig: ITransformConfig, action:
transformations.push("topScope");
}

if (config.merge) {
if (config.merge && transformations.indexOf("merge") === -1) {
transformations.push("merge");
}

Expand Down

0 comments on commit eb43443

Please sign in to comment.