|
| 1 | +import * as jscodeshift from "jscodeshift"; |
| 2 | + |
| 3 | +interface IScaffoldBaseObject { |
| 4 | + type: string; |
| 5 | + name: string; |
| 6 | + message: string; |
| 7 | +} |
| 8 | + |
| 9 | +interface IInquirerList extends IScaffoldBaseObject { |
| 10 | + choices?: string[]; |
| 11 | +} |
| 12 | + |
| 13 | +interface IInquirerInput extends IScaffoldBaseObject { |
| 14 | + validate?: Function; |
| 15 | +} |
| 16 | + |
| 17 | +export function createArrowFunction(value: Function): string { |
| 18 | + return `() => '${value}'`; |
| 19 | +} |
| 20 | + |
| 21 | +export function createRegularFunction(value: Function): string { |
| 22 | + return `function () {\n return '${value}'\n}`; |
| 23 | +} |
| 24 | + |
| 25 | +export function createDynamicPromise(arrOrString: Function[] | string): string { |
| 26 | + if (Array.isArray(arrOrString)) { |
| 27 | + return ( |
| 28 | + "() => new Promise((resolve) => resolve([" + |
| 29 | + arrOrString.map((func: Function) => { |
| 30 | + return "'" + func + "'"; |
| 31 | + }) + |
| 32 | + "]))" |
| 33 | + ); |
| 34 | + } else { |
| 35 | + return "() => new Promise((resolve) => resolve(" + "'" + arrOrString + "'" + "))"; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export function createAssetFilterFunction(value: string): string { |
| 40 | + return `function (assetFilename) {\n return assetFilename.endsWith('.${value}');\n}`; |
| 41 | +} |
| 42 | + |
| 43 | +export function createExternalFunction(regexp: string): string { |
| 44 | + return ( |
| 45 | + "\n function (context, request, callback) {\n if (" + |
| 46 | + "/" + |
| 47 | + regexp + |
| 48 | + "/.test(request)){" + |
| 49 | + "\n" + |
| 50 | + " return callback(null, 'commonjs' + request);\n}\n" + |
| 51 | + "callback();\n}" |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +export function parseValue(regexp: string): string { |
| 56 | + return jscodeshift(regexp); |
| 57 | +} |
| 58 | + |
| 59 | +export function createRequire(val: string): string { |
| 60 | + return `const ${val} = require('${val}');`; |
| 61 | +} |
| 62 | + |
| 63 | +export function List(name: string, message: string, choices: string[]): IInquirerList { |
| 64 | + return { |
| 65 | + choices, |
| 66 | + message, |
| 67 | + name, |
| 68 | + type: "list", |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +export function RawList(name: string, message: string, choices: string[]): IInquirerList { |
| 73 | + return { |
| 74 | + choices, |
| 75 | + message, |
| 76 | + name, |
| 77 | + type: "rawlist", |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +export function CheckList(name: string, message: string, choices: string[]): IInquirerList { |
| 82 | + return { |
| 83 | + choices, |
| 84 | + message, |
| 85 | + name, |
| 86 | + type: "checkbox", |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +export function Input(name: string, message: string): IInquirerInput { |
| 91 | + return { |
| 92 | + message, |
| 93 | + name, |
| 94 | + type: "input", |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +export function InputValidate(name: string, message: string, cb: Function): IInquirerInput { |
| 99 | + return { |
| 100 | + message, |
| 101 | + name, |
| 102 | + type: "input", |
| 103 | + validate: cb, |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +export function Confirm(name: string, message: string): IScaffoldBaseObject { |
| 108 | + return { |
| 109 | + message, |
| 110 | + name, |
| 111 | + type: "confirm", |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +export function AutoComplete(name: string, message: string, options: object = {}) { |
| 116 | + return Object.assign({ |
| 117 | + message, |
| 118 | + name, |
| 119 | + type: "autocomplete", |
| 120 | + }, options); |
| 121 | +} |
0 commit comments