Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] safe-npm spinner issue #178

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -26,7 +26,8 @@
Options as ArboristOptions
} 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 @@
}
}

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
}
}
Loading