From 345117762aa8db67ec17c5bac3a5f9699606a8b1 Mon Sep 17 00:00:00 2001 From: Thomas Beer <71586988+Tommypop2@users.noreply.github.com> Date: Sun, 14 Apr 2024 14:51:44 +0100 Subject: [PATCH] Add templatePositional argument Replace `p.log.info` with `p.note` Fix awful error messages --- packages/commands/src/handlers/new.ts | 33 +++++++++++++++++++-------- packages/create-solid/src/index.ts | 25 +++++++++++++++++--- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/packages/commands/src/handlers/new.ts b/packages/commands/src/handlers/new.ts index 0dcb903..6826d8c 100644 --- a/packages/commands/src/handlers/new.ts +++ b/packages/commands/src/handlers/new.ts @@ -60,8 +60,10 @@ const startSupported = [ const localSupported = ["ts", "js"] as const; const stackblitzSupported = ["bare"] as const; -type AllSupported = (typeof localSupported)[number] | (typeof stackblitzSupported)[number]; - +export type AllSupported = (typeof localSupported)[number] | (typeof stackblitzSupported)[number]; +export const isSupported = (template: string): template is AllSupported => { + return localSupported.indexOf(template as any) !== -1; +}; const modifyReadme = async (name: string) => { await insertAtEnd( `${name}/README.md`, @@ -162,10 +164,12 @@ const handleNewStartProject = async (projectName: string, variation?: AllSupport writeFileSync(join(projectName, ".gitignore"), gitIgnore); if (!readmeAlreadyExists) await modifyReadme(projectName); const pM = detectPackageManager(); - p.log.info(`${t.GET_STARTED} - - cd ${projectName} - - ${pM.name} install - - ${pM.name} ${pM.runScriptCommand("dev")}`); + p.note( + `cd ${projectName} +${pM.name} install +${pM.name} ${pM.runScriptCommand("dev")}`, + t.GET_STARTED, + ); }; const handleAutocompleteNew = async (name: string, isStart?: boolean) => { @@ -195,6 +199,13 @@ export const handleNew = async ( name ??= await cancelable( p.text({ message: t.PROJECT_NAME, placeholder: "solid-project", defaultValue: "solid-project" }), ); + await spinnerify({ + startText: "This will break", + finishText: "Should've broken", + fn: async () => { + throw new Error("Very broken"); + }, + }); if (!variation) { await handleAutocompleteNew(name, isStart); @@ -230,8 +241,10 @@ export const handleNew = async ( writeFileSync(join(name, ".gitignore"), gitIgnore); if (!readmeAlreadyExists) await modifyReadme(name ?? variation); const pM = detectPackageManager(); - p.log.info(`${t.GET_STARTED} - - cd ${name} - - ${pM.name} install - - ${pM.name} ${pM.runScriptCommand("dev")}`); + p.note( + `cd ${name} +${pM.name} install +${pM.name} ${pM.runScriptCommand("dev")}`, + t.GET_STARTED, + ); }; diff --git a/packages/create-solid/src/index.ts b/packages/create-solid/src/index.ts index d18ee1f..e331de8 100644 --- a/packages/create-solid/src/index.ts +++ b/packages/create-solid/src/index.ts @@ -1,5 +1,5 @@ #! /usr/bin/env node -import { handleNew } from "@solid-cli/commands/new"; +import { AllSupported, handleNew, isSupported } from "@solid-cli/commands/new"; import color from "picocolors"; import { intro } from "@clack/prompts"; import { version } from "../package.json"; @@ -16,6 +16,11 @@ const app = command({ displayName: "Project Name", description: "The name of the project to be generated", }), + templatePositional: positional({ + type: optional(string), + displayName: "Template name", + description: "Name of template to be initialised", + }), projectNameOption: option({ type: optional(string), long: "project-name", @@ -29,8 +34,22 @@ const app = command({ description: "Create a SolidStart project", }), }, - handler: async ({ projectNameOption, solidStart, projectNamePositional }) => { - await handleNew(undefined, projectNamePositional ?? projectNameOption, false, solidStart); + handler: async ({ projectNameOption, solidStart, projectNamePositional, templatePositional }) => { + if (templatePositional && !isSupported(templatePositional)) { + console.error(`Template "${templatePositional}" is not supported`); + process.exit(0); + } + try { + await handleNew( + templatePositional as AllSupported, + projectNamePositional ?? projectNameOption, + false, + solidStart, + ); + } catch (e) { + console.error(e); + process.exit(1); + } }, }); run(app, process.argv.slice(2));