Skip to content

Commit 7d83453

Browse files
committedMay 27, 2019
chore: added a generatePluginName method in generators utils
Added generatePluginName in generators/utils/plugins.ts and also moved the replaceAt method from add-generator.ts to generators/utils/plugins.ts. Added tests for the plugin name method
1 parent b7328c6 commit 7d83453

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { generatePluginName } from "../utils/plugins"
2+
3+
describe("generatePluginName", () => {
4+
it("should return webpack Standard Plugin Name for Name : extract-text-webpack-plugin", () => {
5+
const pluginName = generatePluginName("extract-text-webpack-plugin");
6+
expect(pluginName).toEqual("ExtractTextWebpackPlugin");
7+
});
8+
9+
it("should return webpack Standard Plugin Name for Name : webpack.DefinePlugin", () => {
10+
const pluginName = generatePluginName("webpack.DefinePlugin");
11+
expect(pluginName).toEqual("Webpack.DefinePlugin");
12+
});
13+
});
14+

‎packages/generators/add-generator.ts

+2-21
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,11 @@ import { AutoComplete, Confirm, Input, List } from "@webpack-cli/webpack-scaffol
1111

1212
import { SchemaProperties, WebpackOptions } from "./types";
1313
import entryQuestions from "./utils/entry";
14-
14+
import { generatePluginName } from "./utils/plugins";
1515
import webpackDevServerSchema from "webpack-dev-server/lib/options.json";
1616
import webpackSchema from "./utils/optionsSchema.json";
1717
const PROPS: string[] = Array.from(PROP_TYPES.keys());
1818

19-
/**
20-
*
21-
* Replaces the string with a substring at the given index
22-
* https://gist.github.com/efenacigiray/9367920
23-
*
24-
* @param {String} str - string to be modified
25-
* @param {Number} index - index to replace from
26-
* @param {String} replace - string to replace starting from index
27-
*
28-
* @returns {String} string - The newly mutated string
29-
*
30-
*/
31-
function replaceAt(str: string, index: number, replace: string): string {
32-
return str.substring(0, index) + replace + str.substring(index + 1);
33-
}
3419

3520
/**
3621
*
@@ -395,11 +380,7 @@ export default class AddGenerator extends Generator {
395380
(p: boolean): void => {
396381
if (p) {
397382
this.dependencies.push(answerToAction.actionAnswer);
398-
let myPluginNameArray = answerToAction.actionAnswer.split("-")
399-
for (let i = 0; i < myPluginNameArray.length; i++) {
400-
myPluginNameArray[i] = replaceAt(myPluginNameArray[i], 0, myPluginNameArray[i].charAt(0).toUpperCase());
401-
}
402-
const pluginName = myPluginNameArray.join("")
383+
const pluginName = generatePluginName(answerToAction.actionAnswer)
403384
this.configuration.config.topScope.push(
404385
`const ${pluginName} = require("${answerToAction.actionAnswer}")`
405386
);

‎packages/generators/utils/plugins.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/**
23
*
34
* Callable function with the initial plugins
@@ -6,6 +7,44 @@
67
* that consists of terser-webpack-plugin
78
*/
89

9-
export default function(): string[] {
10+
export default function (): string[] {
1011
return ["new TerserPlugin()"];
1112
}
13+
14+
/**
15+
*
16+
* Replaces the string with a substring at the given index
17+
* https://gist.github.com/efenacigiray/9367920
18+
*
19+
* @param {String} str - string to be modified
20+
* @param {Number} index - index to replace from
21+
* @param {String} replace - string to replace starting from index
22+
*
23+
* @returns {String} string - The newly mutated string
24+
*
25+
*/
26+
27+
export const replaceAt = (str: string, index: number, replace: string) : string => {
28+
return str.substring(0, index) + replace + str.substring(index + 1);
29+
}
30+
31+
32+
/**
33+
*
34+
* Generate a webpack standard webpack plugin name from the plugin name from the Answer
35+
*
36+
* @param {String} rawPluginName - plugin name from answer
37+
*
38+
* @returns {String} string - the webpack standard plugin name
39+
*
40+
*/
41+
42+
43+
export const generatePluginName = (rawPluginName: string): string => {
44+
let myPluginNameArray : string[];
45+
myPluginNameArray = rawPluginName.split("-");
46+
for (let i = 0; i < myPluginNameArray.length; i++) {
47+
myPluginNameArray[i] = replaceAt(myPluginNameArray[i], 0, myPluginNameArray[i].charAt(0).toUpperCase());
48+
}
49+
return myPluginNameArray.join("")
50+
}

0 commit comments

Comments
 (0)
Please sign in to comment.