Skip to content

Commit

Permalink
fix: fix npm v7 install error; resolve factory versions with prerelea…
Browse files Browse the repository at this point in the history
…se versions
  • Loading branch information
shaw committed Nov 26, 2020
1 parent 7da700a commit 353508e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/core/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export abstract class BaseClass {
}

installDeps(cwd = process.cwd(), packageManager?: string, lockfile = false, opts?: any) {
const npmVersion = this.context.get('env.npmVersion')
const pm = packageManager || this.context.get('config').packageManager

const cmds = [pm, 'install']
Expand All @@ -159,6 +160,10 @@ export abstract class BaseClass {
cmds.push(pm === 'npm' ? '--no-package-lock' : pm === 'yarn' ? '--no-lockfile' : '')
}

if (npmVersion && npmVersion[0] >= 7) {
cmds.push('--legacy-peer-deps')
}

this.debug(`\nrunning \`${cmds.join(' ')}\` in ${cwd}`)

return this.exec(cmds[0], cmds.slice(1).filter(Boolean), {
Expand Down
7 changes: 4 additions & 3 deletions src/core/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BaseClass } from './base'
import { git, isGitRepo, groupBy, getPathByVersion, getVersionByPath } from '../utils'

const types = ['tag', 'branch']
const semverMatchOptions = { includePrerelease: true }

export class Version extends BaseClass {
private type = 'tag' // or branch
Expand Down Expand Up @@ -85,7 +86,7 @@ export class Version extends BaseClass {
}
} else {
const versions = this.versions.map((v) => v.long)
const long = semver.maxSatisfying(versions, target || '*')
const long = semver.maxSatisfying(versions, target || '*', semverMatchOptions)
const short = this.versions.find((v) => v.long === long)?.short
const dir = getPathByVersion(this.baseDir, this.baseName, short)
version = { dir, long, short }
Expand All @@ -106,7 +107,7 @@ export class Version extends BaseClass {

public getLatest() {
const versions = this.versions.map((v) => v.long)
const long = semver.maxSatisfying(versions, '*')
const long = semver.maxSatisfying(versions, '*', semverMatchOptions)
return this.versions.find((v) => v.long === long)?.short
}

Expand All @@ -124,7 +125,7 @@ export class Version extends BaseClass {
// slim versions
const groupVersions = groupBy(semver.sort(versions).reverse(), semver.minor)
return Object.values(groupVersions)
.map((val) => semver.maxSatisfying(val, '*') || '')
.map((val) => semver.maxSatisfying(val, '*', semverMatchOptions) || '')
.filter(Boolean)
.map((val: any) => ({
short: this.parseVersion(val),
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isWindows, isMacos, isLinux, hasGit, hasNpm, hasYarn, hasPnpm } from '../utils'
import { isWindows, isMacos, isLinux, hasGit, hasNpm, hasYarn, hasPnpm, npmVersion } from '../utils'

export function getEnv() {
return {
Expand All @@ -19,7 +19,8 @@ export function getEnv() {
zsh: process.env.ZSH,
term: process.env.TERM_PROGRAM,
termVersion: process.env.TERM_PROGRAM_VERSION,
fbiVersion: require('../../package.json').version
fbiVersion: require('../../package.json').version,
npmVersion: npmVersion()
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,22 @@ export const nodeVersion = process.version
.replace('v', '')
.split('.')
.map((x: any) => x * 1)

export const npmVersion = () => {
try {
const version = execSync('npm --version', { encoding: 'utf8' })
return (
(version &&
version
.trim()
.replace(/\\\n[ \t]*/g, '')
.replace(/\\`/g, '`')
.replace(/\\n/g, '')
.split('.')
.map((x: any) => x * 1)) ||
null
)
} catch (err) {
return null
}
}

0 comments on commit 353508e

Please sign in to comment.