-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce51a0a
commit d901d49
Showing
3 changed files
with
145 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import PROP_TYPES from "@webpack-cli/utils/prop-types"; | ||
|
||
const PROPS: string[] = Array.from(PROP_TYPES.keys()); | ||
|
||
// tslint:disable:no-var-requires | ||
export const webpackDevServerSchema = require("webpack-dev-server/lib/options.json"); | ||
export const webpackSchema = require("./utils/optionsSchema.json"); | ||
|
||
/** | ||
* | ||
* Replaces the string with a substring at the given index | ||
* https://gist.github.com/efenacigiray/9367920 | ||
* | ||
* @param {String} string - 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 function replaceAt(str: string, index: number, replace: string): string { | ||
return str.substring(0, index) + replace + str.substring(index + 1); | ||
} | ||
|
||
/** | ||
* | ||
* Checks if the given array has a given property | ||
* | ||
* @param {Array} arr - array to check | ||
* @param {String} prop - property to check existence of | ||
* | ||
* @returns {Boolean} hasProp - Boolean indicating if the property | ||
* is present | ||
*/ | ||
export const traverseAndGetProperties = (arr: object[], prop: string): boolean => { | ||
let hasProp: boolean = false; | ||
arr.forEach((p: object): void => { | ||
if (p[prop]) { | ||
hasProp = true; | ||
} | ||
}); | ||
return hasProp; | ||
}; | ||
|
||
/** | ||
* | ||
* Search config properties | ||
* | ||
* @param {Object} answers Prompt answers object | ||
* @param {String} input Input search string | ||
* | ||
* @returns {Promise} Returns promise which resolves to filtered props | ||
* | ||
*/ | ||
export const searchProps = (answers: object, input: string): Promise<string[]> => { | ||
input = input || ""; | ||
return Promise.resolve( | ||
PROPS.filter((prop: string): boolean => | ||
prop.toLowerCase().includes(input.toLowerCase()), | ||
), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
AutoComplete, | ||
Confirm, | ||
IInquirerInput, | ||
Input, | ||
InputValidate, | ||
List, | ||
} from "@webpack-cli/webpack-scaffold"; | ||
import { existsSync } from "fs"; | ||
import { resolve } from "path"; | ||
import { | ||
searchProps, | ||
} from "../index"; | ||
|
||
/** | ||
* Returns Inquirer question for given action | ||
* @param action string | ||
*/ | ||
export const manualOrListInput: (action: string) => IInquirerInput = (action: string) => { | ||
const actionQuestion = `What do you want to add to ${action}?`; | ||
return Input("actionAnswer", actionQuestion); | ||
}; | ||
|
||
export const actionTypeQuestion = AutoComplete( | ||
"actionType", | ||
"What property do you want to add to?", | ||
{ | ||
pageSize: 7, | ||
source: searchProps, | ||
suggestOnly: false, | ||
}, | ||
); | ||
|
||
export const entryTypeQuestion = Confirm( | ||
"entryType", | ||
"Will your application have multiple bundles?", | ||
false, | ||
); | ||
|
||
export const topScopeQuestion = Input( | ||
"topScope", | ||
"What do you want to add to topScope?", | ||
); | ||
|
||
const mergeFileQuestionFunction = () => { | ||
const question = "What is the location of webpack configuration with which you want to merge current configuration?"; | ||
const validator = (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 InputValidate("mergeFile", question, validator); | ||
}; | ||
export const mergeFileQuestion = mergeFileQuestionFunction(); |