From b3fdb2ab82e4b6e3b377744995e8e1cbd237a077 Mon Sep 17 00:00:00 2001 From: markthree <1801982702@qq.com> Date: Sat, 25 Mar 2023 20:21:41 +0800 Subject: [PATCH] chore: restrict package manager selection --- mod.ts | 37 ++++++++++++++----------------------- src/pm.ts | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/mod.ts b/mod.ts index 3c7473d..634adee 100644 --- a/mod.ts +++ b/mod.ts @@ -1,40 +1,31 @@ import { cyan, - dim, green, yellow, } from "https://deno.land/std@0.178.0/fmt/colors.ts"; + import { listLog } from "./src/log.ts"; +import { isPackageManager, usePackageManager } from "./src/pm.ts"; import { exist, findUpNodeModules, findUpPackageJson } from "./src/fs.ts"; -import type { PackageManager } from "./src/pm.ts"; import { execa, normalFusing } from "./src/process.ts"; import { join } from "https://deno.land/std@0.180.0/path/mod.ts"; -import { isPackageManager, usePackageManager } from "./src/pm.ts"; import { extractDeps, extractDepsFromPackageJson } from "./src/deps.ts"; const { staging, + ref: pm, getCommand, - ref: packageManager, + select: selectPM, } = usePackageManager(); -function inputPackageManager() { - packageManager.value = prompt( - `🤯 Input your package manager ${dim( - "(npm | yarn | pnpm)", - ) - }`, - "npm", - ) as PackageManager; -} export async function hopeCreateProject() { if (Deno.args[0] !== "create") { return false; } - inputPackageManager(); + await selectPM(); await execa(getCommand()); @@ -60,11 +51,11 @@ export async function ensureProjectInit() { normalFusing(); } - inputPackageManager(); + await selectPM(); - const cmd = [packageManager.value, "init"]; + const cmd = [pm.value, "init"]; - if (packageManager.value !== "pnpm") { + if (pm.value !== "pnpm") { const skipTedious = confirm( `👻 Whether to ${green("skip complicated steps")}?`, ); @@ -86,10 +77,10 @@ async function runCommand() { return true; } -function refresh() { +async function refresh() { if (Deno.args[0] === "refresh") { if (isPackageManager(Deno.args[1])) { - packageManager.value = Deno.args[1]; + pm.value = Deno.args[1]; return here(true); } @@ -99,7 +90,7 @@ function refresh() { if (!wantRefresh) normalFusing(); - inputPackageManager(); + await selectPM(); return here(true); } @@ -109,7 +100,7 @@ function here(see = Deno.args[0] === "here") { if (see) { console.log( `🦖 The manager of the current directory is ${cyan( - packageManager.value ?? "null", + pm.value ?? "null", ) }`, ); @@ -167,8 +158,8 @@ async function autoInstall( if (wantInstallDeps) { await execa([ - packageManager.value ?? "npm", - packageManager.value === "yarn" ? "add" : "install", + pm.value ?? "npm", + pm.value === "yarn" ? "add" : "install", ...depsToInstall, ]); } diff --git a/src/pm.ts b/src/pm.ts index 9f08fe8..a7976d2 100644 --- a/src/pm.ts +++ b/src/pm.ts @@ -1,8 +1,11 @@ import { exist } from "./fs.ts"; import { creatLocalStorageRef } from "./storage.ts"; +import { Select } from "https://deno.land/x/cliffy@v0.25.7/mod.ts"; export type PackageManager = "npm" | "yarn" | "pnpm"; +const pms = ['npm', 'yarn', 'pnpm'] + export function usePackageManager() { const cwd = Deno.cwd(); const ref = creatLocalStorageRef(cwd); @@ -35,10 +38,17 @@ export function usePackageManager() { return isRunScript ? [pm, "run", ...Deno.args] : [pm, ...Deno.args]; } - return { ref, staging, getCommand }; + async function select() { + ref.value = await Select.prompt({ + message: 'select your package manager', + options: pms + }) as PackageManager + } + + return { ref, staging, getCommand, select }; } export function isPackageManager(v: string): v is PackageManager { // Check if the value of v is one of the package managers - return ["npm", "yarn", "pnpm"].includes(v); + return pms.includes(v); }