Skip to content

Commit

Permalink
implemented feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
charliegerard committed Jan 18, 2024
1 parent b133204 commit 8345060
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions lib/commands/wrapper/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-console */
import { exec } from 'child_process'
import fs from 'fs'
import homedir from 'os'
import readline from 'readline'
Expand Down Expand Up @@ -75,21 +74,22 @@ function setupCommand (name, description, argv, importMeta) {
if (fs.existsSync(BASH_FILE)) {
const socketWrapperEnabled = checkSocketWrapperAlreadySetup(BASH_FILE)
!socketWrapperEnabled && addAlias(BASH_FILE)
} else if (fs.existsSync(ZSH_BASH_FILE)) {
const socketWrapperEnabled = checkSocketWrapperAlreadySetup(BASH_FILE)
}
if (fs.existsSync(ZSH_BASH_FILE)) {
const socketWrapperEnabled = checkSocketWrapperAlreadySetup(ZSH_BASH_FILE)
!socketWrapperEnabled && addAlias(ZSH_BASH_FILE)
} else {
console.error('There was an issue setting up the alias in your bash profile')
}
} else if (disable) {
if (fs.existsSync(BASH_FILE)) {
removeAlias(BASH_FILE)
} else if (fs.existsSync(ZSH_BASH_FILE)) {
}
if (fs.existsSync(ZSH_BASH_FILE)) {
removeAlias(ZSH_BASH_FILE)
} else {
console.error('There was an issue setting up the alias in your bash profile')
}
}
if (!fs.existsSync(BASH_FILE) && !fs.existsSync(ZSH_BASH_FILE)) {
console.error('There was an issue setting up the alias in your bash profile')
}
return
}

Expand Down Expand Up @@ -124,7 +124,8 @@ const askQuestion = (rl, query) => {
try {
if (fs.existsSync(BASH_FILE)) {
addAlias(BASH_FILE)
} else if (fs.existsSync(ZSH_BASH_FILE)) {
}
if (fs.existsSync(ZSH_BASH_FILE)) {
addAlias(ZSH_BASH_FILE)
}
} catch (e) {
Expand All @@ -144,9 +145,9 @@ const askQuestion = (rl, query) => {
* @returns {void}
*/
const addAlias = (file) => {
exec(`echo "alias npm='socket npm'\nalias npx='socket npx'" >> ${file}`, (err, _, stderr) => {
return fs.appendFile(file, 'alias npm="socket npm"\nalias npx="socket npx"\n', (err) => {
if (err) {
return new Error(`There was an error setting up the alias: ${stderr}`)
return new Error(`There was an error setting up the alias: ${err}`)
}
console.log(`
The alias was added to ${file}. Running 'npm install' will now be wrapped in Socket's "safe npm" 🎉
Expand All @@ -165,7 +166,7 @@ const removeAlias = (file) => {
console.error(`There was an error removing the alias: ${err}`)
return
}
const linesWithoutSocketAlias = data.split('\n').filter(l => l !== "alias npm='socket npm'" && l !== "alias npx='socket npx'")
const linesWithoutSocketAlias = data.split('\n').filter(l => l !== 'alias npm="socket npm"' && l !== 'alias npx="socket npx"')

const updatedFileContent = linesWithoutSocketAlias.join('\n')

Expand All @@ -188,10 +189,10 @@ The alias was removed from ${file}. Running 'npm install' will now run the stand
*/
const checkSocketWrapperAlreadySetup = (file) => {
const fileContent = fs.readFileSync(file, 'utf-8')
const linesWithSocketAlias = fileContent.split('\n').filter(l => l === "alias npm='socket npm'" || l === "alias npx='socket npx'")
const linesWithSocketAlias = fileContent.split('\n').filter(l => l === 'alias npm="socket npm"' || l === 'alias npx="socket npx"')

if (linesWithSocketAlias.length) {
console.log(`It looks like the Socket npm/npx wrapper is already set up in your bash profile (${file}).`)
console.log(`The Socket npm/npx wrapper is set up in your bash profile (${file}).`)
return true
}
return false
Expand Down

0 comments on commit 8345060

Please sign in to comment.