|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 12 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 13 | +}; |
| 14 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 15 | +const arg_1 = __importDefault(require("arg")); |
| 16 | +const inquirer_1 = __importDefault(require("inquirer")); |
| 17 | +const main_1 = __importDefault(require("./main")); |
| 18 | +function parseArgumentsIntoOptions(rawArgs) { |
| 19 | + const args = arg_1.default({ |
| 20 | + "--typescript": Boolean, |
| 21 | + "--filepath": String, |
| 22 | + // Aliases |
| 23 | + "-t": "--typescript", |
| 24 | + "-m": "--mongoose", |
| 25 | + }, { |
| 26 | + argv: rawArgs.slice(2), |
| 27 | + }); |
| 28 | + return { |
| 29 | + language: args["--typescript"] || false, |
| 30 | + filePath: args["--filepath"] || "/", |
| 31 | + }; |
| 32 | +} |
| 33 | +function promptForMissingOptions(options) { |
| 34 | + return __awaiter(this, void 0, void 0, function* () { |
| 35 | + const defaultOptions = { |
| 36 | + language: "Javascript", |
| 37 | + schema: "default", |
| 38 | + }; |
| 39 | + const questions = []; |
| 40 | + //@TODO: Temporary Disabled, Uncomment after adding Typescript Schema |
| 41 | + // if (!options.language) { |
| 42 | + // questions.push({ |
| 43 | + // type: "list", |
| 44 | + // name: "language", |
| 45 | + // message: "Please choose which language Schema to use", |
| 46 | + // choices: ["JavaScript", "TypeScript"], |
| 47 | + // default: defaultOptions.language, |
| 48 | + // }); |
| 49 | + // } |
| 50 | + questions.push({ |
| 51 | + type: "input", |
| 52 | + name: "schema", |
| 53 | + message: "Please input Schema name", |
| 54 | + default: defaultOptions.schema, |
| 55 | + }); |
| 56 | + const answers = yield inquirer_1.default.prompt(questions); |
| 57 | + return Object.assign(Object.assign({}, options), { language: options.language || answers.language, schema: answers.schema }); |
| 58 | + }); |
| 59 | +} |
| 60 | +function promptForSchemaObject() { |
| 61 | + return __awaiter(this, void 0, void 0, function* () { |
| 62 | + const defaultOptions = { |
| 63 | + name: "default", |
| 64 | + type: "String", |
| 65 | + required: true, |
| 66 | + default: "", |
| 67 | + }; |
| 68 | + const questions = []; |
| 69 | + questions.push({ |
| 70 | + type: "input", |
| 71 | + name: "name", |
| 72 | + message: "Please input Schema Key name", |
| 73 | + default: defaultOptions.name, |
| 74 | + }); |
| 75 | + questions.push({ |
| 76 | + type: "list", |
| 77 | + name: "type", |
| 78 | + message: "Please input Schema Key type", |
| 79 | + choices: ["String", "Number", "Boolean"], |
| 80 | + default: defaultOptions.name, |
| 81 | + }); |
| 82 | + questions.push({ |
| 83 | + type: "confirm", |
| 84 | + name: "required", |
| 85 | + message: "Is this Schema Key Required", |
| 86 | + default: defaultOptions.required, |
| 87 | + }); |
| 88 | + questions.push({ |
| 89 | + type: "input", |
| 90 | + name: "default", |
| 91 | + message: "What is the default value?", |
| 92 | + default: defaultOptions.default, |
| 93 | + }); |
| 94 | + const answers = yield inquirer_1.default.prompt(questions); |
| 95 | + return { |
| 96 | + name: answers.name, |
| 97 | + type: answers.type, |
| 98 | + isRequired: answers.required, |
| 99 | + defaultValue: answers.default, |
| 100 | + }; |
| 101 | + }); |
| 102 | +} |
| 103 | +function cli(args) { |
| 104 | + return __awaiter(this, void 0, void 0, function* () { |
| 105 | + var options = parseArgumentsIntoOptions(args); |
| 106 | + var missingOptions = yield promptForMissingOptions(options); |
| 107 | + const { schemaKeys } = yield inquirer_1.default.prompt({ |
| 108 | + type: "input", |
| 109 | + name: "schemaKeys", |
| 110 | + message: "How many key this Schema has?", |
| 111 | + default: 0, |
| 112 | + }); |
| 113 | + const schemaKeyValues = []; |
| 114 | + for (let i = 0; i < schemaKeys; i++) { |
| 115 | + const objectValues = yield promptForSchemaObject(); |
| 116 | + schemaKeyValues.push(objectValues); |
| 117 | + } |
| 118 | + var schema = missingOptions.schema; |
| 119 | + yield main_1.default(schema, schemaKeyValues); |
| 120 | + }); |
| 121 | +} |
| 122 | +exports.default = cli; |
0 commit comments