Skip to content

Commit

Permalink
feat(types): add webpack types schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvdutt committed Jul 13, 2018
1 parent c1844f8 commit 90909e4
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 59 deletions.
20 changes: 10 additions & 10 deletions packages/generators/add-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function replaceAt(str: string, index: number, replace: string): string {
*/
const traverseAndGetProperties = (arr: object[], prop: string): boolean => {
let hasProp: boolean = false;
arr.forEach((p: object) => {
arr.forEach((p: object): void => {
if (p[prop]) {
hasProp = true;
}
Expand All @@ -72,7 +72,7 @@ const traverseAndGetProperties = (arr: object[], prop: string): boolean => {
const searchProps = (answers: object, input: string): Promise<string[]> => {
input = input || "";
return Promise.resolve(
PROPS.filter((prop: string) =>
PROPS.filter((prop: string): boolean =>
prop.toLowerCase().includes(input.toLowerCase()),
),
);
Expand Down Expand Up @@ -207,15 +207,15 @@ export default class AddGenerator extends Generator {
const originalPropDesc: object = defOrPropDescription[0].enum;
// Array -> Object -> Merge objects into one for compat in manualOrListInput
defOrPropDescription = Object.keys(defOrPropDescription[0].enum)
.map((p: string) => {
.map((p: string): object => {
return Object.assign(
{},
{
[originalPropDesc[p]]: "noop",
},
);
})
.reduce((result: object, currentObject: object) => {
.reduce((result: object, currentObject: object): object => {
for (const key in currentObject) {
if (currentObject.hasOwnProperty(key)) {
result[key] = currentObject[key];
Expand Down Expand Up @@ -323,14 +323,14 @@ export default class AddGenerator extends Generator {
"node_modules/webpack/lib/*Plugin.js",
"node_modules/webpack/lib/**/*Plugin.js",
])
.map((p: string) =>
.map((p: string): string =>
p
.split("/")
.pop()
.replace(".js", ""),
)
.find(
(p: string) => p.toLowerCase().indexOf(answerToAction.actionAnswer) >= 0,
(p: string): boolean => p.toLowerCase().indexOf(answerToAction.actionAnswer) >= 0,
);

if (pluginExist) {
Expand All @@ -341,7 +341,7 @@ export default class AddGenerator extends Generator {
"node_modules/webpack/schemas/plugins/**/*Plugin.json",
])
.find(
(p: string) =>
(p: string): boolean =>
p
.split("/")
.pop()
Expand All @@ -361,9 +361,9 @@ export default class AddGenerator extends Generator {
let pluginsSchemaProps: string[] = ["other"];
if (pluginSchema) {
Object.keys(pluginSchema)
.filter((p: string) => Array.isArray(pluginSchema[p]))
.forEach((p: string) => {
Object.keys(pluginSchema[p]).forEach((n: string) => {
.filter((p: string): boolean => Array.isArray(pluginSchema[p]))
.forEach((p: string): void => {
Object.keys(pluginSchema[p]).forEach((n: string): void => {
if (pluginSchema[p][n].properties) {
pluginsSchemaProps = Object.keys(
pluginSchema[p][n].properties,
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/addon-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default function addonGenerator(
public install() {
this.npmInstall(["webpack-defaults", "bluebird"], {
"save-dev": true,
}).then((_: void) => {
}).then((_: void): void => {
this.spawnCommand("npm", ["run", "defaults"]);
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/generators/loader-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const LoaderGenerator = addonGenerator(
message: "Loader name",
name: "name",
type: "input",
validate: (str: string) => str.length > 0,
validate: (str: string): boolean => str.length > 0,
},
],
path.resolve(__dirname, "..", "generate-loader"),
Expand All @@ -51,7 +51,7 @@ const LoaderGenerator = addonGenerator(
"examples/simple/src/static-esm-module.js.tpl",
],
["src/_index.js.tpl"],
(gen: IYeoman) => ({ name: gen.props.name }),
(gen: IYeoman): object => ({ name: gen.props.name }),
);

export default LoaderGenerator;
4 changes: 2 additions & 2 deletions packages/generators/plugin-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const PluginGenerator = addonGenerator(
message: "Plugin name",
name: "name",
type: "input",
validate: (str: string) => str.length > 0,
validate: (str: string): boolean => str.length > 0,
},
],
path.resolve(__dirname, "..", "generate-plugin"),
Expand All @@ -32,7 +32,7 @@ const PluginGenerator = addonGenerator(
"examples/simple/src/static-esm-module.js.tpl",
],
["src/_index.js.tpl", "examples/simple/_webpack.config.js.tpl"],
(gen: IYeoman) => ({ name: _.upperFirst(_.camelCase(gen.props.name)) }),
(gen: IYeoman): object => ({ name: _.upperFirst(_.camelCase(gen.props.name)) }),
);

export default PluginGenerator;
14 changes: 8 additions & 6 deletions packages/generators/remove-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export default class RemoveGenerator extends Generator {
public getModuleLoadersNames(): string[] {
if (typeof this.webpackOptions === "object") {
if (this.webpackOptions.module && this.webpackOptions.module.rules) {
return this.webpackOptions.module.rules.map((rule: any) => rule ? rule.loader : null);
return this.webpackOptions.module.rules.map((rule: {
loader: string;
}) => rule ? rule.loader : null);
}
}
}
Expand All @@ -65,7 +67,7 @@ export default class RemoveGenerator extends Generator {
Array.from(this.getPropTypes()),
),
])
.then(({ propType }: { propType: string }) => {
.then(({ propType }: { propType: string }): Promise<{}> => {
if (!PROP_TYPES.has(propType)) {
console.log("Invalid webpack config prop");
return;
Expand All @@ -80,7 +82,7 @@ export default class RemoveGenerator extends Generator {
`Which key do you want to remove from ${propType}?`,
Array.from(propValue),
),
]).then(({ keyType }: { keyType: string }) => {
]).then(({ keyType }: { keyType: string }): void => {
this.configuration.config.webpackOptions[propType] = [ keyType ];
});
} else {
Expand All @@ -91,7 +93,7 @@ export default class RemoveGenerator extends Generator {
Array.from(Object.keys(propValue)),
),
])
.then(({ keyType }: { keyType: string }) => {
.then(({ keyType }: { keyType: string }): Promise<{}> => {
if (propType === "module" && keyType === "rules") {
return this.prompt([
List(
Expand All @@ -100,7 +102,7 @@ export default class RemoveGenerator extends Generator {
Array.from(this.getModuleLoadersNames()),
),
])
.then(({ rule }: { rule: string }) => {
.then(({ rule }: { rule: string }): void => {
if (typeof this.webpackOptions === "object") {
const loaderIndex: number = this.getModuleLoadersNames().indexOf(rule);
const loader: object = this.webpackOptions.module.rules[loaderIndex];
Expand All @@ -125,7 +127,7 @@ export default class RemoveGenerator extends Generator {
this.configuration.config.webpackOptions[propType] = null;
}
})
.then((_: void) => {
.then((_: void): void => {
done();
});
}
Expand Down
Loading

0 comments on commit 90909e4

Please sign in to comment.