From 5db42875e15fcce711d24d73b752fb6c1e78291c Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 11 Dec 2019 01:40:41 +0100 Subject: [PATCH] fix: move modules installation question and refactor prompt func --- bin/index.js | 54 ++++++++----------- src/questions.json | 130 +++++++++++++++++++++++---------------------- 2 files changed, 89 insertions(+), 95 deletions(-) diff --git a/bin/index.js b/bin/index.js index 3eb334a..c1674e1 100644 --- a/bin/index.js +++ b/bin/index.js @@ -29,7 +29,7 @@ const TEMPLATE_DIR = join(ROOT_DIR, "template"); const DEFAULT_FILES_DIR = join(TEMPLATE_DIR, "defaultFiles"); const DEFAULT_FILES_INCLUDE = join(TEMPLATE_DIR, "include"); const DEFAULT_FILES_TEST = join(TEMPLATE_DIR, "test"); -const GEN_QUESTIONS = require("../src/questions.json"); +const { GEN_QUESTIONS, MODULES_QUESTIONS } = require("../src/questions.json"); const { DEV_DEPENDENCIES, NAPI_DEPENDENCIES } = require("../src/dependencies.json"); const TEST_SCRIPTS = { ava: "cross-env psp && ava --verbose", @@ -109,24 +109,24 @@ async function getQueriesResponse() { if (row.type === "interactive") { row.symbol = "->"; } - let ret = await qoa.prompt([row]); - - while (row.handle === "projectname") { - ret.projectname = filterPackageName(ret.projectname); - if (ret.projectname.length <= 1 || ret.projectname.length > 214) { - console.log(red().bold("The project name must be of length 2<>214")); - ret = await qoa.prompt([row]); - } - else { - break; + let ret; + + while (true) { + ret = await qoa.prompt([row]); + if (row.handle === "projectname") { + ret.projectname = filterPackageName(ret.projectname); + if (ret.projectname.length <= 1 || ret.projectname.length > 214) { + console.log(red().bold("The project name must be of length 2<>214")); + continue; + } } - } - if (row.handle === "testfw" && ret.testfw === "jest") { - skipNext = true; - response.covpackage = null; + if (row.handle === "testfw" && ret.testfw === "jest") { + skipNext = true; + response.covpackage = null; + } + break ; } - Object.assign(response, ret); console.log(gray().bold("----------------------------")); } @@ -153,26 +153,11 @@ async function main() { const response = await getQueriesResponse(); const projectName = response.projectname; - /* - const projectName = filterPackageName(response.projectname); - if (projectName.length <= 1 || projectName.length > 214) { - console.log(red().bold("The project name must be of length 2<>214")); - process.exit(0); - } - */ - // Check the addon package name if (response.type === "Addon" && !validate(projectName)) { console.log(red().bold(`The addon name not matching expected regex ${CONSTANTS.VALIDATE_REGEX}`)); process.exit(0); } - - // Check if the developer want to install nodes modules - if (response.modules === false) { - console.log("Nodes modules are required to run the project."); - process.exit(0); - } - console.log(gray().bold(`\n > Start configuring project ${cyan().bold(projectName)}\n`)); // Create initial package.json && write default projects files @@ -353,6 +338,13 @@ async function main() { await writeFile("index.js", "\"use strict\";\n"); } + // Installation of nodes modules + const { modules } = await qoa.prompt([MODULES_QUESTIONS]); + if (modules === false) { + console.log("Nodes modules aren't installed."); + process.exit(0); + } + const spinner = new Spinner().start(white().bold(`Running '${cyan().bold("npm install")}' on node_modules ...`)); try { const start = performance.now(); diff --git a/src/questions.json b/src/questions.json index e320eca..585dee3 100644 --- a/src/questions.json +++ b/src/questions.json @@ -1,64 +1,66 @@ -[ - { - "query": "Name of the project ?", - "type": "input", - "handle": "projectname" - }, - { - "query": "A description (optional) ?", - "type": "input", - "handle": "projectdesc" - }, - { - "query": "What kind of project is this ? ", - "description": "Documentation on each 'kind' can be found here: https://github.com/SlimIO/Manifest#available-types", - "type": "interactive", - "handle": "type", - "menu": ["Addon", "NAPI", "CLI", "Package", "Service", "Degraded"] - }, - { - "query": "Which unit-testing package do you want ?", - "description": "If you do not know what it is, take 'japa'", - "type": "interactive", - "handle": "testfw", - "menu": ["japa", "ava", "jest"] - }, - { - "query": "Which package do you want for code coverage ?", - "description": "If you do not know what it is, take 'nyc'", - "type": "interactive", - "handle": "covpackage", - "menu": ["nyc", "c8"] - }, - { - "query": "In which version do you want to start : 1.0.0 or 0.1.0 ", - "description": "Choose 0.1.0 if the project is an experimentation (or will not be pushed to production after first draft).", - "type": "interactive", - "handle": "version", - "menu": ["1.0.0", "0.1.0"] - }, - { - "query": "Do you want a .env file ?", - "description": ".env file are useful to store credentials (Database, WEB API tokens etc..)", - "type": "confirm", - "handle": "env" - }, - { - "query": "Do you want a .npmrc file ?", - "description": ".npmrc file are useful to configure npm (Proxy, Registery etc..)", - "type": "confirm", - "handle": "npmrc" - }, - { - "query": "Is this project must have a binary entry ?", - "description": "Most of the time it's only required for CLI projects. Say 'no' if you dont know why you need this.", - "type": "confirm", - "handle": "binary" - }, - { - "query": "Do you want to install nodes modules ?", - "description": "Nodes modules are required for use the project", - "type": "confirm", - "handle": "modules" - } -] +{ + "GEN_QUESTIONS": [ + { + "query": "Name of the project ?", + "type": "input", + "handle": "projectname" + }, + { + "query": "A description (optional) ?", + "type": "input", + "handle": "projectdesc" + }, + { + "query": "What kind of project is this ? ", + "description": "Documentation on each 'kind' can be found here: https://github.com/SlimIO/Manifest#available-types", + "type": "interactive", + "handle": "type", + "menu": ["Addon", "NAPI", "CLI", "Package", "Service", "Degraded"] + }, + { + "query": "Which unit-testing package do you want ?", + "description": "If you do not know what it is, take 'japa'", + "type": "interactive", + "handle": "testfw", + "menu": ["japa", "ava", "jest"] + }, + { + "query": "Which package do you want for code coverage ?", + "description": "If you do not know what it is, take 'nyc'", + "type": "interactive", + "handle": "covpackage", + "menu": ["nyc", "c8"] + }, + { + "query": "In which version do you want to start : 1.0.0 or 0.1.0 ", + "description": "Choose 0.1.0 if the project is an experimentation (or will not be pushed to production after first draft).", + "type": "interactive", + "handle": "version", + "menu": ["1.0.0", "0.1.0"] + }, + { + "query": "Do you want a .env file ?", + "description": ".env file are useful to store credentials (Database, WEB API tokens etc..)", + "type": "confirm", + "handle": "env" + }, + { + "query": "Do you want a .npmrc file ?", + "description": ".npmrc file are useful to configure npm (Proxy, Registery etc..)", + "type": "confirm", + "handle": "npmrc" + }, + { + "query": "Is this project must have a binary entry ?", + "description": "Most of the time it's only required for CLI projects. Say 'no' if you dont know why you need this.", + "type": "confirm", + "handle": "binary" + } + ], + "MODULES_QUESTIONS": { + "query": "Do you want to install nodes modules ?", + "description": "Nodes modules are required for use the project", + "type": "confirm", + "handle": "modules" + } +}