Skip to content

Commit

Permalink
[fix] safe-npm spinner issue
Browse files Browse the repository at this point in the history
  • Loading branch information
charliegerard committed Aug 21, 2024
1 parent fc3f2aa commit 319684c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
26 changes: 25 additions & 1 deletion src/shadow/npm-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { realpathSync } from 'node:fs'
import path from 'node:path'

import { installLinks } from './link'
import { findRoot } from '../utils/path-resolve'

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

process.exitCode = 1

/*
Adding the `--quiet` and `--no-progress` flags when the `proc-log` module
is found to fix a UX issue when running the command with recent versions of npm
(input swallowed by the standard npm spinner)
*/
let npmArgs: string[] = []
if(process.argv.slice(2).includes('install')){
const npmEntrypoint = realpathSync(npmPath)
const npmRootPath = findRoot(path.dirname(npmEntrypoint))
if (npmRootPath === undefined) {
process.exit(127)
}
const npmDepPath = path.join(npmRootPath, 'node_modules')

let npmlog
try {
npmlog = require(path.join(npmDepPath, 'proc-log/lib/index.js')).log
} catch {}
if (npmlog) {
npmArgs = ['--quiet', '--no-progress']
}
}

spawn(
process.execPath,
['--require', injectionPath, npmPath, ...process.argv.slice(2)],
['--require', injectionPath, npmPath, ...process.argv.slice(2), ...npmArgs],
{
stdio: 'inherit'
}
Expand Down
15 changes: 1 addition & 14 deletions src/shadow/npm-injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
} from '@npmcli/arborist'
import type { Writable } from 'node:stream'
import type { Options as OraOptions } from 'ora'

Check warning on line 29 in src/shadow/npm-injection.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import { findRoot } from '../utils/path-resolve'

Check warning on line 30 in src/shadow/npm-injection.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../utils/path-resolve` import should occur before import of `../utils/sdk`

type ArboristClass = typeof BaseArborist & {
new (...args: any): any
Expand Down Expand Up @@ -175,20 +176,6 @@ async function* batchScan(
}
}

function findRoot(filepath: string): string | undefined {
let curPath = filepath
while (true) {
if (path.basename(curPath) === 'npm') {
return curPath
}
const parent = path.dirname(curPath)
if (parent === curPath) {
return undefined
}
curPath = parent
}
}

function findSocketYML() {
let prevDir = null
let dir = process.cwd()
Expand Down
14 changes: 14 additions & 0 deletions src/utils/path-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,17 @@ export async function mapGlobEntryToFiles(

return files
}

export function findRoot(filepath: string): string | undefined {
let curPath = filepath
while (true) {
if (path.basename(curPath) === 'npm') {
return curPath
}
const parent = path.dirname(curPath)
if (parent === curPath) {
return undefined
}
curPath = parent
}
}

0 comments on commit 319684c

Please sign in to comment.