@@ -2,45 +2,93 @@ import * as path from "node:path";
22import  *  as  fs  from  "node:fs" ; 
33import  *  as  kl  from  "kolorist" ; 
44import  {  JsrPackage  }  from  "./utils" ; 
5- import  {  getPkgManager  }  from  "./pkg_manager" ; 
5+ import  {  Bun ,   PkgManagerName ,   getPkgManager  }  from  "./pkg_manager" ; 
66
7+ const  NPMRC_FILE  =  ".npmrc" ; 
8+ const  BUNFIG_FILE  =  "bunfig.toml" ; 
79const  JSR_NPMRC  =  `@jsr:registry=https://npm.jsr.io\n` ; 
10+ const  JSR_BUNFIG  =  `[install.scopes]\n"@jsr" = "https://npm.jsr.io/"\n` ; 
11+ 
12+ async  function  wrapWithStatus ( msg : string ,  fn : ( )  =>  Promise < void > )  { 
13+   process . stdout . write ( msg  +  "..." ) ; 
14+ 
15+   try  { 
16+     await  fn ( ) ; 
17+     process . stdout . write ( kl . green ( "ok" )  +  "\n" ) ; 
18+   }  catch  ( err )  { 
19+     process . stdout . write ( kl . red ( "error" )  +  "\n" ) ; 
20+     throw  err ; 
21+   } 
22+ } 
823
924export  async  function  setupNpmRc ( dir : string )  { 
10-   const  npmRcPath  =  path . join ( dir ,  ".npmrc" ) ; 
25+   const  npmRcPath  =  path . join ( dir ,  NPMRC_FILE ) ; 
26+   const  msg  =  `Setting up ${ NPMRC_FILE }  ; 
1127  try  { 
1228    let  content  =  await  fs . promises . readFile ( npmRcPath ,  "utf-8" ) ; 
1329    if  ( ! content . includes ( JSR_NPMRC ) )  { 
1430      content  +=  JSR_NPMRC ; 
15-       await  fs . promises . writeFile ( npmRcPath ,  content ) ; 
31+       await  wrapWithStatus ( msg ,  async  ( )  =>  { 
32+         await  fs . promises . writeFile ( npmRcPath ,  content ) ; 
33+       } ) ; 
34+     } 
35+   }  catch  ( err )  { 
36+     if  ( err  instanceof  Error  &&  ( err  as  any ) . code  ===  "ENOENT" )  { 
37+       await  wrapWithStatus ( msg ,  async  ( )  =>  { 
38+         await  fs . promises . writeFile ( npmRcPath ,  JSR_NPMRC ) ; 
39+       } ) ; 
40+     }  else  { 
41+       throw  err ; 
42+     } 
43+   } 
44+ } 
45+ 
46+ export  async  function  setupBunfigToml ( dir : string )  { 
47+   const  bunfigPath  =  path . join ( dir ,  BUNFIG_FILE ) ; 
48+   const  msg  =  `Setting up ${ BUNFIG_FILE }  ; 
49+   try  { 
50+     let  content  =  await  fs . promises . readFile ( bunfigPath ,  "utf-8" ) ; 
51+     if  ( ! / ^ " @ m y o r g 1 " \s + = / gm. test ( content ) )  { 
52+       content  +=  JSR_BUNFIG ; 
53+       await  wrapWithStatus ( msg ,  async  ( )  =>  { 
54+         await  fs . promises . writeFile ( bunfigPath ,  content ) ; 
55+       } ) ; 
1656    } 
1757  }  catch  ( err )  { 
1858    if  ( err  instanceof  Error  &&  ( err  as  any ) . code  ===  "ENOENT" )  { 
19-       await  fs . promises . writeFile ( npmRcPath ,  JSR_NPMRC ) ; 
59+       await  wrapWithStatus ( msg ,  async  ( )  =>  { 
60+         await  fs . promises . writeFile ( bunfigPath ,  JSR_BUNFIG ) ; 
61+       } ) ; 
2062    }  else  { 
2163      throw  err ; 
2264    } 
2365  } 
2466} 
2567
2668export  interface  BaseOptions  { 
27-   pkgManagerName : "npm"   |   "yarn"   |   "pnpm"  |  null ; 
69+   pkgManagerName : PkgManagerName  |  null ; 
2870} 
2971
3072export  interface  InstallOptions  extends  BaseOptions  { 
3173  mode : "dev"  |  "prod"  |  "optional" ; 
3274} 
3375
3476export  async  function  install ( packages : JsrPackage [ ] ,  options : InstallOptions )  { 
35-   console . log ( `Installing ${ kl . cyan ( packages . join ( ", " ) ) }  ) ; 
3677  const  pkgManager  =  await  getPkgManager ( process . cwd ( ) ,  options . pkgManagerName ) ; 
37-   await  setupNpmRc ( pkgManager . cwd ) ; 
3878
79+   if  ( pkgManager  instanceof  Bun )  { 
80+     // Bun doesn't support reading from .npmrc yet 
81+     await  setupBunfigToml ( pkgManager . cwd ) ; 
82+   }  else  { 
83+     await  setupNpmRc ( pkgManager . cwd ) ; 
84+   } 
85+ 
86+   console . log ( `Installing ${ kl . cyan ( packages . join ( ", " ) ) }  ) ; 
3987  await  pkgManager . install ( packages ,  options ) ; 
4088} 
4189
4290export  async  function  remove ( packages : JsrPackage [ ] ,  options : BaseOptions )  { 
43-   console . log ( `Removing ${ kl . cyan ( packages . join ( ", " ) ) }  ) ; 
4491  const  pkgManager  =  await  getPkgManager ( process . cwd ( ) ,  options . pkgManagerName ) ; 
92+   console . log ( `Removing ${ kl . cyan ( packages . join ( ", " ) ) }  ) ; 
4593  await  pkgManager . remove ( packages ) ; 
4694} 
0 commit comments