Skip to content

Commit 4b53471

Browse files
authored
feat: run pkg scripts with or without run cmd (#21)
* feat: run pkg scripts with or without run cmd * format code
1 parent a08c881 commit 4b53471

File tree

6 files changed

+98
-7
lines changed

6 files changed

+98
-7
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ commands.
4545
- `add`, `install`, `i`: Adds a JSR package to your project.
4646
- `remove`, `uninstall`, `r`: Remove a JSR package from your project.
4747
- `publish`: Publish `package.json` libraries to JSR.
48+
- `run <script>`: Run a JSR package script.
49+
- `<script>`: Run a JSR package script without `run` command.
4850

4951
## Limitations
5052

src/bin.ts

+33-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as kl from "kolorist";
44
import * as fs from "node:fs";
55
import * as path from "node:path";
66
import { parseArgs } from "node:util";
7-
import { install, publish, remove } from "./commands";
7+
import { install, publish, remove, runScript } from "./commands";
88
import { JsrPackage, JsrPackageNameError, prettyTime, setDebug } from "./utils";
99
import { PkgManagerName } from "./pkg_manager";
1010

@@ -39,6 +39,8 @@ ${
3939
Commands:
4040
${
4141
prettyPrintRow([
42+
["<script>", "Run a script from the package.json file"],
43+
["run <script>", "Run a script from the package.json file"],
4244
["i, install, add", "Install one or more JSR packages."],
4345
["r, uninstall, remove", "Remove one or more JSR packages."],
4446
["publish", "Publish a package to the JSR registry."],
@@ -120,8 +122,9 @@ if (args.length === 0) {
120122
// frequently.
121123
if (
122124
cmd === "publish" &&
123-
!args.some((arg) =>
124-
arg === "-h" || arg === "--help" || arg === "--version" || arg === "-v"
125+
!args.some(
126+
(arg) =>
127+
arg === "-h" || arg === "--help" || arg === "--version" || arg === "-v",
125128
)
126129
) {
127130
const binFolder = path.join(__dirname, "..", ".download");
@@ -200,11 +203,34 @@ if (args.length === 0) {
200203
const packages = getPackages(options.positionals);
201204
await remove(packages, { pkgManagerName });
202205
});
206+
} else if (cmd === "run") {
207+
const script = options.positionals[1];
208+
if (!script) {
209+
console.error(kl.red(`Missing script argument.`));
210+
console.log();
211+
printHelp();
212+
process.exit(1);
213+
}
214+
run(async () => {
215+
await runScript(process.cwd(), script, { pkgManagerName });
216+
});
203217
} else {
204-
console.error(kl.red(`Unknown command: ${cmd}`));
205-
console.log();
206-
printHelp();
207-
process.exit(1);
218+
const packageJsonPath = path.join(process.cwd(), "package.json");
219+
if (fs.existsSync(packageJsonPath)) {
220+
const packageJson = JSON.parse(
221+
fs.readFileSync(packageJsonPath, "utf-8"),
222+
);
223+
if (packageJson.scripts && packageJson.scripts[cmd]) {
224+
run(async () => {
225+
await runScript(process.cwd(), cmd, { pkgManagerName });
226+
});
227+
} else {
228+
console.error(kl.red(`Unknown command: ${cmd}`));
229+
console.log();
230+
printHelp();
231+
process.exit(1);
232+
}
233+
}
208234
}
209235
}
210236
}

src/commands.ts

+9
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,12 @@ export async function publish(cwd: string, options: PublishOptions) {
145145
];
146146
await exec(binPath, args, cwd);
147147
}
148+
149+
export async function runScript(
150+
cwd: string,
151+
script: string,
152+
options: BaseOptions,
153+
) {
154+
const pkgManager = await getPkgManager(process.cwd(), options.pkgManagerName);
155+
await pkgManager.runScript(script);
156+
}

src/pkg_manager.ts

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface PackageManager {
2626
cwd: string;
2727
install(packages: JsrPackage[], options: InstallOptions): Promise<void>;
2828
remove(packages: JsrPackage[]): Promise<void>;
29+
runScript(script: string): Promise<void>;
2930
}
3031

3132
class Npm implements PackageManager {
@@ -49,6 +50,10 @@ class Npm implements PackageManager {
4950
this.cwd,
5051
);
5152
}
53+
54+
async runScript(script: string) {
55+
await execWithLog("npm", ["run", script], this.cwd);
56+
}
5257
}
5358

5459
class Yarn implements PackageManager {
@@ -71,6 +76,10 @@ class Yarn implements PackageManager {
7176
this.cwd,
7277
);
7378
}
79+
80+
async runScript(script: string) {
81+
await execWithLog("yarn", [script], this.cwd);
82+
}
7483
}
7584

7685
class Pnpm implements PackageManager {
@@ -93,6 +102,10 @@ class Pnpm implements PackageManager {
93102
this.cwd,
94103
);
95104
}
105+
106+
async runScript(script: string) {
107+
await execWithLog("pnpm", [script], this.cwd);
108+
}
96109
}
97110

98111
export class Bun implements PackageManager {
@@ -115,6 +128,10 @@ export class Bun implements PackageManager {
115128
this.cwd,
116129
);
117130
}
131+
132+
async runScript(script: string) {
133+
await execWithLog("bun", ["run", script], this.cwd);
134+
}
118135
}
119136

120137
export type PkgManagerName = "npm" | "yarn" | "pnpm" | "bun";

test/commands.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,39 @@ describe("publish", () => {
324324
});
325325
}
326326
});
327+
328+
describe("run", () => {
329+
it("should run a script", async () => {
330+
await runInTempDir(async (dir) => {
331+
const pkgJsonPath = path.join(dir, "package.json");
332+
const pkgJson = await readJson<PkgJson>(pkgJsonPath);
333+
pkgJson.scripts = {
334+
test: 'echo "test"',
335+
};
336+
await fs.promises.writeFile(
337+
pkgJsonPath,
338+
JSON.stringify(pkgJson),
339+
"utf-8",
340+
);
341+
342+
await runJsr(["run", "test"], dir);
343+
});
344+
});
345+
346+
it("should run a script without the run command", async () => {
347+
await runInTempDir(async (dir) => {
348+
const pkgJsonPath = path.join(dir, "package.json");
349+
const pkgJson = await readJson<PkgJson>(pkgJsonPath);
350+
pkgJson.scripts = {
351+
test: 'echo "test"',
352+
};
353+
await fs.promises.writeFile(
354+
pkgJsonPath,
355+
JSON.stringify(pkgJson),
356+
"utf-8",
357+
);
358+
359+
await runJsr(["test"], dir);
360+
});
361+
});
362+
});

test/test_utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface PkgJson {
1212
devDependencies?: Record<string, string>;
1313
optionalDependencies?: Record<string, string>;
1414
exports?: string | Record<string, string | Record<string, string>>;
15+
scripts?: Record<string, string>;
1516
}
1617

1718
export interface DenoJson {

0 commit comments

Comments
 (0)