Skip to content

Commit

Permalink
fix: yarn v1 npm commands call fix
Browse files Browse the repository at this point in the history
Closes #58
  • Loading branch information
SkReD committed Oct 28, 2022
1 parent d1d5545 commit 3fd7819
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
23 changes: 23 additions & 0 deletions packages/pvm-core/lib/node-boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { error, logger } from './logger'
import shell from './shell'
import { env } from './env'
import { gitFetch } from './git/commands'
import path from 'path'
import fs from 'fs'
import ini from 'ini'

function readNpmRc(cwd: string): string {
const npmrcPath = path.join(cwd, '.npmrc')
return fs.existsSync(npmrcPath) ? fs.readFileSync(npmrcPath).toString('utf8') : ''
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version } = require('../package.json')
Expand Down Expand Up @@ -56,4 +64,19 @@ function nodeBoot(): void {
gitFetch(cwd)
}
}

const npmRc = readNpmRc(cwd)
// eslint-disable-next-line pvm/no-process-env
const classicYarn = process.env.npm_config_user_agent ? process.env.npm_config_user_agent.indexOf('yarn/1') !== -1 : false
// yarn 1 overrides npmrc settings with its own set of npm config envs so `yarn publish-command` wont work if it depends on those
// strict-ssl is one of those important settings, so copy it to envs and override yarn default one
if (classicYarn && npmRc) {
if (npmRc.indexOf('strict-ssl') !== -1) {
const parsedNpmRc = ini.parse(npmRc)
if (parsedNpmRc['strict-ssl'] !== undefined) {
// eslint-disable-next-line pvm/no-process-env
process.env.npm_config_strict_ssl = parsedNpmRc['strict-ssl']
}
}
}
}
3 changes: 2 additions & 1 deletion packages/pvm-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"through2": "^4.0.2",
"git-url-parse": "^11.4.4",
"json5": "^2.2.1",
"bin-version-check": "^4.0.0"
"bin-version-check": "^4.0.0",
"ini": "^1.3.8"
},
"devDependencies": {
"@pvm/releases": "0.0.0-stub",
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"chalk": "^4.0.0",
"cli-table": "^0.3.6",
"p-map": "^4.0.0",
"lodash": "^4.2.0",
"ini": "^1.3.8"
"lodash": "^4.2.0"
}
}
14 changes: 0 additions & 14 deletions src/plugins/core/publish/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import shell from '@pvm/core/lib/shell'
import { logger as defaultLogger } from '@pvm/core/lib/logger'
import type { LoggerFunc } from '@pvm/core/lib/logger'
import { env } from '@pvm/core/lib/env'
import ini from 'ini'

interface LoggerLike {
log: LoggerFunc,
Expand Down Expand Up @@ -105,19 +104,6 @@ export async function setupPublishNpmRCAndEnvVariables(cwd: string, opts: Prepar
npmrcContents.push(`email=${email}`)
}

// eslint-disable-next-line pvm/no-process-env
const classicYarn = process.env.npm_config_user_agent ? process.env.npm_config_user_agent.indexOf('yarn/1') !== -1 : false
// yarn 1 overrides npmrc settings with its own set of npm config envs so `yarn publish-command` wont work if it depends on those
// strict-ssl is one of those important settings, so copy it to envs and override yarn default one
if (classicYarn && npmRc) {
if (npmRc.indexOf('strict-ssl') !== -1) {
const parsedNpmRc = ini.parse(npmRc)
if (parsedNpmRc['strict-ssl'] !== undefined) {
publishEnv.npm_config_strict_ssl = parsedNpmRc['strict-ssl']
}
}
}

if (!dontWriteNpmRc && npmrcContents.length) {
await writeFile(path.join(cwd, '.npmrc'), `\n${npmrcContents.join('\n')}\n`, {
flag: 'a',
Expand Down
2 changes: 2 additions & 0 deletions test/scenarios/__fixtures__/boot-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
process.chdir(process.argv[1])
require('@pvm/core/lib/node-boot')
26 changes: 16 additions & 10 deletions test/scenarios/pvm-publish.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const fs = require('fs')
const path = require('path')
const { runRegistryMockServer } = require('../npm-registry-mock')
const { runMessengerMocker } = require('../slack-mock')
const { setupPublishNpmRCAndEnvVariables } = require('@pvm/plugin-core/publish/prepare')
const fsExtra = require('fs-extra')
const execShell = require('@pvm/core/lib/shell/exec').default

function readStats(repo, statsName = 'publish-stats.json') {
const rawStr = fs.readFileSync(path.join(repo.dir, statsName)).toString('utf8')
Expand Down Expand Up @@ -246,19 +246,25 @@ describe('pvm/publish', () => {
expect(successNames).not.toContain('c')
})

it('should provide strict-ssl value from .npmrc to publish command if yarn v1 used', async () => {
it('should provide strict-ssl value from .npmrc as env npm config if yarn v1 used', async () => {
const repo = await initRepo('simple-one')
await repo.writeFile('.npmrc', `strict-ssl = abc`, `disable strict-ssl`)
process.env['npm_config_user_agent'] = 'yarn/1.22.19 npm/? node/v16.16.0 win32 x64'
const { publishEnv } = await setupPublishNpmRCAndEnvVariables(repo.cwd)
expect(publishEnv['npm_config_strict_ssl']).toBe('abc')
await repo.writeFile('.npmrc', `strict-ssl = "strict-ssl value"`, `disable strict-ssl`)
const { stdout } = await execShell(`node -r "${require.resolve('./__fixtures__/boot-test.js')}" -e "console.log(process.env['npm_config_strict_ssl'])" "${repo.cwd}"`, {
env: {
...process.env,
'npm_config_user_agent': 'yarn/1.22.19 npm/? node/v16.16.0 win32 x64',
},
})
expect(stdout).toContain('strict-ssl value')
})

it('should not provide strict-ssl value from .npmrc to publish command if yarn v1 not used', async () => {
it('should not provide strict-ssl value from .npmrc as env npm config if yarn v1 not used', async () => {
const repo = await initRepo('simple-one')
await repo.writeFile('.npmrc', `strict-ssl = abc`, `disable strict-ssl`)
const { publishEnv } = await setupPublishNpmRCAndEnvVariables(repo.cwd)
expect(publishEnv['npm_config_strict_ssl']).not.toBe('abc')
await repo.writeFile('.npmrc', `strict-ssl = "strict-ssl value"`, `disable strict-ssl`)
const { stdout } = await execShell(`node -r "${require.resolve('./__fixtures__/boot-test.js')}" -e "console.log(process.env['npm_config_strict_ssl'])" "${repo.cwd}"`, {
env: process.env,
})
expect(stdout).not.toContain('strict-ssl value')
})

it('should take unified version from tag in case of unified wildcard and publish path set', async () => {
Expand Down

0 comments on commit 3fd7819

Please sign in to comment.