Skip to content

Commit

Permalink
ast(parser): remove
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvdutt committed Jun 30, 2018
1 parent 9db4842 commit faeec57
Show file tree
Hide file tree
Showing 10 changed files with 675 additions and 51 deletions.
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"init",
"migrate",
"add",
/*
"remove",
/*
"update",
"make",
*/
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 120 additions & 1 deletion packages/generators/remove-generator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,122 @@
const Generator = require("yeoman-generator");
const path = require("path");
const fs = require("fs");
const { List } = require("@webpack-cli/webpack-scaffold");

module.exports = class RemoveGenerator extends Generator {};
const PROP_TYPES = require("@webpack-cli/utils/prop-types");

/**
*
* Generator for removing properties
* @class RemoveGenerator
* @extends Generator
* @returns {Void} After execution, transforms are triggered
*
*/

module.exports = class RemoveGenerator extends Generator {
constructor(args, opts) {
super(args, opts);
this.configuration = {
config: {
webpackOptions: {},
}
};

let configPath = path.resolve(process.cwd(), "webpack.config.js");
const webpackConfigExists = fs.existsSync(configPath);
if (!webpackConfigExists) {
configPath = null;
// end the generator stating webpack config not found or to specify the config
}
this.webpackOptions = require(configPath);
}

getPropTypes() {
return Object.keys(this.webpackOptions);
}

getModuleLoaders() {
if (this.webpackOptions.module && this.webpackOptions.module.rules) {
return this.webpackOptions.module.rules.map(rule => rule ? rule.loader : null);
}
}

prompting() {
const done = this.async();
let propValue;

return this.prompt([
List(
"propType",
"Which property do you want to remove?",
Array.from(this.getPropTypes())
)
])
.then(({ propType }) => {
if (!PROP_TYPES.has(propType)) {
console.log("Invalid webpack config prop");
return;
}

propValue = this.webpackOptions[propType];
if (typeof propValue === "object") {
if (Array.isArray(propValue)) {
return this.prompt([
List(
"keyType",
`Which key do you want to remove from ${propType}?`,
Array.from(propValue)
)
]).then(({ keyType }) => {
this.configuration.config.webpackOptions[propType] = [ keyType ];
});
} else {
return this.prompt([
List(
"keyType",
`Which key do you want to remove from ${propType}?`,
Array.from(Object.keys(propValue))
)
])
.then(({ keyType }) => {
if (propType === "module" && keyType === "rules") {
return this.prompt([
List(
"rule",
"Which loader do you want to remove?",
Array.from(this.getModuleLoaders())
)
])
.then(({ rule }) => {
const loaderIndex = this.getModuleLoaders().indexOf(rule);
const loader = this.webpackOptions.module.rules[loaderIndex];
this.configuration.config.webpackOptions.module = {
rules: [ loader ]
};
});
} else {
// remove the complete prop object if there is only one key
if (Object.keys(this.webpackOptions[propType]).length <= 1) {
this.configuration.config.webpackOptions[propType] = null;
} else {
this.configuration.config.webpackOptions[propType] = {
[keyType]: null
};
}
}
});
}
} else {
this.configuration.config.webpackOptions[propType] = null;
}
})
.then(() => {
done();
});
}

writing() {
this.config.set("configuration", this.configuration);
}
};
3 changes: 2 additions & 1 deletion packages/info/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as envinfo from "envinfo";
import * as process from "process";

/**
* Prints debugging information for webpack issue reporting
*/

export default async function info() {
console.log(
process.stdout.write(
await envinfo.run({
Binaries: ["Node", "Yarn", "npm"],
Browsers: ["Chrome", "Firefox", "Safari"],
Expand Down
Loading

0 comments on commit faeec57

Please sign in to comment.