Skip to content

Commit

Permalink
feat(templatesList): add templates list for stable and experimental f…
Browse files Browse the repository at this point in the history
…or interactive choices

- add template types and enum
- add conditional template selection
- fix src/new/cli.ts bad eol
  • Loading branch information
joaoneto committed Oct 9, 2023
1 parent 79b2f5f commit cee5213
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./config";
export * from "./template";
10 changes: 10 additions & 0 deletions src/@types/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export enum TemplateEnum {
OPINIONATED = "opinionated",
NON_OPINIONATED = "non-opinionated"
}

export interface Template {
name: TemplateEnum;
description: string;
path: string;
}
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ yargs(hideBin(process.argv))
)
.example(
"$0 new expressots-demo -p yarn -t opinionated --experimental",
"Create with experimental swc build system"
"Create using experimental, not battle-tested, features of ExpressoTS",
)
.example("$0 generate service user-create", "Scaffold a service")
.example("$0 info", "Show CLI details")
Expand Down
17 changes: 3 additions & 14 deletions src/new/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const createProject = (): CommandModule<CommandModuleArgs, any> => {
alias: "d",
})
.option("experimental", {
describe: "Use experimental traspile with swc",
describe: "Use experimental, not battle-tested, features of ExpressoTS",
type: "boolean",
default: false,
})
Expand All @@ -47,19 +47,8 @@ const createProject = (): CommandModule<CommandModuleArgs, any> => {

return yargs;
},
handler: async ({
projectName,
packageManager,
template,
directory,
experimental,
}) => {
return await projectForm(projectName, [
packageManager,
template,
directory,
experimental,
]);
handler: async ({ projectName, packageManager, template, directory, experimental }) => {
return await projectForm(projectName, [packageManager, template, directory, experimental]);
},
};
};
Expand Down
46 changes: 24 additions & 22 deletions src/new/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import fs from "node:fs";
import path from "node:path";
import { centerText } from "../utils/center-text";
import { printError } from "../utils/cli-ui";
import { TemplateEnum } from "../@types";
import templateList from "../templates-list";

async function packageManagerInstall({
packageManager,
Expand Down Expand Up @@ -79,11 +81,6 @@ function changePackageName({
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
}

enum Template {
"non-opinionated" = "Non-Opinionated :: A simple ExpressoTS project.",
opinionated = "Opinionated :: A complete ExpressoTS project with an opinionated structure and features.",
}

const enum PackageManager {
npm = "npm",
yarn = "yarn",
Expand All @@ -95,7 +92,7 @@ const projectForm = async (projectName: string, args: any[]): Promise<void> => {
let answer: any;
const projName: string = projectName;
let packageManager: PackageManager | undefined;
let template: keyof typeof Template | undefined;
let template: TemplateEnum | undefined;
let directory: string | undefined;
let experimental: boolean;

Expand All @@ -111,7 +108,7 @@ const projectForm = async (projectName: string, args: any[]): Promise<void> => {
) {
packageManager = arg as PackageManager;
} else if (arg === "non-opinionated" || arg === "opinionated") {
template = arg as keyof typeof Template;
template = arg as TemplateEnum;
} else if (arg === true) {
experimental = arg;
} else {
Expand All @@ -124,7 +121,7 @@ const projectForm = async (projectName: string, args: any[]): Promise<void> => {
answer = {
name: projectName,
packageManager: packageManager,
template: Template[template],
template: template,
experimental: experimental,
confirm: true,
};
Expand All @@ -145,20 +142,25 @@ const projectForm = async (projectName: string, args: any[]): Promise<void> => {
message: "Package manager",
choices: ["npm", "yarn", "pnpm", "bun"],
},
{
type: "confirm",
name: "experimental",
message: "Use experimental, not battle-tested, features of ExpressoTS",
default: false,
},
{
type: "list",
name: "template",
message: "Select a template",
choices: [
"Non-Opinionated :: A simple ExpressoTS project.",
"Opinionated :: A complete ExpressoTS project with an opinionated structure and features.",
],
when: ({ experimental }) => !experimental,
choices: templateList.stable.map(({ description }) => description),
},
{
type: "confirm",
name: "experimental",
message: "Use experimental traspile with swc",
default: false
type: "list",
name: "template",
message: "Select a experimental template",
when: ({ experimental }) => experimental,
choices: templateList.experimental.map(({ description }) => description),
},
{
type: "confirm",
Expand All @@ -179,9 +181,9 @@ const projectForm = async (projectName: string, args: any[]): Promise<void> => {
}

// Hashmap of templates and their directories
const templates: Record<string, unknown> = {
"Non-Opinionated": "non_opinionated",
Opinionated: "opinionated",
const templates: Record<string, TemplateEnum> = {
"Non-Opinionated": TemplateEnum.NON_OPINIONATED,
Opinionated: TemplateEnum.OPINIONATED,
};

if (answer.confirm) {
Expand Down Expand Up @@ -215,9 +217,9 @@ const projectForm = async (projectName: string, args: any[]): Promise<void> => {

try {
// @todo: new templates, see expressots/expressots PR #79
const emitter = experimental
? degit(`expressots/expressots/templates/experimental/${templates[template]}`)
: degit(`expressots/expressots/templates/${templates[template]}`);
const stability = answer.experimental ? "experimental" : "stable";
const { path } = templateList[stability].find((item) => (item.name = templates[template]));
const emitter = degit(path);

await emitter.clone(answer.name);
} catch (err: any) {
Expand Down
30 changes: 30 additions & 0 deletions src/templates-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { TemplateEnum, Template } from "./@types";

const stable: Template[] = [
{
name: TemplateEnum.OPINIONATED,
description: "Non-Opinionated :: A simple ExpressoTS project.",
path: "expressots/expressots/templates/non_opinionated",
},
{
name: TemplateEnum.NON_OPINIONATED,
description: "Opinionated :: A complete ExpressoTS project with an opinionated structure and features.",
path: "expressots/expressots/templates/opinionated",
},
];

const experimental: Template[] = [
{
name: TemplateEnum.OPINIONATED,
description: "Non-Opinionated :: EXPERIMENTAL BUILD - A simple ExpressoTS project.",
path: "expressots/expressots/templates/experimental/non_opinionated",
},
{
name: TemplateEnum.NON_OPINIONATED,
description:
"Opinionated :: EXPERIMENTAL BUILD - A complete ExpressoTS project with an opinionated structure and features.",
path: "expressots/expressots/templates/experimental/opinionated",
},
];

export default { stable, experimental };

0 comments on commit cee5213

Please sign in to comment.