From 6189ee7eb669b25a3d3f624835077417f655ee31 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Sat, 2 Nov 2019 15:08:42 -0400 Subject: [PATCH 1/4] fix: avoid adding unnecessary new line to `.npmrc` --- lib/set-npmrc-auth.js | 10 ++++++++-- test/set-npmrc-auth.test.js | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/set-npmrc-auth.js b/lib/set-npmrc-auth.js index d2d1ba8f..596f1826 100644 --- a/lib/set-npmrc-auth.js +++ b/lib/set-npmrc-auth.js @@ -25,10 +25,16 @@ module.exports = async ( } if (NPM_USERNAME && NPM_PASSWORD && NPM_EMAIL) { - await outputFile(npmrc, `${currentConfig}\n_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`); + await outputFile( + npmrc, + `${currentConfig ? `${currentConfig}\n` : ''}_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}` + ); logger.log(`Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${npmrc}`); } else if (NPM_TOKEN) { - await outputFile(npmrc, `${currentConfig}\n${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`); + await outputFile( + npmrc, + `${currentConfig ? `${currentConfig}\n` : ''}${nerfDart(registry)}:_authToken = \${NPM_TOKEN}` + ); logger.log(`Wrote NPM_TOKEN to ${npmrc}`); } else { throw new AggregateError([getError('ENONPMTOKEN', {registry})]); diff --git a/test/set-npmrc-auth.test.js b/test/set-npmrc-auth.test.js index 94700ae5..7178e54f 100644 --- a/test/set-npmrc-auth.test.js +++ b/test/set-npmrc-auth.test.js @@ -44,7 +44,7 @@ test.serial('Set auth with "NPM_USERNAME", "NPM_PASSWORD" and "NPM_EMAIL"', asyn await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger}); - t.is((await readFile(npmrc)).toString(), `\n_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`); + t.is((await readFile(npmrc)).toString(), `_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`); t.deepEqual(t.context.log.args[1], [`Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${npmrc}`]); }); From a0120d20e84924d076e5bb1ee451d8307e7db023 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Sat, 2 Nov 2019 15:09:08 -0400 Subject: [PATCH 2/4] fix: log the path of existing `.npmrc` files --- lib/set-npmrc-auth.js | 5 +++++ test/set-npmrc-auth.test.js | 24 +++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/set-npmrc-auth.js b/lib/set-npmrc-auth.js index 596f1826..821632ca 100644 --- a/lib/set-npmrc-auth.js +++ b/lib/set-npmrc-auth.js @@ -17,6 +17,11 @@ module.exports = async ( {registry: 'https://registry.npmjs.org/'}, {config: NPM_CONFIG_USERCONFIG || path.resolve(cwd, '.npmrc')} ); + + if (configs) { + logger.log('Reading npm config from %s', configs.join(', ')); + } + const currentConfig = configs ? (await Promise.all(configs.map(config => readFile(config)))).join('\n') : ''; if (getAuthToken(registry, {npmrc: rcConfig})) { diff --git a/test/set-npmrc-auth.test.js b/test/set-npmrc-auth.test.js index 7178e54f..3d79ef03 100644 --- a/test/set-npmrc-auth.test.js +++ b/test/set-npmrc-auth.test.js @@ -60,7 +60,11 @@ test.serial('Preserve home ".npmrc"', async t => { await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger}); t.is((await readFile(npmrc)).toString(), `home_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`); - t.deepEqual(t.context.log.args[1], [`Wrote NPM_TOKEN to ${npmrc}`]); + t.deepEqual(t.context.log.args[1], [ + 'Reading npm config from %s', + [path.resolve(process.env.HOME, '.npmrc')].join(', '), + ]); + t.deepEqual(t.context.log.args[2], [`Wrote NPM_TOKEN to ${npmrc}`]); }); test.serial('Preserve home and local ".npmrc"', async t => { @@ -79,7 +83,11 @@ test.serial('Preserve home and local ".npmrc"', async t => { (await readFile(npmrc)).toString(), `home_config = test\ncwd_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}` ); - t.deepEqual(t.context.log.args[1], [`Wrote NPM_TOKEN to ${npmrc}`]); + t.deepEqual(t.context.log.args[1], [ + 'Reading npm config from %s', + [path.resolve(process.env.HOME, '.npmrc'), path.resolve(cwd, '.npmrc')].join(', '), + ]); + t.deepEqual(t.context.log.args[2], [`Wrote NPM_TOKEN to ${npmrc}`]); }); test.serial('Preserve all ".npmrc" if auth is already configured', async t => { @@ -94,7 +102,10 @@ test.serial('Preserve all ".npmrc" if auth is already configured', async t => { await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env: {}, logger: t.context.logger}); t.is((await readFile(npmrc)).toString(), `home_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`); - t.is(t.context.log.callCount, 1); + t.deepEqual(t.context.log.args[1], [ + 'Reading npm config from %s', + [path.resolve(process.env.HOME, '.npmrc'), path.resolve(cwd, '.npmrc')].join(', '), + ]); }); test.serial('Preserve ".npmrc" if auth is already configured for a scoped package', async t => { @@ -115,7 +126,10 @@ test.serial('Preserve ".npmrc" if auth is already configured for a scoped packag (await readFile(npmrc)).toString(), `home_config = test\n@scope:registry=http://custom.registry.com\n//custom.registry.com/:_authToken = \${NPM_TOKEN}` ); - t.is(t.context.log.callCount, 1); + t.deepEqual(t.context.log.args[1], [ + 'Reading npm config from %s', + [path.resolve(process.env.HOME, '.npmrc'), path.resolve(cwd, '.npmrc')].join(', '), + ]); }); test.serial('Throw error if "NPM_TOKEN" is missing', async t => { @@ -148,7 +162,7 @@ test.serial('Emulate npm config resolution if "NPM_CONFIG_USERCONFIG" is set', a }); t.is((await readFile(npmrc)).toString(), `//custom.registry.com/:_authToken = \${NPM_TOKEN}`); - t.is(t.context.log.callCount, 1); + t.deepEqual(t.context.log.args[1], ['Reading npm config from %s', [path.resolve(cwd, '.custom-npmrc')].join(', ')]); }); test.serial('Throw error if "NPM_USERNAME" is missing', async t => { From cd1ecaaebdec6384a80cfe1a633db17797342287 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Sun, 3 Nov 2019 16:19:22 -0500 Subject: [PATCH 3/4] fix: log the output of `npm whoami` command --- lib/verify-auth.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/verify-auth.js b/lib/verify-auth.js index fe8c60bf..5f690e2a 100644 --- a/lib/verify-auth.js +++ b/lib/verify-auth.js @@ -9,6 +9,8 @@ module.exports = async (npmrc, pkg, context) => { const { cwd, env: {DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/', ...env}, + stdout, + stderr, } = context; const registry = getRegistry(pkg, context); @@ -16,7 +18,16 @@ module.exports = async (npmrc, pkg, context) => { if (normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY)) { try { - await execa('npm', ['whoami', '--userconfig', npmrc, '--registry', registry], {cwd, env}); + const whoamiResult = execa('npm', ['whoami', '--userconfig', npmrc, '--registry', registry], {cwd, env}); + whoamiResult.stdout.pipe( + stdout, + {end: false} + ); + whoamiResult.stderr.pipe( + stderr, + {end: false} + ); + await whoamiResult; } catch (_) { throw new AggregateError([getError('EINVALIDNPMTOKEN', {registry})]); } From c9fc3c14ae42a9f75573bbb195e3735c28790a3a Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Wed, 20 Nov 2019 20:21:33 -0500 Subject: [PATCH 4/4] style: prettier formatting --- lib/prepare.js | 20 ++++---------------- lib/publish.js | 10 ++-------- lib/verify-auth.js | 10 ++-------- 3 files changed, 8 insertions(+), 32 deletions(-) diff --git a/lib/prepare.js b/lib/prepare.js index 254adab4..b471c8e5 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -11,28 +11,16 @@ module.exports = async (npmrc, {tarballDir, pkgRoot}, {cwd, env, stdout, stderr, cwd: basePath, env, }); - versionResult.stdout.pipe( - stdout, - {end: false} - ); - versionResult.stderr.pipe( - stderr, - {end: false} - ); + versionResult.stdout.pipe(stdout, {end: false}); + versionResult.stderr.pipe(stderr, {end: false}); await versionResult; if (tarballDir) { logger.log('Creating npm package version %s', version); const packResult = execa('npm', ['pack', basePath, '--userconfig', npmrc], {cwd, env}); - packResult.stdout.pipe( - stdout, - {end: false} - ); - packResult.stderr.pipe( - stderr, - {end: false} - ); + packResult.stdout.pipe(stdout, {end: false}); + packResult.stderr.pipe(stderr, {end: false}); const tarball = (await packResult).stdout.split('\n').pop(); const tarballSource = path.resolve(cwd, tarball); diff --git a/lib/publish.js b/lib/publish.js index 51e6cd7d..6f7756e7 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -19,14 +19,8 @@ module.exports = async (npmrc, {npmPublish, pkgRoot}, pkg, context) => { logger.log('Publishing version %s to npm registry', version); const result = execa('npm', ['publish', basePath, '--userconfig', npmrc, '--registry', registry], {cwd, env}); - result.stdout.pipe( - stdout, - {end: false} - ); - result.stderr.pipe( - stderr, - {end: false} - ); + result.stdout.pipe(stdout, {end: false}); + result.stderr.pipe(stderr, {end: false}); await result; logger.log(`Published ${pkg.name}@${pkg.version} on ${registry}`); diff --git a/lib/verify-auth.js b/lib/verify-auth.js index 5f690e2a..03421697 100644 --- a/lib/verify-auth.js +++ b/lib/verify-auth.js @@ -19,14 +19,8 @@ module.exports = async (npmrc, pkg, context) => { if (normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY)) { try { const whoamiResult = execa('npm', ['whoami', '--userconfig', npmrc, '--registry', registry], {cwd, env}); - whoamiResult.stdout.pipe( - stdout, - {end: false} - ); - whoamiResult.stderr.pipe( - stderr, - {end: false} - ); + whoamiResult.stdout.pipe(stdout, {end: false}); + whoamiResult.stderr.pipe(stderr, {end: false}); await whoamiResult; } catch (_) { throw new AggregateError([getError('EINVALIDNPMTOKEN', {registry})]);