@@ -2,45 +2,93 @@ import * as path from "node:path";
2
2
import * as fs from "node:fs" ;
3
3
import * as kl from "kolorist" ;
4
4
import { JsrPackage } from "./utils" ;
5
- import { getPkgManager } from "./pkg_manager" ;
5
+ import { Bun , PkgManagerName , getPkgManager } from "./pkg_manager" ;
6
6
7
+ const NPMRC_FILE = ".npmrc" ;
8
+ const BUNFIG_FILE = "bunfig.toml" ;
7
9
const 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
+ }
8
23
9
24
export 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 } ` ;
11
27
try {
12
28
let content = await fs . promises . readFile ( npmRcPath , "utf-8" ) ;
13
29
if ( ! content . includes ( JSR_NPMRC ) ) {
14
30
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
+ } ) ;
16
56
}
17
57
} catch ( err ) {
18
58
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
+ } ) ;
20
62
} else {
21
63
throw err ;
22
64
}
23
65
}
24
66
}
25
67
26
68
export interface BaseOptions {
27
- pkgManagerName : "npm" | "yarn" | "pnpm" | null ;
69
+ pkgManagerName : PkgManagerName | null ;
28
70
}
29
71
30
72
export interface InstallOptions extends BaseOptions {
31
73
mode : "dev" | "prod" | "optional" ;
32
74
}
33
75
34
76
export async function install ( packages : JsrPackage [ ] , options : InstallOptions ) {
35
- console . log ( `Installing ${ kl . cyan ( packages . join ( ", " ) ) } ...` ) ;
36
77
const pkgManager = await getPkgManager ( process . cwd ( ) , options . pkgManagerName ) ;
37
- await setupNpmRc ( pkgManager . cwd ) ;
38
78
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 ( ", " ) ) } ...` ) ;
39
87
await pkgManager . install ( packages , options ) ;
40
88
}
41
89
42
90
export async function remove ( packages : JsrPackage [ ] , options : BaseOptions ) {
43
- console . log ( `Removing ${ kl . cyan ( packages . join ( ", " ) ) } ...` ) ;
44
91
const pkgManager = await getPkgManager ( process . cwd ( ) , options . pkgManagerName ) ;
92
+ console . log ( `Removing ${ kl . cyan ( packages . join ( ", " ) ) } ...` ) ;
45
93
await pkgManager . remove ( packages ) ;
46
94
}
0 commit comments