Skip to content

Commit

Permalink
feat: run pkg scripts with or without run cmd (#21)
Browse files Browse the repository at this point in the history
* feat: run pkg scripts with or without run cmd

* format code
  • Loading branch information
chaqchase authored Mar 1, 2024
1 parent a08c881 commit 4b53471
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ commands.
- `add`, `install`, `i`: Adds a JSR package to your project.
- `remove`, `uninstall`, `r`: Remove a JSR package from your project.
- `publish`: Publish `package.json` libraries to JSR.
- `run <script>`: Run a JSR package script.
- `<script>`: Run a JSR package script without `run` command.

## Limitations

Expand Down
40 changes: 33 additions & 7 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as kl from "kolorist";
import * as fs from "node:fs";
import * as path from "node:path";
import { parseArgs } from "node:util";
import { install, publish, remove } from "./commands";
import { install, publish, remove, runScript } from "./commands";
import { JsrPackage, JsrPackageNameError, prettyTime, setDebug } from "./utils";
import { PkgManagerName } from "./pkg_manager";

Expand Down Expand Up @@ -39,6 +39,8 @@ ${
Commands:
${
prettyPrintRow([
["<script>", "Run a script from the package.json file"],
["run <script>", "Run a script from the package.json file"],
["i, install, add", "Install one or more JSR packages."],
["r, uninstall, remove", "Remove one or more JSR packages."],
["publish", "Publish a package to the JSR registry."],
Expand Down Expand Up @@ -120,8 +122,9 @@ if (args.length === 0) {
// frequently.
if (
cmd === "publish" &&
!args.some((arg) =>
arg === "-h" || arg === "--help" || arg === "--version" || arg === "-v"
!args.some(
(arg) =>
arg === "-h" || arg === "--help" || arg === "--version" || arg === "-v",
)
) {
const binFolder = path.join(__dirname, "..", ".download");
Expand Down Expand Up @@ -200,11 +203,34 @@ if (args.length === 0) {
const packages = getPackages(options.positionals);
await remove(packages, { pkgManagerName });
});
} else if (cmd === "run") {
const script = options.positionals[1];
if (!script) {
console.error(kl.red(`Missing script argument.`));
console.log();
printHelp();
process.exit(1);
}
run(async () => {
await runScript(process.cwd(), script, { pkgManagerName });
});
} else {
console.error(kl.red(`Unknown command: ${cmd}`));
console.log();
printHelp();
process.exit(1);
const packageJsonPath = path.join(process.cwd(), "package.json");
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(
fs.readFileSync(packageJsonPath, "utf-8"),
);
if (packageJson.scripts && packageJson.scripts[cmd]) {
run(async () => {
await runScript(process.cwd(), cmd, { pkgManagerName });
});
} else {
console.error(kl.red(`Unknown command: ${cmd}`));
console.log();
printHelp();
process.exit(1);
}
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,12 @@ export async function publish(cwd: string, options: PublishOptions) {
];
await exec(binPath, args, cwd);
}

export async function runScript(
cwd: string,
script: string,
options: BaseOptions,
) {
const pkgManager = await getPkgManager(process.cwd(), options.pkgManagerName);
await pkgManager.runScript(script);
}
17 changes: 17 additions & 0 deletions src/pkg_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface PackageManager {
cwd: string;
install(packages: JsrPackage[], options: InstallOptions): Promise<void>;
remove(packages: JsrPackage[]): Promise<void>;
runScript(script: string): Promise<void>;
}

class Npm implements PackageManager {
Expand All @@ -49,6 +50,10 @@ class Npm implements PackageManager {
this.cwd,
);
}

async runScript(script: string) {
await execWithLog("npm", ["run", script], this.cwd);
}
}

class Yarn implements PackageManager {
Expand All @@ -71,6 +76,10 @@ class Yarn implements PackageManager {
this.cwd,
);
}

async runScript(script: string) {
await execWithLog("yarn", [script], this.cwd);
}
}

class Pnpm implements PackageManager {
Expand All @@ -93,6 +102,10 @@ class Pnpm implements PackageManager {
this.cwd,
);
}

async runScript(script: string) {
await execWithLog("pnpm", [script], this.cwd);
}
}

export class Bun implements PackageManager {
Expand All @@ -115,6 +128,10 @@ export class Bun implements PackageManager {
this.cwd,
);
}

async runScript(script: string) {
await execWithLog("bun", ["run", script], this.cwd);
}
}

export type PkgManagerName = "npm" | "yarn" | "pnpm" | "bun";
Expand Down
36 changes: 36 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,39 @@ describe("publish", () => {
});
}
});

describe("run", () => {
it("should run a script", async () => {
await runInTempDir(async (dir) => {
const pkgJsonPath = path.join(dir, "package.json");
const pkgJson = await readJson<PkgJson>(pkgJsonPath);
pkgJson.scripts = {
test: 'echo "test"',
};
await fs.promises.writeFile(
pkgJsonPath,
JSON.stringify(pkgJson),
"utf-8",
);

await runJsr(["run", "test"], dir);
});
});

it("should run a script without the run command", async () => {
await runInTempDir(async (dir) => {
const pkgJsonPath = path.join(dir, "package.json");
const pkgJson = await readJson<PkgJson>(pkgJsonPath);
pkgJson.scripts = {
test: 'echo "test"',
};
await fs.promises.writeFile(
pkgJsonPath,
JSON.stringify(pkgJson),
"utf-8",
);

await runJsr(["test"], dir);
});
});
});
1 change: 1 addition & 0 deletions test/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface PkgJson {
devDependencies?: Record<string, string>;
optionalDependencies?: Record<string, string>;
exports?: string | Record<string, string | Record<string, string>>;
scripts?: Record<string, string>;
}

export interface DenoJson {
Expand Down

0 comments on commit 4b53471

Please sign in to comment.