@@ -4,7 +4,7 @@ import * as kl from "kolorist";
4
4
import * as fs from "node:fs" ;
5
5
import * as path from "node:path" ;
6
6
import { parseArgs } from "node:util" ;
7
- import { install , publish , remove } from "./commands" ;
7
+ import { install , publish , remove , runScript } from "./commands" ;
8
8
import { JsrPackage , JsrPackageNameError , prettyTime , setDebug } from "./utils" ;
9
9
import { PkgManagerName } from "./pkg_manager" ;
10
10
39
39
Commands:
40
40
${
41
41
prettyPrintRow ( [
42
+ [ "<script>" , "Run a script from the package.json file" ] ,
43
+ [ "run <script>" , "Run a script from the package.json file" ] ,
42
44
[ "i, install, add" , "Install one or more JSR packages." ] ,
43
45
[ "r, uninstall, remove" , "Remove one or more JSR packages." ] ,
44
46
[ "publish" , "Publish a package to the JSR registry." ] ,
@@ -120,8 +122,9 @@ if (args.length === 0) {
120
122
// frequently.
121
123
if (
122
124
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" ,
125
128
)
126
129
) {
127
130
const binFolder = path . join ( __dirname , ".." , ".download" ) ;
@@ -200,11 +203,34 @@ if (args.length === 0) {
200
203
const packages = getPackages ( options . positionals ) ;
201
204
await remove ( packages , { pkgManagerName } ) ;
202
205
} ) ;
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
+ } ) ;
203
217
} 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
+ }
208
234
}
209
235
}
210
236
}
0 commit comments