1
1
/* eslint-disable no-console */
2
- import { exec } from 'child_process'
3
2
import fs from 'fs'
4
3
import homedir from 'os'
5
4
import readline from 'readline'
@@ -75,21 +74,22 @@ function setupCommand (name, description, argv, importMeta) {
75
74
if ( fs . existsSync ( BASH_FILE ) ) {
76
75
const socketWrapperEnabled = checkSocketWrapperAlreadySetup ( BASH_FILE )
77
76
! 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 )
80
80
! socketWrapperEnabled && addAlias ( ZSH_BASH_FILE )
81
- } else {
82
- console . error ( 'There was an issue setting up the alias in your bash profile' )
83
81
}
84
82
} else if ( disable ) {
85
83
if ( fs . existsSync ( BASH_FILE ) ) {
86
84
removeAlias ( BASH_FILE )
87
- } else if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
85
+ }
86
+ if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
88
87
removeAlias ( ZSH_BASH_FILE )
89
- } else {
90
- console . error ( 'There was an issue setting up the alias in your bash profile' )
91
88
}
92
89
}
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
+ }
93
93
return
94
94
}
95
95
@@ -124,7 +124,8 @@ const askQuestion = (rl, query) => {
124
124
try {
125
125
if ( fs . existsSync ( BASH_FILE ) ) {
126
126
addAlias ( BASH_FILE )
127
- } else if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
127
+ }
128
+ if ( fs . existsSync ( ZSH_BASH_FILE ) ) {
128
129
addAlias ( ZSH_BASH_FILE )
129
130
}
130
131
} catch ( e ) {
@@ -144,9 +145,9 @@ const askQuestion = (rl, query) => {
144
145
* @returns {void }
145
146
*/
146
147
const 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 ) => {
148
149
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 } ` )
150
151
}
151
152
console . log ( `
152
153
The alias was added to ${ file } . Running 'npm install' will now be wrapped in Socket's "safe npm" 🎉
@@ -165,7 +166,7 @@ const removeAlias = (file) => {
165
166
console . error ( `There was an error removing the alias: ${ err } ` )
166
167
return
167
168
}
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"' )
169
170
170
171
const updatedFileContent = linesWithoutSocketAlias . join ( '\n' )
171
172
@@ -188,10 +189,10 @@ The alias was removed from ${file}. Running 'npm install' will now run the stand
188
189
*/
189
190
const checkSocketWrapperAlreadySetup = ( file ) => {
190
191
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"' )
192
193
193
194
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 } ).` )
195
196
return true
196
197
}
197
198
return false
0 commit comments