From 615699b16a5e70673734ac81ad428e7f3e79346e Mon Sep 17 00:00:00 2001 From: hunghg255 Date: Wed, 24 Jan 2024 18:09:04 +0700 Subject: [PATCH] chore: updaet ni --- package.json | 2 +- src/ni.ts | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 024ea0a..100318b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "run-script-cli", - "version": "0.0.13", + "version": "0.0.14", "license": "MIT", "main": "./dist/index.cjs", "module": "./dist/index.mjs", diff --git a/src/ni.ts b/src/ni.ts index c34236a..9335fb7 100644 --- a/src/ni.ts +++ b/src/ni.ts @@ -7,42 +7,55 @@ import { detectAgent } from '@skarab/detect-package-manager'; import { execaCommand } from 'execa'; // @ts-ignore import c from 'kleur'; -import { intro } from 'unprompts'; +import { cancel, isCancel, select, intro } from 'unprompts'; export const niCli = async (cwd: string = process.cwd(), argv = process.argv) => { try { const agent = await detectAgent(cwd); + + let nodeManager = agent?.name as string; let scriptValue: string = ''; - if (!agent?.name) { - return process.exit(0); + if (!nodeManager) { + nodeManager = (await select({ + message: c.bgCyan(' Choose npm manager '), + options: ['npm', 'pnpm', 'yarn', 'bun'].map((scriptItem) => ({ + label: `${c.yellow(scriptItem)}`, + value: scriptItem, + })), + })) as string; + + if (isCancel(nodeManager)) { + cancel('Choose npm manager cancelled'); + return process.exit(0); + } } if (argv?.length > 2) { scriptValue = argv.slice(2).join(' '); } - if (agent.name === 'bun') { + if (nodeManager === 'bun') { const cmd = scriptValue ? `bun add ${scriptValue}` : 'bun install'; intro(c.bold(c.green(`${cmd}\n`))); await execaCommand(`${cmd}`, { stdio: 'inherit', cwd }); } - if (agent.name === 'pnpm') { + if (nodeManager === 'pnpm') { const cmd = scriptValue ? `pnpm add ${scriptValue}` : 'pnpm install'; intro(c.bold(c.green(`${cmd}\n`))); await execaCommand(`${cmd}`, { stdio: 'inherit', cwd }); } - if (agent.name === 'npm') { + if (nodeManager === 'npm') { intro(c.bold(c.green(`npm install ${scriptValue}\n`))); await execaCommand(`npm install ${scriptValue}`, { stdio: 'inherit', cwd }); } - if (agent.name === 'yarn') { + if (nodeManager === 'yarn') { const cmd = scriptValue ? `yarn add ${scriptValue}` : 'yarn install'; intro(c.bold(c.green(`${cmd}\n`)));