Skip to content

Commit

Permalink
add raw-npm and raw-npx
Browse files Browse the repository at this point in the history
  • Loading branch information
charliegerard committed Jan 17, 2024
1 parent 87faa04 commit b44930b
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ socket --help
socket info [email protected]
socket report create package.json --view
socket report view QXU8PmK7LfH608RAwfIKdbcHgwEd_ZeWJ9QEGv05FJUQ
socket wrapper --enable
```

## Commands
Expand All @@ -35,6 +36,10 @@ socket report view QXU8PmK7LfH608RAwfIKdbcHgwEd_ZeWJ9QEGv05FJUQ

* `socket report view <report-id>` - looks up issues and scores from a report

* `socket wrapper --enable` and `socket wrapper --disable` - Enables and disables the Socket 'safe-npm' wrapper.

* `socket raw-npm` and `socket raw-npx` - Temporarily disables the Socket 'safe-npm' wrapper.

## Aliases

All aliases supports flags and arguments of the commands they alias.
Expand Down
11 changes: 10 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ import { initUpdateNotifier } from './lib/utils/update-notifier.js'
initUpdateNotifier()

try {
const formattedCliCommands = Object.fromEntries(Object.entries(cliCommands).map((entry) => {
if (entry[0] === 'rawNpm') {
entry[0] = 'raw-npm'
} else if (entry[0] === 'rawNpx') {
entry[0] = 'raw-npx'
}
return entry
}))

await meowWithSubcommands(
cliCommands,
formattedCliCommands,
{
aliases: {
ci: {
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from './npx/index.js'
export * from './login/index.js'
export * from './logout/index.js'
export * from './wrapper/index.js'
export * from './raw-npm/index.js'
export * from './raw-npx/index.js'
59 changes: 59 additions & 0 deletions lib/commands/raw-npm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { spawn } from 'child_process'

import meow from 'meow'

import { validationFlags } from '../../flags/index.js'
import { printFlagList } from '../../utils/formatting.js'

/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
export const rawNpm = {
description: 'Temporarily disable the Socket npm wrapper',
async run (argv, importMeta, { parentName }) {
const name = parentName + ' raw-npm'

setupCommand(name, rawNpm.description, argv, importMeta)
}
}

/**
* @param {string} name
* @param {string} description
* @param {readonly string[]} argv
* @param {ImportMeta} importMeta
* @returns {void}
*/
function setupCommand (name, description, argv, importMeta) {
const flags = validationFlags

const cli = meow(`
Usage
$ ${name} <npm command>
Options
${printFlagList(flags, 6)}
Examples
$ ${name} install
`, {
argv,
description,
importMeta,
flags
})

if (!argv[0]) {
cli.showHelp()
return
}

spawn('npm', [argv.join(' ')], {
stdio: 'inherit',
shell: true
}).on('exit', (code, signal) => {
if (signal) {
process.kill(process.pid, signal)
} else if (code !== null) {
process.exit(code)
}
})
}
59 changes: 59 additions & 0 deletions lib/commands/raw-npx/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { spawn } from 'child_process'

import meow from 'meow'

import { validationFlags } from '../../flags/index.js'
import { printFlagList } from '../../utils/formatting.js'

/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
export const rawNpx = {
description: 'Temporarily disable the Socket npm/npx wrapper',
async run (argv, importMeta, { parentName }) {
const name = parentName + ' raw-npx'

setupCommand(name, rawNpx.description, argv, importMeta)
}
}

/**
* @param {string} name
* @param {string} description
* @param {readonly string[]} argv
* @param {ImportMeta} importMeta
* @returns {void}
*/
function setupCommand (name, description, argv, importMeta) {
const flags = validationFlags

const cli = meow(`
Usage
$ ${name} <npx command>
Options
${printFlagList(flags, 6)}
Examples
$ ${name} install
`, {
argv,
description,
importMeta,
flags
})

if (!argv[0]) {
cli.showHelp()
return
}

spawn('npx', [argv.join(' ')], {
stdio: 'inherit',
shell: true
}).on('exit', (code, signal) => {
if (signal) {
process.kill(process.pid, signal)
} else if (code !== null) {
process.exit(code)
}
})
}
22 changes: 19 additions & 3 deletions lib/commands/wrapper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ function setupCommand (name, description, argv, importMeta) {

if (enable) {
if (fs.existsSync(BASH_FILE)) {
addAlias(BASH_FILE)
const socketWrapperEnabled = checkSocketWrapperAlreadySetup(BASH_FILE)
!socketWrapperEnabled && addAlias(BASH_FILE)
} else if (fs.existsSync(ZSH_BASH_FILE)) {
addAlias(ZSH_BASH_FILE)
const socketWrapperEnabled = checkSocketWrapperAlreadySetup(BASH_FILE)
!socketWrapperEnabled && addAlias(ZSH_BASH_FILE)
} else {
console.error('There was an issue setting up the alias in your bash profile')
}
Expand All @@ -83,7 +85,6 @@ function setupCommand (name, description, argv, importMeta) {
console.error('There was an issue setting up the alias in your bash profile')
}
}

return
}

Expand Down Expand Up @@ -175,3 +176,18 @@ The alias was removed from ${file}. Running 'npm install' will now run the stand
})
})
}

/**
* @param {string} file
* @returns {boolean}
*/
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'")

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

0 comments on commit b44930b

Please sign in to comment.