Skip to content

Commit

Permalink
feat: improvement preset support (#313)
Browse files Browse the repository at this point in the history
* feat: add preset support

* bump 1.7.3
omeraplak authored Aug 25, 2022
1 parent f765347 commit ce8aa40
Showing 8 changed files with 94 additions and 81 deletions.
1 change: 1 addition & 0 deletions @types/sao.d.ts
Original file line number Diff line number Diff line change
@@ -309,6 +309,7 @@ interface IExtras {
debug: boolean;
paths: IPaths;
projectType: string;
npmClient: NPM_CLIENT;
presetAnswers?: Record<string, string | undefined>;
}
interface Options$1 {
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "superplate-cli",
"version": "1.7.1",
"version": "1.7.3",
"description": "The frontend boilerplate with superpowers",
"license": "MIT",
"repository": {
2 changes: 2 additions & 0 deletions src/Helper/index.ts
Original file line number Diff line number Diff line change
@@ -29,3 +29,5 @@ export {
get_random_answer,
get_random_answers,
} from "./lucky";
export { prompt_telemetry } from "./telemetry";
export { prompt_npm_cli } from "./npm";
36 changes: 36 additions & 0 deletions src/Helper/npm/index.ts
Original file line number Diff line number Diff line change
@@ -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;
};
3 changes: 2 additions & 1 deletion src/Helper/preset/index.ts
Original file line number Diff line number Diff line change
@@ -2,13 +2,14 @@ import path from "path";

export type Preset = {
name: string;
type: string;
answers: Record<string, string>;
};

export const get_presets = async (source: string): Promise<Preset[]> => {
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) {
20 changes: 20 additions & 0 deletions src/Helper/telemetry/index.ts
Original file line number Diff line number Diff line change
@@ -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;
};
39 changes: 20 additions & 19 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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<string, string> | undefined = undefined;
const selectedPreset = program.preset;
@@ -167,12 +150,29 @@ const cli = async (): Promise<void> => {

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<void> => {
extras: {
debug: !!program.debug,
projectType,
npmClient,
paths: {
sourcePath,
},
72 changes: 12 additions & 60 deletions src/saofile.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ import {
mergeBabel,
tips,
mergePluginData,
BinaryHelper,
prompt_telemetry,
} from "@Helper";

import { ProjectPrompt } from "@Helper/lucky";
@@ -37,81 +37,31 @@ 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",
name: "name",
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,
});
},
};

0 comments on commit ce8aa40

Please sign in to comment.