Skip to content

Commit 13aac33

Browse files
Merge pull request #178 from SocketDev/cg/fixIssueSpinner
[fix] safe-npm spinner issue
2 parents fc3f2aa + 319684c commit 13aac33

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

src/shadow/npm-cli.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { realpathSync } from 'node:fs'
55
import path from 'node:path'
66

77
import { installLinks } from './link'
8+
import { findRoot } from '../utils/path-resolve'
89

910
const realFilename = realpathSync(__filename)
1011
const realDirname = path.dirname(realFilename)
@@ -14,9 +15,32 @@ const injectionPath = path.join(realDirname, 'npm-injection.js')
1415

1516
process.exitCode = 1
1617

18+
/*
19+
Adding the `--quiet` and `--no-progress` flags when the `proc-log` module
20+
is found to fix a UX issue when running the command with recent versions of npm
21+
(input swallowed by the standard npm spinner)
22+
*/
23+
let npmArgs: string[] = []
24+
if(process.argv.slice(2).includes('install')){
25+
const npmEntrypoint = realpathSync(npmPath)
26+
const npmRootPath = findRoot(path.dirname(npmEntrypoint))
27+
if (npmRootPath === undefined) {
28+
process.exit(127)
29+
}
30+
const npmDepPath = path.join(npmRootPath, 'node_modules')
31+
32+
let npmlog
33+
try {
34+
npmlog = require(path.join(npmDepPath, 'proc-log/lib/index.js')).log
35+
} catch {}
36+
if (npmlog) {
37+
npmArgs = ['--quiet', '--no-progress']
38+
}
39+
}
40+
1741
spawn(
1842
process.execPath,
19-
['--require', injectionPath, npmPath, ...process.argv.slice(2)],
43+
['--require', injectionPath, npmPath, ...process.argv.slice(2), ...npmArgs],
2044
{
2145
stdio: 'inherit'
2246
}

src/shadow/npm-injection.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type {
2727
} from '@npmcli/arborist'
2828
import type { Writable } from 'node:stream'
2929
import type { Options as OraOptions } from 'ora'
30+
import { findRoot } from '../utils/path-resolve'
3031

3132
type ArboristClass = typeof BaseArborist & {
3233
new (...args: any): any
@@ -175,20 +176,6 @@ async function* batchScan(
175176
}
176177
}
177178

178-
function findRoot(filepath: string): string | undefined {
179-
let curPath = filepath
180-
while (true) {
181-
if (path.basename(curPath) === 'npm') {
182-
return curPath
183-
}
184-
const parent = path.dirname(curPath)
185-
if (parent === curPath) {
186-
return undefined
187-
}
188-
curPath = parent
189-
}
190-
}
191-
192179
function findSocketYML() {
193180
let prevDir = null
194181
let dir = process.cwd()

src/utils/path-resolve.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,17 @@ export async function mapGlobEntryToFiles(
134134

135135
return files
136136
}
137+
138+
export function findRoot(filepath: string): string | undefined {
139+
let curPath = filepath
140+
while (true) {
141+
if (path.basename(curPath) === 'npm') {
142+
return curPath
143+
}
144+
const parent = path.dirname(curPath)
145+
if (parent === curPath) {
146+
return undefined
147+
}
148+
curPath = parent
149+
}
150+
}

0 commit comments

Comments
 (0)