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" ;
55import { App , ProjectConfig , Util } from "../util" ;
66
77import componentsConfig = require( "./components" ) ;
88
9+ /** @internal */
10+ export const REGISTRY_ATTEMPT_LOGIN = false ;
11+
912export class PackageManager {
1013 private static ossPackage : string = "ignite-ui" ;
1114 private static fullPackage : string = "@infragistics/ignite-ui-full" ;
@@ -45,6 +48,10 @@ export class PackageManager {
4548 const version = ossVersion ? `@"${ ossVersion } "` : "" ;
4649 const errorMsg = "Something went wrong, " +
4750 "please follow the steps in this guide: https://www.igniteui.com/help/using-ignite-ui-npm-packages" ;
51+ Util . log (
52+ "The project you've created requires the full version of Ignite UI from Infragistics private feed." ,
53+ "gray"
54+ ) ;
4855 // fallback to @latest , in case when igniteui-full does not have a matching version to ossVersion
4956 // ex: "ignite-ui": "^21.1.13" BUT --> ignite-ui-full": "^21.1.11" (no 21.1.13 released).
5057 // TODO: update temp fix - only working in 21.1.11 without errors
@@ -88,9 +95,8 @@ export class PackageManager {
8895 const config = ProjectConfig . localConfig ( ) ;
8996 if ( ! config . packagesInstalled ) {
9097 let command : string ;
91- let managerCommand : string ;
98+ const managerCommand = this . getManager ( ) ;
9299
93- managerCommand = this . getManager ( ) ;
94100 switch ( managerCommand ) {
95101 case "npm" :
96102 /* passes through */
@@ -123,49 +129,52 @@ export class PackageManager {
123129 }
124130
125131 public static removePackage ( packageName : string , verbose : boolean = false ) : boolean {
132+ let command : string ;
126133 const managerCommand = this . getManager ( ) ;
127- let args : string [ ] ;
134+ const sanitizePackage = Util . sanitizeShellArg ( packageName ) ;
128135 switch ( managerCommand ) {
129136 case "npm" :
130137 /* passes through */
131138 default :
132- args = [ ' uninstall' , packageName , ' --quiet' , ' --save' ] ;
139+ command = ` ${ managerCommand } uninstall ${ sanitizePackage } --quiet --save` ;
133140 break ;
134141 }
135142 try {
136143 // tslint:disable-next-line:object-literal-sort-keys
137- Util . spawnSync ( managerCommand , args , { stdio : "pipe" , encoding : "utf8" } ) ;
144+ Util . execSync ( command , { stdio : "pipe" , encoding : "utf8" } ) ;
138145 } catch ( error ) {
139- Util . log ( `Error uninstalling package ${ packageName } with ${ managerCommand } ` ) ;
146+ Util . log ( `Error uninstalling package ${ sanitizePackage } with ${ managerCommand } ` ) ;
140147 if ( verbose ) {
141148 Util . log ( error . message ) ;
142149 }
143150 return false ;
144151 }
145152
146- Util . log ( `Package ${ packageName } uninstalled successfully` ) ;
153+ Util . log ( `Package ${ sanitizePackage } uninstalled successfully` ) ;
147154 return true ;
148155 }
149156
150157 public static addPackage ( packageName : string , verbose : boolean = false ) : boolean {
151158 const managerCommand = this . getManager ( ) ;
152- const args = this . getInstallArgs ( packageName ) ;
159+ const sanitizePackage = Util . sanitizeShellArg ( packageName ) ;
160+ const command = this . getInstallCommand ( managerCommand , sanitizePackage ) ;
153161 try {
154162 // tslint:disable-next-line:object-literal-sort-keys
155- Util . spawnSync ( managerCommand , args , { stdio : "pipe" , encoding : "utf8" } ) ;
163+ Util . execSync ( command , { stdio : "pipe" , encoding : "utf8" } ) ;
156164 } catch ( error ) {
157- Util . log ( `Error installing package ${ packageName } with ${ managerCommand } ` ) ;
165+ Util . log ( `Error installing package ${ sanitizePackage } with ${ managerCommand } ` ) ;
158166 if ( verbose ) {
159167 Util . log ( error . message ) ;
160168 }
161169 return false ;
162170 }
163- Util . log ( `Package ${ packageName } installed successfully` ) ;
171+ Util . log ( `Package ${ sanitizePackage } installed successfully` ) ;
164172 return true ;
165173 }
166174
167175 public static async queuePackage ( packageName : string , verbose = false ) {
168- const args = this . getInstallArgs ( packageName ) . map ( arg => arg === '--save' ? '--no-save' : arg ) ;
176+ const command = this . getInstallCommand ( this . getManager ( ) , Util . sanitizeShellArg ( packageName ) )
177+ . replace ( "--save" , "--no-save" ) ;
169178 const [ packName , version ] = packageName . split ( / @ (? = [ ^ \/ ] + $ ) / ) ;
170179 const packageJSON = this . getPackageJSON ( ) ;
171180 if ( ! packageJSON . dependencies ) {
@@ -190,24 +199,13 @@ export class PackageManager {
190199 // D.P. Concurrent install runs should be supported
191200 // https://github.com/npm/npm/issues/5948
192201 // https://github.com/npm/npm/issues/2500
193- const managerCommand = this . getManager ( ) ;
194202 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- } ) ;
203+ const child = exec (
204+ command , { } ,
205+ ( error , stdout , stderr ) => {
206+ resolve ( { packageName, error, stdout, stderr } ) ;
207+ }
208+ ) ;
211209 } ) ;
212210 task [ "packageName" ] = packName ;
213211 this . installQueue . push ( task ) ;
@@ -233,16 +231,17 @@ export class PackageManager {
233231 }
234232
235233 public static ensureRegistryUser ( config : Config , message : string ) : boolean {
236- const fullPackageRegistry = config . igPackageRegistry ;
234+ const fullPackageRegistry = Util . sanitizeShellArg ( config . igPackageRegistry ) ;
237235 try {
238236 // tslint:disable-next-line:object-literal-sort-keys
239- Util . spawnSync ( ' npm' , [ ' whoami' , ` --registry=${ fullPackageRegistry } `] , { stdio : ' pipe' , encoding : ' utf8' } ) ;
237+ Util . execSync ( ` npm whoami --registry=${ fullPackageRegistry } `, { stdio : " pipe" , encoding : " utf8" } ) ;
240238 } catch ( error ) {
239+ if ( ! REGISTRY_ATTEMPT_LOGIN ) {
240+ Util . log ( message , "gray" ) ;
241+ return true ;
242+ }
243+
241244 // try registering the user:
242- Util . log (
243- "The project you've created requires the full version of Ignite UI from Infragistics private feed." ,
244- "gray"
245- ) ;
246245 Util . log (
247246 "We are initiating the login process for you. This will be required only once per environment." ,
248247 "gray"
@@ -257,20 +256,16 @@ export class PackageManager {
257256 if ( process . stdin . isTTY ) {
258257 process . stdin . setRawMode ( true ) ;
259258 }
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 ) {
259+
260+ try {
261+ Util . execSync (
262+ `npm login --registry= ${ fullPackageRegistry } --scope=@infragistics --auth-type=legacy` ,
263+ { stdio : "inherit" }
264+ ) ;
266265 //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 {
266+ Util . execSync ( `npm config set @infragistics:registry ${ fullPackageRegistry } ` ) ;
267+ return true ;
268+ } catch ( error ) {
274269 Util . log ( message , "red" ) ;
275270 return false ;
276271 }
@@ -292,11 +287,7 @@ export class PackageManager {
292287 }
293288 }
294289
295- private static getInstallArgs ( packageName : string ) : string [ ] {
296- return [ 'install' , packageName , '--quiet' , '--save' ] ;
297- }
298-
299- private static getManager ( /*config:Config*/ ) : string {
290+ private static getManager ( /*config:Config*/ ) : 'npm' {
300291 //stub to potentially swap out managers
301292 return "npm" ;
302293 }
0 commit comments