1- import { spawn } from 'node:child_process' ;
1+ import { fork } from 'node:child_process' ;
22import chalk from 'chalk' ;
33import coffee from 'coffee' ;
44import open from 'open' ;
55import stripAnsi from 'strip-ansi' ;
6- import {
7- afterAll ,
8- afterEach ,
9- beforeAll ,
10- beforeEach ,
11- describe ,
12- expect ,
13- it ,
14- vi ,
15- } from 'vitest' ;
6+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
167
178import { unlink } from 'node:fs/promises' ;
9+ import path from 'node:path' ;
1810import { onHome , onTest } from '../src/actions' ;
1911import { NPMRC , NRMRC , REGISTRIES } from '../src/constants' ;
2012import { isUnicodeSupported , readFile , writeFile } from '../src/helpers' ;
2113
22- const isWin = process . platform === 'win32' ;
23-
2414const shouldUseMain = isUnicodeSupported ( ) ;
25-
2615const pointer = shouldUseMain ? '❯' : '>' ;
2716const radioOff = shouldUseMain ? '◯' : '( )' ;
2817const radioOn = shouldUseMain ? '◉' : '(*)' ;
18+ const LOCAL_NRM = path . resolve ( __dirname , '../dist/index.js' ) ;
2919
3020vi . setConfig ( {
3121 testTimeout : 20000 ,
@@ -51,34 +41,15 @@ vi.mock('undici', () => {
5141
5242vi . stubGlobal ( '__NRM_VERSION__' , null ) ;
5343vi . stubGlobal ( '__REGISTRY__' , null ) ;
54- beforeAll ( async ( ) => {
55- const { stdout } = await coffee . spawn ( 'nrm' , [ '-V' ] , { shell : isWin } ) . end ( ) ;
56- global . __NRM_VERSION__ = stdout ? stdout : null ;
57- await coffee . spawn ( 'npm' , [ 'link' ] , { shell : isWin } ) . end ( ) ;
58- } ) ;
59-
60- afterAll ( async ( ) => {
61- await coffee . spawn ( 'npm' , [ 'unlink' , 'nrm' , '-g' ] , { shell : isWin } ) . end ( ) ;
62-
63- if ( global . __NRM_VERSION__ !== null ) {
64- await coffee
65- . spawn ( 'npm' , [ `install -g nrm@${ global . __NRM_VERSION__ } ` ] , {
66- shell : isWin ,
67- } )
68- . end ( ) ;
69- }
70- } ) ;
7144
7245it ( 'nrm ls' , async ( ) => {
7346 await coffee
74- . spawn ( 'nrm' , [ 'use' , 'cnpm' ] , { shell : isWin } )
47+ . fork ( LOCAL_NRM , [ 'use' , 'cnpm' ] )
7548 . expect ( 'stdout' , / T h e r e g i s t r y h a s b e e n c h a n g e d t o ' c n p m ' / g)
7649 . expect ( 'code' , 0 )
7750 . end ( ) ;
7851
79- const { stdout, code } = await coffee
80- . spawn ( 'nrm' , [ 'ls' ] , { shell : isWin } )
81- . end ( ) ;
52+ const { stdout, code } = await coffee . fork ( LOCAL_NRM , [ 'ls' ] ) . end ( ) ;
8253
8354 const match = `${ chalk . green . bold ( '* ' ) } cnpm` ;
8455
@@ -88,15 +59,15 @@ it('nrm ls', async () => {
8859
8960it ( 'nrm use <registry>' , async ( ) => {
9061 await coffee
91- . spawn ( 'nrm' , [ 'use' , 'cnpm' ] , { shell : isWin } )
62+ . fork ( LOCAL_NRM , [ 'use' , 'cnpm' ] )
9263 . expect ( 'stdout' , / T h e r e g i s t r y h a s b e e n c h a n g e d t o ' c n p m ' / g)
9364 . expect ( 'code' , 0 )
9465 . end ( ) ;
9566} ) ;
9667
9768it ( 'nrm use <registry> local' , async ( ) => {
9869 await coffee
99- . spawn ( 'nrm' , [ 'use' , 'cnpm' , 'local' ] , { shell : isWin } )
70+ . fork ( LOCAL_NRM , [ 'use' , 'cnpm' , 'local' ] )
10071 . expect ( 'stdout' , / T h e r e g i s t r y h a s b e e n c h a n g e d t o ' c n p m ' / g)
10172 . expect ( 'code' , 0 )
10273 . end ( ) ;
@@ -106,7 +77,7 @@ it('nrm use <registry> local', async () => {
10677 expect ( npmrc . registry ) . toBe ( REGISTRIES . cnpm . registry ) ;
10778
10879 await coffee
109- . spawn ( 'nrm' , [ 'current' ] , { shell : isWin } )
80+ . fork ( LOCAL_NRM , [ 'current' ] )
11081 . expect ( 'stdout' , / c n p m / g)
11182 . expect ( 'code' , 0 )
11283 . end ( ) ;
@@ -116,7 +87,7 @@ it('nrm use <registry> local with user config', async () => {
11687 await writeFile ( NPMRC , { abc : '123' } ) ;
11788
11889 await coffee
119- . spawn ( 'nrm' , [ 'use' , 'cnpm' , 'local' ] , { shell : isWin } )
90+ . fork ( LOCAL_NRM , [ 'use' , 'cnpm' , 'local' ] )
12091 . expect ( 'stdout' , / T h e r e g i s t r y h a s b e e n c h a n g e d t o ' c n p m ' / g)
12192 . expect ( 'code' , 0 )
12293 . end ( ) ;
@@ -127,21 +98,20 @@ it('nrm use <registry> local with user config', async () => {
12798 expect ( npmrc . abc ) . toBe ( '123' ) ;
12899
129100 await coffee
130- . spawn ( 'nrm' , [ 'current' ] , { shell : isWin } )
101+ . fork ( LOCAL_NRM , [ 'current' ] )
131102 . expect ( 'stdout' , / c n p m / g)
132103 . expect ( 'code' , 0 )
133104 . end ( ) ;
134105} ) ;
135106
136107it ( 'nrm use without argument' , async ( ) => {
137- const { stdout } = spawn ( 'nrm' , [ 'use' ] , { shell : isWin } ) ;
108+ const { stdout } = fork ( LOCAL_NRM , [ 'use' ] , { stdio : 'pipe' } ) ;
138109
139110 const message = await new Promise ( ( resolve ) => {
140- stdout . on ( 'data' , ( data ) => {
111+ stdout ? .on ( 'data' , ( data ) => {
141112 resolve ( stripAnsi ( data . toString ( ) ) . trim ( ) ) ;
142113 } ) ;
143114 } ) ;
144-
145115 expect (
146116 message ,
147117 ) . toBe ( `? Please select the registry you want to use (Use arrow keys)
@@ -156,13 +126,13 @@ ${pointer} npm
156126
157127it ( 'nrm current' , async ( ) => {
158128 await coffee
159- . spawn ( 'nrm' , [ 'use' , 'cnpm' ] , { shell : isWin } )
129+ . fork ( LOCAL_NRM , [ 'use' , 'cnpm' ] )
160130 . expect ( 'stdout' , / T h e r e g i s t r y h a s b e e n c h a n g e d t o ' c n p m ' / g)
161131 . expect ( 'code' , 0 )
162132 . end ( ) ;
163133
164134 await coffee
165- . spawn ( 'nrm' , [ 'current' ] , { shell : isWin } )
135+ . fork ( LOCAL_NRM , [ 'current' ] )
166136 . expect ( 'stdout' , / c n p m / g)
167137 . expect ( 'code' , 0 )
168138 . end ( ) ;
@@ -176,15 +146,15 @@ describe('nrm command which needs to add a custom registry', () => {
176146 __REGISTRY__ = customName ;
177147
178148 await coffee
179- . spawn ( 'nrm' , [ 'add' , `${ __REGISTRY__ } ` , `${ url } ` ] , { shell : isWin } )
149+ . fork ( LOCAL_NRM , [ 'add' , `${ __REGISTRY__ } ` , `${ url } ` ] )
180150 . expect ( 'stdout' , / s u c c e s s / g)
181151 . expect ( 'code' , 0 )
182152 . end ( ) ;
183153 } ) ;
184154
185155 afterEach ( async ( ) => {
186156 await coffee
187- . spawn ( 'nrm' , [ 'del' , `${ __REGISTRY__ } ` ] , { shell : isWin } )
157+ . fork ( LOCAL_NRM , [ 'del' , `${ __REGISTRY__ } ` ] )
188158 . expect ( 'stdout' , / h a s b e e n d e l e t e d s u c c e s s f u l l y / g)
189159 . expect ( 'code' , 0 )
190160 . end ( ) ;
@@ -199,7 +169,7 @@ describe('nrm command which needs to add a custom registry', () => {
199169 ) ;
200170
201171 await coffee
202- . spawn ( 'nrm' , [ 'rename' , `${ customName } ` , `${ newName } ` ] , { shell : isWin } )
172+ . fork ( LOCAL_NRM , [ 'rename' , `${ customName } ` , `${ newName } ` ] )
203173 . expect ( 'stdout' , match )
204174 . expect ( 'code' , 0 )
205175 . end ( ) ;
@@ -210,11 +180,14 @@ describe('nrm command which needs to add a custom registry', () => {
210180 const value = 'value' ;
211181
212182 await coffee
213- . spawn (
214- 'nrm' ,
215- [ 'set' , `${ __REGISTRY__ } ` , '-a' , `${ attr } ` , '-v' , `${ value } ` ] ,
216- { shell : isWin } ,
217- )
183+ . fork ( LOCAL_NRM , [
184+ 'set' ,
185+ `${ __REGISTRY__ } ` ,
186+ '-a' ,
187+ `${ attr } ` ,
188+ '-v' ,
189+ `${ value } ` ,
190+ ] )
218191 . expect ( 'stdout' , / s u c c e s s f u l l y / g)
219192 . expect ( 'code' , 0 )
220193 . end ( ) ;
@@ -232,13 +205,13 @@ describe('nrm command which needs to add a custom registry', () => {
232205 const url = 'https://scope.example.org' ;
233206
234207 await coffee
235- . spawn ( 'nrm' , [ 'set-scope' , `${ scopeName } ` , `${ url } ` ] , { shell : isWin } )
208+ . fork ( LOCAL_NRM , [ 'set-scope' , `${ scopeName } ` , `${ url } ` ] )
236209 . expect ( 'stdout' , / s u c c e s s / g)
237210 . expect ( 'code' , 0 )
238211 . end ( ) ;
239212
240213 await coffee
241- . spawn ( 'nrm' , [ 'del-scope' , `${ scopeName } ` ] , { shell : isWin } )
214+ . fork ( LOCAL_NRM , [ 'del-scope' , `${ scopeName } ` ] )
242215 . expect ( 'stdout' , / s u c c e s s / g)
243216 . expect ( 'code' , 0 )
244217 . end ( ) ;
@@ -252,9 +225,7 @@ describe('nrm command which needs to add a custom registry', () => {
252225 ) ;
253226
254227 await coffee
255- . spawn ( 'nrm' , [ 'set-hosted-repo' , `${ __REGISTRY__ } ` , `${ repo } ` ] , {
256- shell : isWin ,
257- } )
228+ . fork ( LOCAL_NRM , [ 'set-hosted-repo' , `${ __REGISTRY__ } ` , `${ repo } ` ] )
258229 . expect ( 'stdout' , match )
259230 . expect ( 'code' , 0 )
260231 . end ( ) ;
@@ -265,17 +236,20 @@ describe('nrm command which needs to add a custom registry', () => {
265236 const password = 'password' ;
266237
267238 await coffee
268- . spawn (
269- 'nrm' ,
270- [ 'login' , `${ __REGISTRY__ } ` , '-u' , `${ username } ` , '-p' , `${ password } ` ] ,
271- { shell : isWin } ,
272- )
239+ . fork ( LOCAL_NRM , [
240+ 'login' ,
241+ `${ __REGISTRY__ } ` ,
242+ '-u' ,
243+ `${ username } ` ,
244+ '-p' ,
245+ `${ password } ` ,
246+ ] )
273247 . expect ( 'stdout' , / s u c c e s s / g)
274248 . expect ( 'code' , 0 )
275249 . end ( ) ;
276250
277251 await coffee
278- . spawn ( 'nrm' , [ 'login' , `${ __REGISTRY__ } ` ] , { shell : isWin } )
252+ . fork ( LOCAL_NRM , [ 'login' , `${ __REGISTRY__ } ` ] )
279253 . expect (
280254 'stderr' ,
281255 / A u t h o r i z a t i o n i n f o r m a t i o n i n b a s e 6 4 f o r m a t o r u s e r n a m e & p a s s w o r d i s r e q u i r e d / g,
@@ -298,9 +272,7 @@ describe('nrm delete without argument (use keyword to select delete)', () => {
298272 beforeEach ( async ( ) => {
299273 for ( const registry of registries ) {
300274 await coffee
301- . spawn ( 'nrm' , [ 'add' , `${ registry . name } ` , `${ registry . url } ` ] , {
302- shell : isWin ,
303- } )
275+ . fork ( LOCAL_NRM , [ 'add' , `${ registry . name } ` , `${ registry . url } ` ] )
304276 . expect ( 'stdout' , / s u c c e s s / g)
305277 . expect ( 'code' , 0 )
306278 . end ( ) ;
@@ -312,10 +284,10 @@ describe('nrm delete without argument (use keyword to select delete)', () => {
312284 } ) ;
313285
314286 it ( 'nrm delete' , async ( ) => {
315- const { stdout } = spawn ( 'nrm' , [ 'del' ] , { shell : isWin } ) ;
287+ const { stdout } = fork ( LOCAL_NRM , [ 'del' ] , { stdio : 'pipe' } ) ;
316288
317289 const message = await new Promise ( ( resolve ) => {
318- stdout . on ( 'data' , ( data ) => {
290+ stdout ? .on ( 'data' , ( data ) => {
319291 resolve ( stripAnsi ( data . toString ( ) ) . trim ( ) ) ;
320292 } ) ;
321293 } ) ;
@@ -330,12 +302,12 @@ describe('nrm delete without argument (use keyword to select delete)', () => {
330302 } ) ;
331303
332304 it ( 'nrm delete (with keyword input)' , async ( ) => {
333- const { stdout, stdin } = spawn ( 'nrm' , [ 'del' ] , { shell : isWin } ) ;
334- stdin . write ( '\u001b[B' ) ;
305+ const { stdout, stdin } = fork ( LOCAL_NRM , [ 'del' ] , { stdio : 'pipe' } ) ;
306+ stdin ? .write ( '\u001b[B' ) ;
335307
336308 const message = await new Promise ( ( resolve ) => {
337309 const m : string [ ] = [ ] ;
338- stdout . on ( 'data' , ( data ) => {
310+ stdout ? .on ( 'data' , ( data ) => {
339311 m . push ( stripAnsi ( data . toString ( ) ) . trim ( ) ) ;
340312 // get the last output
341313 if ( m . length === 2 ) {
0 commit comments