From ce8aa40b16470ed2f3f64cae8a037453b2849493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20APLAK?= Date: Thu, 25 Aug 2022 14:35:19 +0300 Subject: [PATCH] feat: improvement preset support (#313) * feat: add preset support * bump 1.7.3 --- @types/sao.d.ts | 1 + package.json | 2 +- src/Helper/index.ts | 2 + src/Helper/npm/index.ts | 36 ++++++++++++++++++ src/Helper/preset/index.ts | 3 +- src/Helper/telemetry/index.ts | 20 ++++++++++ src/cli.ts | 39 ++++++++++--------- src/saofile.ts | 72 ++++++----------------------------- 8 files changed, 94 insertions(+), 81 deletions(-) create mode 100644 src/Helper/npm/index.ts create mode 100644 src/Helper/telemetry/index.ts diff --git a/@types/sao.d.ts b/@types/sao.d.ts index 447566d4..98038380 100644 --- a/@types/sao.d.ts +++ b/@types/sao.d.ts @@ -309,6 +309,7 @@ interface IExtras { debug: boolean; paths: IPaths; projectType: string; + npmClient: NPM_CLIENT; presetAnswers?: Record; } interface Options$1 { diff --git a/package.json b/package.json index e0ec5b19..797def52 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "superplate-cli", - "version": "1.7.1", + "version": "1.7.3", "description": "The frontend boilerplate with superpowers", "license": "MIT", "repository": { diff --git a/src/Helper/index.ts b/src/Helper/index.ts index d3f3eac8..9b039990 100644 --- a/src/Helper/index.ts +++ b/src/Helper/index.ts @@ -29,3 +29,5 @@ export { get_random_answer, get_random_answers, } from "./lucky"; +export { prompt_telemetry } from "./telemetry"; +export { prompt_npm_cli } from "./npm"; diff --git a/src/Helper/npm/index.ts b/src/Helper/npm/index.ts new file mode 100644 index 00000000..7b6dcdc6 --- /dev/null +++ b/src/Helper/npm/index.ts @@ -0,0 +1,36 @@ +import prompts from "prompts"; + +import { BinaryHelper } from "@Helper/binary"; + +export const prompt_npm_cli = async (): Promise<{ client: string }> => { + const pmQuestionChoises = [{ title: "Npm", value: "npm" }]; + const canUseYarn = BinaryHelper.CanUseYarn(); + const canUsePnpm = BinaryHelper.CanUsePnpm(); + + if (canUseYarn) { + pmQuestionChoises.push({ title: "Yarn", value: "yarn" }); + } + + if (canUsePnpm) { + pmQuestionChoises.push({ + title: "pnpm" + .split("") + .map((v) => + Math.round(Math.random()) + ? v.toUpperCase() + : v.toLowerCase(), + ) + .join(""), + value: "pnpm", + }); + } + + const { npmClient } = await prompts({ + type: "select", + name: "npmClient", + message: "Package manager:", + choices: pmQuestionChoises, + }); + + return npmClient; +}; diff --git a/src/Helper/preset/index.ts b/src/Helper/preset/index.ts index 881b399c..9ae5c416 100644 --- a/src/Helper/preset/index.ts +++ b/src/Helper/preset/index.ts @@ -2,13 +2,14 @@ import path from "path"; export type Preset = { name: string; + type: string; answers: Record; }; export const get_presets = async (source: string): Promise => { try { // eslint-disable-next-line @typescript-eslint/no-var-requires - const sourcePrompts = require(path.resolve(source, "prompt.js")); + const sourcePrompts = require(path.resolve(source, "presets.js")); return (sourcePrompts.presets ?? []) as Preset[]; } catch (e) { diff --git a/src/Helper/telemetry/index.ts b/src/Helper/telemetry/index.ts new file mode 100644 index 00000000..77094811 --- /dev/null +++ b/src/Helper/telemetry/index.ts @@ -0,0 +1,20 @@ +import prompts from "prompts"; + +export const prompt_telemetry = async (): Promise<{ + telemetry: "yes" | "no"; +}> => { + const { telemetry } = await prompts({ + type: "select", + name: "telemetry", + message: "Would you like to share your choices with us anonymously?", + choices: [ + { + title: "I want to share anonymously! Thank you! ❤️", + value: "yes", + }, + { title: "No", value: "no" }, + ], + }); + + return telemetry; +}; diff --git a/src/cli.ts b/src/cli.ts index 76846a34..d6c72ce3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -14,6 +14,7 @@ import { get_presets, get_prompts_and_choices, get_random_answers, + prompt_npm_cli, } from "@Helper"; const generator = path.resolve(__dirname, "./"); @@ -134,27 +135,9 @@ const cli = async (): Promise => { process.exit(1); } + let projectType = program.project; const isMultiType = await is_multi_type(sourcePath); - let projectType = ""; - - if (sourcePath && isMultiType) { - // get project types - const projectTypes = await get_project_types(sourcePath); - - const [ - finalSourcePath, - selectedProjectType, - ] = await prompt_project_types( - sourcePath, - projectTypes, - program.project, - ); - - sourcePath = finalSourcePath; - projectType = selectedProjectType; - } - /** handle presets, can either be partial or fully provided answers from `prompt.js > presets` */ let presetAnswers: Record | undefined = undefined; const selectedPreset = program.preset; @@ -167,12 +150,29 @@ const cli = async (): Promise => { if (preset) { presetAnswers = preset.answers; + projectType = preset.type; } else { console.log( `${chalk.bold`${selectedPreset}`} is not a valid preset.`, ); } } + + const npmClient = await prompt_npm_cli(); + + if (sourcePath && isMultiType) { + // get project types + const projectTypes = await get_project_types(sourcePath); + + const [ + finalSourcePath, + selectedProjectType, + ] = await prompt_project_types(sourcePath, projectTypes, projectType); + + sourcePath = finalSourcePath; + projectType = selectedProjectType; + } + if (isLucky && sourcePath) { const promptsAndChoices = await get_prompts_and_choices(sourcePath); presetAnswers = get_random_answers(promptsAndChoices); @@ -192,6 +192,7 @@ const cli = async (): Promise => { extras: { debug: !!program.debug, projectType, + npmClient, paths: { sourcePath, }, diff --git a/src/saofile.ts b/src/saofile.ts index 7ebd94e1..c8bf5966 100644 --- a/src/saofile.ts +++ b/src/saofile.ts @@ -19,7 +19,7 @@ import { mergeBabel, tips, mergePluginData, - BinaryHelper, + prompt_telemetry, } from "@Helper"; import { ProjectPrompt } from "@Helper/lucky"; @@ -37,28 +37,6 @@ const saoConfig: GeneratorConfig = { "prompt.js", )); - const pmQuestionChoises = [{ message: "Npm", value: "npm" }]; - const canUseYarn = BinaryHelper.CanUseYarn(); - const canUsePnpm = BinaryHelper.CanUsePnpm(); - - if (canUseYarn) { - pmQuestionChoises.push({ message: "Yarn", value: "yarn" }); - } - - if (canUsePnpm) { - pmQuestionChoises.push({ - message: "pnpm" - .split("") - .map((v) => - Math.round(Math.random()) - ? v.toUpperCase() - : v.toLowerCase(), - ) - .join(""), - value: "pnpm", - }); - } - return [ { type: "input", @@ -66,52 +44,24 @@ const saoConfig: GeneratorConfig = { message: "What will be the name of your app", default: appName, }, - ...(pmQuestionChoises.length > 1 - ? [ - { - name: "pm", - message: "Package manager:", - choices: pmQuestionChoises, - type: "select", - default: "npm", - }, - ] - : []), ...(sourcePrompts?.prompts ?? []).map((el: ProjectPrompt) => ({ ...el, default: presetAnswers?.[el.name] ?? el.default, })), - { - name: "telemetry", - message: - "Would you like to share your choices with us anonymously?", - type: "select", - pageSize: 2, - choices: [ - { - message: "I want to share anonymously! Thank you! ❤️", - name: "yes", - }, - { message: "No", name: "no" }, - ], - default: "yes", - }, ]; }, data(sao) { /** * Package Manager */ - - sao.answers.pm = - BinaryHelper.CanUseYarn() || BinaryHelper.CanUsePnpm() - ? sao.answers.pm - : "npm"; + const { + extras: { npmClient }, + } = sao.opts; let pmRun = "npm run"; - if (sao.answers.pm === "yarn") { + if (npmClient === "yarn") { pmRun = "yarn"; - } else if (sao.answers.pm === "pnpm") { + } else if (npmClient === "pnpm") { pmRun = "pnpm"; } @@ -317,7 +267,9 @@ const saoConfig: GeneratorConfig = { }, }); - if (sao.answers.telemetry === "yes") { + const { telemetry } = await prompt_telemetry(); + + if (telemetry === "yes") { analytics.track({ event: "generate", properties: { @@ -334,14 +286,14 @@ const saoConfig: GeneratorConfig = { tips.preInstall(); }, async completed(saoInstance) { - const { debug } = saoInstance.opts.extras; + const { debug, npmClient } = saoInstance.opts.extras; /** * Git init and install packages */ if (!debug) { saoInstance.gitInit(); await saoInstance.npmInstall({ - npmClient: this.answers.pm, + npmClient: npmClient, installArgs: ["--silent"], }); } @@ -378,7 +330,7 @@ const saoConfig: GeneratorConfig = { tips.postInstall({ name: saoInstance.opts.appName ?? "", dir: saoInstance.outDir, - pm: saoInstance.answers.pm, + pm: saoInstance.opts.extras.npmClient, }); }, };