Skip to content

Commit b44930b

Browse files
committed
add raw-npm and raw-npx
1 parent 87faa04 commit b44930b

File tree

6 files changed

+154
-4
lines changed

6 files changed

+154
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ socket --help
1818
socket info [email protected]
1919
socket report create package.json --view
2020
socket report view QXU8PmK7LfH608RAwfIKdbcHgwEd_ZeWJ9QEGv05FJUQ
21+
socket wrapper --enable
2122
```
2223

2324
## Commands
@@ -35,6 +36,10 @@ socket report view QXU8PmK7LfH608RAwfIKdbcHgwEd_ZeWJ9QEGv05FJUQ
3536

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

39+
* `socket wrapper --enable` and `socket wrapper --disable` - Enables and disables the Socket 'safe-npm' wrapper.
40+
41+
* `socket raw-npm` and `socket raw-npx` - Temporarily disables the Socket 'safe-npm' wrapper.
42+
3843
## Aliases
3944

4045
All aliases supports flags and arguments of the commands they alias.

cli.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ import { initUpdateNotifier } from './lib/utils/update-notifier.js'
1515
initUpdateNotifier()
1616

1717
try {
18+
const formattedCliCommands = Object.fromEntries(Object.entries(cliCommands).map((entry) => {
19+
if (entry[0] === 'rawNpm') {
20+
entry[0] = 'raw-npm'
21+
} else if (entry[0] === 'rawNpx') {
22+
entry[0] = 'raw-npx'
23+
}
24+
return entry
25+
}))
26+
1827
await meowWithSubcommands(
19-
cliCommands,
28+
formattedCliCommands,
2029
{
2130
aliases: {
2231
ci: {

lib/commands/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export * from './npx/index.js'
55
export * from './login/index.js'
66
export * from './logout/index.js'
77
export * from './wrapper/index.js'
8+
export * from './raw-npm/index.js'
9+
export * from './raw-npx/index.js'

lib/commands/raw-npm/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { spawn } from 'child_process'
2+
3+
import meow from 'meow'
4+
5+
import { validationFlags } from '../../flags/index.js'
6+
import { printFlagList } from '../../utils/formatting.js'
7+
8+
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
9+
export const rawNpm = {
10+
description: 'Temporarily disable the Socket npm wrapper',
11+
async run (argv, importMeta, { parentName }) {
12+
const name = parentName + ' raw-npm'
13+
14+
setupCommand(name, rawNpm.description, argv, importMeta)
15+
}
16+
}
17+
18+
/**
19+
* @param {string} name
20+
* @param {string} description
21+
* @param {readonly string[]} argv
22+
* @param {ImportMeta} importMeta
23+
* @returns {void}
24+
*/
25+
function setupCommand (name, description, argv, importMeta) {
26+
const flags = validationFlags
27+
28+
const cli = meow(`
29+
Usage
30+
$ ${name} <npm command>
31+
32+
Options
33+
${printFlagList(flags, 6)}
34+
35+
Examples
36+
$ ${name} install
37+
`, {
38+
argv,
39+
description,
40+
importMeta,
41+
flags
42+
})
43+
44+
if (!argv[0]) {
45+
cli.showHelp()
46+
return
47+
}
48+
49+
spawn('npm', [argv.join(' ')], {
50+
stdio: 'inherit',
51+
shell: true
52+
}).on('exit', (code, signal) => {
53+
if (signal) {
54+
process.kill(process.pid, signal)
55+
} else if (code !== null) {
56+
process.exit(code)
57+
}
58+
})
59+
}

lib/commands/raw-npx/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { spawn } from 'child_process'
2+
3+
import meow from 'meow'
4+
5+
import { validationFlags } from '../../flags/index.js'
6+
import { printFlagList } from '../../utils/formatting.js'
7+
8+
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
9+
export const rawNpx = {
10+
description: 'Temporarily disable the Socket npm/npx wrapper',
11+
async run (argv, importMeta, { parentName }) {
12+
const name = parentName + ' raw-npx'
13+
14+
setupCommand(name, rawNpx.description, argv, importMeta)
15+
}
16+
}
17+
18+
/**
19+
* @param {string} name
20+
* @param {string} description
21+
* @param {readonly string[]} argv
22+
* @param {ImportMeta} importMeta
23+
* @returns {void}
24+
*/
25+
function setupCommand (name, description, argv, importMeta) {
26+
const flags = validationFlags
27+
28+
const cli = meow(`
29+
Usage
30+
$ ${name} <npx command>
31+
32+
Options
33+
${printFlagList(flags, 6)}
34+
35+
Examples
36+
$ ${name} install
37+
`, {
38+
argv,
39+
description,
40+
importMeta,
41+
flags
42+
})
43+
44+
if (!argv[0]) {
45+
cli.showHelp()
46+
return
47+
}
48+
49+
spawn('npx', [argv.join(' ')], {
50+
stdio: 'inherit',
51+
shell: true
52+
}).on('exit', (code, signal) => {
53+
if (signal) {
54+
process.kill(process.pid, signal)
55+
} else if (code !== null) {
56+
process.exit(code)
57+
}
58+
})
59+
}

lib/commands/wrapper/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ function setupCommand (name, description, argv, importMeta) {
6868

6969
if (enable) {
7070
if (fs.existsSync(BASH_FILE)) {
71-
addAlias(BASH_FILE)
71+
const socketWrapperEnabled = checkSocketWrapperAlreadySetup(BASH_FILE)
72+
!socketWrapperEnabled && addAlias(BASH_FILE)
7273
} else if (fs.existsSync(ZSH_BASH_FILE)) {
73-
addAlias(ZSH_BASH_FILE)
74+
const socketWrapperEnabled = checkSocketWrapperAlreadySetup(BASH_FILE)
75+
!socketWrapperEnabled && addAlias(ZSH_BASH_FILE)
7476
} else {
7577
console.error('There was an issue setting up the alias in your bash profile')
7678
}
@@ -83,7 +85,6 @@ function setupCommand (name, description, argv, importMeta) {
8385
console.error('There was an issue setting up the alias in your bash profile')
8486
}
8587
}
86-
8788
return
8889
}
8990

@@ -175,3 +176,18 @@ The alias was removed from ${file}. Running 'npm install' will now run the stand
175176
})
176177
})
177178
}
179+
180+
/**
181+
* @param {string} file
182+
* @returns {boolean}
183+
*/
184+
const checkSocketWrapperAlreadySetup = (file) => {
185+
const fileContent = fs.readFileSync(file, 'utf-8')
186+
const linesWithSocketAlias = fileContent.split('\n').filter(l => l === "alias npm='socket npm'" || l === "alias npx='socket npx'")
187+
188+
if (linesWithSocketAlias.length) {
189+
console.log(`It looks like the Socket npm/npx wrapper is already set up in your bash profile (${file}).`)
190+
return true
191+
}
192+
return false
193+
}

0 commit comments

Comments
 (0)