Skip to content

Commit

Permalink
chore: updaet ni
Browse files Browse the repository at this point in the history
  • Loading branch information
hunghg255 committed Jan 24, 2024
1 parent 201b0d6 commit 615699b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
27 changes: 20 additions & 7 deletions src/ni.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)));
Expand Down

0 comments on commit 615699b

Please sign in to comment.