1- import { spawn } from "child_process" ;
1+ import { exec } from "child_process" ;
22import * as path from "path" ;
33import { TemplateManager } from "../../cli/lib/TemplateManager" ;
44import { Config , FS_TOKEN , IFileSystem , ProjectTemplate } from "../types" ;
@@ -88,9 +88,8 @@ export class PackageManager {
8888 const config = ProjectConfig . localConfig ( ) ;
8989 if ( ! config . packagesInstalled ) {
9090 let command : string ;
91- let managerCommand : string ;
91+ const managerCommand = this . getManager ( ) ;
9292
93- managerCommand = this . getManager ( ) ;
9493 switch ( managerCommand ) {
9594 case "npm" :
9695 /* passes through */
@@ -123,49 +122,52 @@ export class PackageManager {
123122 }
124123
125124 public static removePackage ( packageName : string , verbose : boolean = false ) : boolean {
125+ let command : string ;
126126 const managerCommand = this . getManager ( ) ;
127- let args : string [ ] ;
127+ const sanitizePackage = Util . sanitizeShellArg ( packageName ) ;
128128 switch ( managerCommand ) {
129129 case "npm" :
130130 /* passes through */
131131 default :
132- args = [ ' uninstall' , packageName , ' --quiet' , ' --save' ] ;
132+ command = ` ${ managerCommand } uninstall ${ sanitizePackage } --quiet --save` ;
133133 break ;
134134 }
135135 try {
136136 // tslint:disable-next-line:object-literal-sort-keys
137- Util . spawnSync ( managerCommand , args , { stdio : "pipe" , encoding : "utf8" } ) ;
137+ Util . execSync ( command , { stdio : "pipe" , encoding : "utf8" } ) ;
138138 } catch ( error ) {
139- Util . log ( `Error uninstalling package ${ packageName } with ${ managerCommand } ` ) ;
139+ Util . log ( `Error uninstalling package ${ sanitizePackage } with ${ managerCommand } ` ) ;
140140 if ( verbose ) {
141141 Util . log ( error . message ) ;
142142 }
143143 return false ;
144144 }
145145
146- Util . log ( `Package ${ packageName } uninstalled successfully` ) ;
146+ Util . log ( `Package ${ sanitizePackage } uninstalled successfully` ) ;
147147 return true ;
148148 }
149149
150150 public static addPackage ( packageName : string , verbose : boolean = false ) : boolean {
151151 const managerCommand = this . getManager ( ) ;
152- const args = this . getInstallArgs ( packageName ) ;
152+ const sanitizePackage = Util . sanitizeShellArg ( packageName ) ;
153+ const command = this . getInstallCommand ( managerCommand , sanitizePackage ) ;
153154 try {
154155 // tslint:disable-next-line:object-literal-sort-keys
155- Util . spawnSync ( managerCommand , args , { stdio : "pipe" , encoding : "utf8" } ) ;
156+ Util . execSync ( command , { stdio : "pipe" , encoding : "utf8" } ) ;
156157 } catch ( error ) {
157- Util . log ( `Error installing package ${ packageName } with ${ managerCommand } ` ) ;
158+ Util . log ( `Error installing package ${ sanitizePackage } with ${ managerCommand } ` ) ;
158159 if ( verbose ) {
159160 Util . log ( error . message ) ;
160161 }
161162 return false ;
162163 }
163- Util . log ( `Package ${ packageName } installed successfully` ) ;
164+ Util . log ( `Package ${ sanitizePackage } installed successfully` ) ;
164165 return true ;
165166 }
166167
167168 public static async queuePackage ( packageName : string , verbose = false ) {
168- const args = this . getInstallArgs ( packageName ) . map ( arg => arg === '--save' ? '--no-save' : arg ) ;
169+ const command = this . getInstallCommand ( this . getManager ( ) , Util . sanitizeShellArg ( packageName ) )
170+ . replace ( "--save" , "--no-save" ) ;
169171 const [ packName , version ] = packageName . split ( / @ (? = [ ^ \/ ] + $ ) / ) ;
170172 const packageJSON = this . getPackageJSON ( ) ;
171173 if ( ! packageJSON . dependencies ) {
@@ -190,24 +192,13 @@ export class PackageManager {
190192 // D.P. Concurrent install runs should be supported
191193 // https://github.com/npm/npm/issues/5948
192194 // https://github.com/npm/npm/issues/2500
193- const managerCommand = this . getManager ( ) ;
194195 const task = new Promise < { packageName , error , stdout , stderr } > ( ( resolve , reject ) => {
195- const child = spawn ( managerCommand , args ) ;
196- let stdout = '' ;
197- let stderr = '' ;
198- child . stdout ?. on ( 'data' , ( data ) => {
199- stdout += data . toString ( ) ;
200- } ) ;
201- child . stderr ?. on ( 'data' , ( data ) => {
202- stderr += data . toString ( ) ;
203- } ) ;
204- child . on ( 'close' , ( code ) => {
205- const error = code !== 0 ? new Error ( `Process exited with code ${ code } ` ) : null ;
206- resolve ( { packageName, error, stdout, stderr } ) ;
207- } ) ;
208- child . on ( 'error' , ( err ) => {
209- resolve ( { packageName, error : err , stdout, stderr } ) ;
210- } ) ;
196+ const child = exec (
197+ command , { } ,
198+ ( error , stdout , stderr ) => {
199+ resolve ( { packageName, error, stdout, stderr } ) ;
200+ }
201+ ) ;
211202 } ) ;
212203 task [ "packageName" ] = packName ;
213204 this . installQueue . push ( task ) ;
@@ -233,10 +224,10 @@ export class PackageManager {
233224 }
234225
235226 public static ensureRegistryUser ( config : Config , message : string ) : boolean {
236- const fullPackageRegistry = config . igPackageRegistry ;
227+ const fullPackageRegistry = Util . sanitizeShellArg ( config . igPackageRegistry ) ;
237228 try {
238229 // tslint:disable-next-line:object-literal-sort-keys
239- Util . spawnSync ( ' npm' , [ ' whoami' , ` --registry=${ fullPackageRegistry } `] , { stdio : ' pipe' , encoding : ' utf8' } ) ;
230+ Util . execSync ( ` npm whoami --registry=${ fullPackageRegistry } `, { stdio : " pipe" , encoding : " utf8" } ) ;
240231 } catch ( error ) {
241232 // try registering the user:
242233 Util . log (
@@ -257,20 +248,16 @@ export class PackageManager {
257248 if ( process . stdin . isTTY ) {
258249 process . stdin . setRawMode ( true ) ;
259250 }
260- const cmd = / ^ w i n / . test ( process . platform ) ? "npm.cmd" : "npm" ; //https://github.com/nodejs/node/issues/3675
261- const login = Util . spawnSync ( cmd ,
262- [ "adduser" , `--registry= ${ fullPackageRegistry } ` , `--scope=@infragistics` , `--auth-type=legacy` ] ,
263- { stdio : "inherit" }
264- ) ;
265- if ( login ?. status === 0 ) {
251+
252+ try {
253+ Util . execSync (
254+ `npm login --registry= ${ fullPackageRegistry } --scope=@infragistics --auth-type=legacy` ,
255+ { stdio : "inherit" }
256+ ) ;
266257 //make sure scope is configured:
267- try {
268- Util . spawnSync ( 'npm' , [ 'config' , 'set' , `@infragistics:registry` , fullPackageRegistry ] ) ;
269- return true ;
270- } catch ( error ) {
271- return false ;
272- }
273- } else {
258+ Util . execSync ( `npm config set @infragistics:registry ${ fullPackageRegistry } ` ) ;
259+ return true ;
260+ } catch ( error ) {
274261 Util . log ( message , "red" ) ;
275262 return false ;
276263 }
@@ -292,11 +279,7 @@ export class PackageManager {
292279 }
293280 }
294281
295- private static getInstallArgs ( packageName : string ) : string [ ] {
296- return [ 'install' , packageName , '--quiet' , '--save' ] ;
297- }
298-
299- private static getManager ( /*config:Config*/ ) : string {
282+ private static getManager ( /*config:Config*/ ) : 'npm' {
300283 //stub to potentially swap out managers
301284 return "npm" ;
302285 }
0 commit comments