11/* eslint-disable no-console */
2- import { exec } from 'child_process'
32import fs from 'fs'
43import homedir from 'os'
54import readline from 'readline'
@@ -75,21 +74,22 @@ function setupCommand (name, description, argv, importMeta) {
7574 if ( fs . existsSync ( BASH_FILE ) ) {
7675 const socketWrapperEnabled = checkSocketWrapperAlreadySetup ( BASH_FILE )
7776 ! socketWrapperEnabled && addAlias ( BASH_FILE )
78- } else if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
79- const socketWrapperEnabled = checkSocketWrapperAlreadySetup ( BASH_FILE )
77+ }
78+ if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
79+ const socketWrapperEnabled = checkSocketWrapperAlreadySetup ( ZSH_BASH_FILE )
8080 ! socketWrapperEnabled && addAlias ( ZSH_BASH_FILE )
81- } else {
82- console . error ( 'There was an issue setting up the alias in your bash profile' )
8381 }
8482 } else if ( disable ) {
8583 if ( fs . existsSync ( BASH_FILE ) ) {
8684 removeAlias ( BASH_FILE )
87- } else if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
85+ }
86+ if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
8887 removeAlias ( ZSH_BASH_FILE )
89- } else {
90- console . error ( 'There was an issue setting up the alias in your bash profile' )
9188 }
9289 }
90+ if ( ! fs . existsSync ( BASH_FILE ) && ! fs . existsSync ( ZSH_BASH_FILE ) ) {
91+ console . error ( 'There was an issue setting up the alias in your bash profile' )
92+ }
9393 return
9494}
9595
@@ -124,7 +124,8 @@ const askQuestion = (rl, query) => {
124124 try {
125125 if ( fs . existsSync ( BASH_FILE ) ) {
126126 addAlias ( BASH_FILE )
127- } else if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
127+ }
128+ if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
128129 addAlias ( ZSH_BASH_FILE )
129130 }
130131 } catch ( e ) {
@@ -144,9 +145,9 @@ const askQuestion = (rl, query) => {
144145 * @returns {void }
145146 */
146147const addAlias = ( file ) => {
147- exec ( `echo " alias npm=' socket npm' \nalias npx=' socket npx'" >> ${ file } ` , ( err , _ , stderr ) => {
148+ return fs . appendFile ( file , ' alias npm=" socket npm" \nalias npx=" socket npx"\n' , ( err ) => {
148149 if ( err ) {
149- return new Error ( `There was an error setting up the alias: ${ stderr } ` )
150+ return new Error ( `There was an error setting up the alias: ${ err } ` )
150151 }
151152 console . log ( `
152153The alias was added to ${ file } . Running 'npm install' will now be wrapped in Socket's "safe npm" 🎉
@@ -165,7 +166,7 @@ const removeAlias = (file) => {
165166 console . error ( `There was an error removing the alias: ${ err } ` )
166167 return
167168 }
168- const linesWithoutSocketAlias = data . split ( '\n' ) . filter ( l => l !== " alias npm=' socket npm'" && l !== " alias npx=' socket npx'" )
169+ const linesWithoutSocketAlias = data . split ( '\n' ) . filter ( l => l !== ' alias npm=" socket npm"' && l !== ' alias npx=" socket npx"' )
169170
170171 const updatedFileContent = linesWithoutSocketAlias . join ( '\n' )
171172
@@ -188,10 +189,10 @@ The alias was removed from ${file}. Running 'npm install' will now run the stand
188189 */
189190const checkSocketWrapperAlreadySetup = ( file ) => {
190191 const fileContent = fs . readFileSync ( file , 'utf-8' )
191- const linesWithSocketAlias = fileContent . split ( '\n' ) . filter ( l => l === " alias npm=' socket npm'" || l === " alias npx=' socket npx'" )
192+ const linesWithSocketAlias = fileContent . split ( '\n' ) . filter ( l => l === ' alias npm=" socket npm"' || l === ' alias npx=" socket npx"' )
192193
193194 if ( linesWithSocketAlias . length ) {
194- console . log ( `It looks like the Socket npm/npx wrapper is already set up in your bash profile (${ file } ).` )
195+ console . log ( `The Socket npm/npx wrapper is set up in your bash profile (${ file } ).` )
195196 return true
196197 }
197198 return false
0 commit comments