Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

feat ✨: (cli) Add snpm run <script> #26

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { benchmark } from "./benchmark.js";
import upgrade from "./upgrade.js";
import list from "./list.js";
import checkVersion from "../utils/checkVersion.js";
import run from "./run.js";

export async function commands(args: string[]) {
const [command, ...rest] = args;
Expand All @@ -27,6 +28,9 @@ export async function commands(args: string[]) {
case "ls":
await list(rest[0]);
break;
case "run":
await run(rest);
break;
default:
console.log(`Unknown command: ${command}`);
}
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,15 @@ export async function installPkg(
readFileSync(`${cacheFolder}/${downloadFile}`, "utf-8")
);

const thisPath = path.join(
process.cwd(),
"node_modules",
parent ? path.join(parent, "node_modules", manifest.name) : manifest.name
);

return await Promise.all(
cachedDeps.map(async (dep: any) => {
await installPkg(dep, manifest.name, spinner);
await installPkg(dep, thisPath, spinner);
})
);
} else {
Expand Down
54 changes: 54 additions & 0 deletions packages/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import path from "path";
import rpjf from "read-package-json-fast";
import ora from "ora";
import chalk from "chalk";
import { spawn } from "child_process";
import { readdirSync } from "fs";

export default async function run(args: Array<string>) {
const pkg = await rpjf(path.join(process.cwd(), "package.json"));
const { scripts } = pkg;
const script = scripts[args[0]];

// Get binaries from node_modules/.bin
const binPath = path.join(process.cwd(), "node_modules", ".bin");
const binaries = readdirSync(binPath);

if (!script) {
ora().fail(chalk.red(`No script found for ${args[0]}`));
// Show all scripts
ora().info(chalk.blue("Available scripts:"));
Object.keys(scripts).forEach((script) => {
ora().info(chalk.blue(script));
});
}

ora().info(chalk.blue(`Running ${chalk.grey(script)}...`));

// Separate scripts to run by &&
const scriptsToRun = script.split("&&").map((s: string) => s.trim());

// Run each script
scriptsToRun.forEach((script: string) => {
// Get the binary
const binary = script.split(" ")[0];

// Get the args
const binaryArgs = script.split(" ").slice(1);

// Check if the binary is in the node_modules/.bin
if (binaries.includes(binary)) {
// Run the binary
spawn(path.join(binPath, binary), binaryArgs, {
stdio: "inherit",
shell: true,
});
} else {
// Run the script
spawn(script, {
stdio: "inherit",
shell: true,
});
}
});
}