@@ -4,7 +4,7 @@ import * as kl from "kolorist";
44import * as fs from "node:fs" ;
55import * as path from "node:path" ;
66import { parseArgs } from "node:util" ;
7- import { install , publish , remove } from "./commands" ;
7+ import { install , publish , remove , runScript } from "./commands" ;
88import { JsrPackage , JsrPackageNameError , prettyTime , setDebug } from "./utils" ;
99import { PkgManagerName } from "./pkg_manager" ;
1010
3939Commands:
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}
0 commit comments