Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anikethsaha committed May 27, 2019
1 parent b7328c6 commit 7d83453
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
14 changes: 14 additions & 0 deletions packages/generators/__tests__/add-generator.test.ts
@@ -0,0 +1,14 @@
import { generatePluginName } from "../utils/plugins"

describe("generatePluginName", () => {
it("should return webpack Standard Plugin Name for Name : extract-text-webpack-plugin", () => {
const pluginName = generatePluginName("extract-text-webpack-plugin");
expect(pluginName).toEqual("ExtractTextWebpackPlugin");
});

it("should return webpack Standard Plugin Name for Name : webpack.DefinePlugin", () => {
const pluginName = generatePluginName("webpack.DefinePlugin");
expect(pluginName).toEqual("Webpack.DefinePlugin");
});
});

23 changes: 2 additions & 21 deletions packages/generators/add-generator.ts
Expand Up @@ -11,26 +11,11 @@ import { AutoComplete, Confirm, Input, List } from "@webpack-cli/webpack-scaffol

import { SchemaProperties, WebpackOptions } from "./types";
import entryQuestions from "./utils/entry";

import { generatePluginName } from "./utils/plugins";
import webpackDevServerSchema from "webpack-dev-server/lib/options.json";
import webpackSchema from "./utils/optionsSchema.json";
const PROPS: string[] = Array.from(PROP_TYPES.keys());

/**
*
* Replaces the string with a substring at the given index
* https://gist.github.com/efenacigiray/9367920
*
* @param {String} str - string to be modified
* @param {Number} index - index to replace from
* @param {String} replace - string to replace starting from index
*
* @returns {String} string - The newly mutated string
*
*/
function replaceAt(str: string, index: number, replace: string): string {
return str.substring(0, index) + replace + str.substring(index + 1);
}

/**
*
Expand Down Expand Up @@ -395,11 +380,7 @@ export default class AddGenerator extends Generator {
(p: boolean): void => {
if (p) {
this.dependencies.push(answerToAction.actionAnswer);
let myPluginNameArray = answerToAction.actionAnswer.split("-")
for (let i = 0; i < myPluginNameArray.length; i++) {
myPluginNameArray[i] = replaceAt(myPluginNameArray[i], 0, myPluginNameArray[i].charAt(0).toUpperCase());
}
const pluginName = myPluginNameArray.join("")
const pluginName = generatePluginName(answerToAction.actionAnswer)
this.configuration.config.topScope.push(
`const ${pluginName} = require("${answerToAction.actionAnswer}")`
);
Expand Down
41 changes: 40 additions & 1 deletion packages/generators/utils/plugins.ts
@@ -1,3 +1,4 @@

/**
*
* Callable function with the initial plugins
Expand All @@ -6,6 +7,44 @@
* that consists of terser-webpack-plugin
*/

export default function(): string[] {
export default function (): string[] {
return ["new TerserPlugin()"];
}

/**
*
* Replaces the string with a substring at the given index
* https://gist.github.com/efenacigiray/9367920
*
* @param {String} str - string to be modified
* @param {Number} index - index to replace from
* @param {String} replace - string to replace starting from index
*
* @returns {String} string - The newly mutated string
*
*/

export const replaceAt = (str: string, index: number, replace: string) : string => {
return str.substring(0, index) + replace + str.substring(index + 1);
}


/**
*
* Generate a webpack standard webpack plugin name from the plugin name from the Answer
*
* @param {String} rawPluginName - plugin name from answer
*
* @returns {String} string - the webpack standard plugin name
*
*/


export const generatePluginName = (rawPluginName: string): string => {
let myPluginNameArray : string[];
myPluginNameArray = rawPluginName.split("-");
for (let i = 0; i < myPluginNameArray.length; i++) {
myPluginNameArray[i] = replaceAt(myPluginNameArray[i], 0, myPluginNameArray[i].charAt(0).toUpperCase());
}
return myPluginNameArray.join("")
}

0 comments on commit 7d83453

Please sign in to comment.