22// @ts -check
33
44const child_process = require ( "child_process" ) ;
5+ const fs = require ( "fs" ) ;
6+ const path = require ( "path" ) ;
7+ const os = require ( "os" ) ;
58
69const username = process . argv [ 2 ] ;
710const password = process . argv [ 3 ] ;
@@ -23,18 +26,54 @@ if (!email) {
2326 process . exit ( 1 ) ;
2427}
2528
26- const child = child_process . exec ( `npm adduser${ registry ? ( ' --registry ' + registry ) :'' } ` ) ;
27-
28- child . stdout . on ( "data" , d => {
29- const data = d . toString ( ) ;
30- process . stdout . write ( d + "\n" ) ;
31- if ( data . match ( / u s e r n a m e / i) ) {
32- child . stdin . write ( username + "\n" ) ;
33- } else if ( data . match ( / p a s s w o r d / i) ) {
34- child . stdin . write ( password + "\n" ) ;
35- } else if ( data . match ( / e m a i l / i) ) {
36- child . stdin . write ( email + "\n" ) ;
37- } else if ( data . match ( / l o g g e d i n a s / i) ) {
38- child . stdin . end ( ) ;
39- }
29+ const registryUrl = registry || "http://localhost:4873" ;
30+
31+ // First set the registry
32+ console . log ( `Setting npm registry to ${ registryUrl } ` ) ;
33+ const setRegistry = child_process . spawnSync ( 'npm' , [ 'config' , 'set' , 'registry' , registryUrl ] , {
34+ stdio : 'inherit' ,
35+ shell : true
4036} ) ;
37+
38+ if ( setRegistry . status !== 0 ) {
39+ console . error ( 'Failed to set registry' ) ;
40+ process . exit ( 1 ) ;
41+ }
42+
43+ // Create auth token for verdaccio
44+ const authString = Buffer . from ( `${ username } :${ password } ` ) . toString ( 'base64' ) ;
45+ const registryPath = registryUrl . replace ( / ^ h t t p s ? : / , '' ) ;
46+
47+ // Set auth in npm config
48+ console . log ( 'Setting authentication...' ) ;
49+ const setAuth = child_process . spawnSync ( 'npm' , [ 'config' , 'set' , `${ registryPath } /:_auth` , authString ] , {
50+ stdio : 'inherit' ,
51+ shell : true
52+ } ) ;
53+
54+ if ( setAuth . status !== 0 ) {
55+ console . error ( 'Failed to set auth' ) ;
56+ process . exit ( 1 ) ;
57+ }
58+
59+ // Set email
60+ const setEmail = child_process . spawnSync ( 'npm' , [ 'config' , 'set' , 'email' , email ] , {
61+ stdio : 'inherit' ,
62+ shell : true
63+ } ) ;
64+
65+ // Verify authentication
66+ console . log ( 'Verifying authentication...' ) ;
67+ const whoami = child_process . spawnSync ( 'npm' , [ 'whoami' , '--registry' , registryUrl ] , {
68+ encoding : 'utf8' ,
69+ shell : true
70+ } ) ;
71+
72+ if ( whoami . status === 0 && whoami . stdout . trim ( ) ) {
73+ console . log ( `Logged in as ${ whoami . stdout . trim ( ) } on ${ registryUrl } ` ) ;
74+ process . exit ( 0 ) ;
75+ } else {
76+ console . error ( 'Authentication verification failed' ) ;
77+ if ( whoami . stderr ) console . error ( 'Error:' , whoami . stderr ) ;
78+ process . exit ( 1 ) ;
79+ }
0 commit comments