From cb86654e218407bb43887c1d66695694fad0332b Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Fri, 15 Jul 2016 20:21:38 +0300 Subject: [PATCH 1/5] Send registry auth token from .npmrc if accessible --- lib/index.js | 23 +++++++++++++++++++++++ package.json | 1 + 2 files changed, 24 insertions(+) diff --git a/lib/index.js b/lib/index.js index e0acd72..c4c8b21 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,6 +7,8 @@ import retry from 'async-retry'; import Agent from './agent'; import EventEmitter from 'events'; import { basename, resolve as resolvePath } from 'path'; +import { homedir } from 'os'; +import { parse as parseIni } from 'ini'; import { stat, readFile } from 'fs-promise'; import resumer from 'resumer'; import splitArray from 'split-array'; @@ -61,6 +63,26 @@ export default class Now extends EventEmitter { throw e; } + // Read .npmrc + let npmrc; + try { + npmrc = await readFile(resolvePath(path, '.npmrc'), 'utf8'); + npmrc = parseIni(npmrc); + } catch (err) { + // Do nothing + } + + if (!npmrc) { + try { + npmrc = await readFile(resolvePath(homedir(), '.npmrc'), 'utf8'); + npmrc = parseIni(npmrc); + } catch (err) { + // Do nothing + } + } + + const authToken = npmrc['//registry.npmjs.org/:_authToken']; + if (this._debug) console.time('> [debug] Getting files'); const files = await getFiles(path, pkg, { debug: this._debug }); if (this._debug) console.timeEnd('> [debug] Getting files'); @@ -83,6 +105,7 @@ export default class Now extends EventEmitter { forceSync, name: pkg.name || basename(path), description: pkg.description, + registryAuthToken: authToken, // Flatten the array to contain files to sync where each nested input // array has a group of files with the same sha but different path files: Array.prototype.concat.apply([], Array.from(this._files).map(([sha, { data, names }]) => { diff --git a/package.json b/package.json index 5f64116..56895c5 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "email-validator": "1.0.4", "fs-promise": "0.5.0", "graceful-fs": "4.1.4", + "ini": "1.3.4", "minimatch": "3.0.0", "minimist": "1.2.0", "ms": "0.7.1", From f4c55e2864aebd79ad345d62eddb5b3d51beb1cc Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Fri, 15 Jul 2016 20:24:40 +0300 Subject: [PATCH 2/5] Forward registry auth token only if asked --- lib/index.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/index.js b/lib/index.js index c4c8b21..873101f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -64,21 +64,23 @@ export default class Now extends EventEmitter { } // Read .npmrc - let npmrc; - try { - npmrc = await readFile(resolvePath(path, '.npmrc'), 'utf8'); - npmrc = parseIni(npmrc); - } catch (err) { - // Do nothing - } - - if (!npmrc) { + let npmrc = {}; + if (pkg['forward-npm']) { try { - npmrc = await readFile(resolvePath(homedir(), '.npmrc'), 'utf8'); + npmrc = await readFile(resolvePath(path, '.npmrc'), 'utf8'); npmrc = parseIni(npmrc); } catch (err) { // Do nothing } + + if (!npmrc) { + try { + npmrc = await readFile(resolvePath(homedir(), '.npmrc'), 'utf8'); + npmrc = parseIni(npmrc); + } catch (err) { + // Do nothing + } + } } const authToken = npmrc['//registry.npmjs.org/:_authToken']; From 9feed0c767da33ab4d98f806c1e3aea4f14341af Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Sun, 17 Jul 2016 01:49:26 +0300 Subject: [PATCH 3/5] Look up `forward-npm` from now nowProperties --- lib/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 873101f..06cf1dd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -63,9 +63,11 @@ export default class Now extends EventEmitter { throw e; } + const nowProperties = pkg.now || {}; + // Read .npmrc let npmrc = {}; - if (pkg['forward-npm']) { + if (nowProperties['forward-npm']) { try { npmrc = await readFile(resolvePath(path, '.npmrc'), 'utf8'); npmrc = parseIni(npmrc); @@ -95,7 +97,6 @@ export default class Now extends EventEmitter { this._files = hashes; - const nowProperties = pkg.now || {}; const engines = nowProperties.engines || pkg.engines; const deployment = await this.retry(async (bail) => { From 753968648368d2e3133d95c6929c08774c38e7cf Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Sun, 17 Jul 2016 01:52:25 +0300 Subject: [PATCH 4/5] Keep reading lower .npmrc if no auth in project --- lib/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 06cf1dd..896aacc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -67,26 +67,27 @@ export default class Now extends EventEmitter { // Read .npmrc let npmrc = {}; + let authToken; if (nowProperties['forward-npm']) { try { npmrc = await readFile(resolvePath(path, '.npmrc'), 'utf8'); npmrc = parseIni(npmrc); + authToken = npmrc['//registry.npmjs.org/:_authToken']; } catch (err) { // Do nothing } - if (!npmrc) { + if (!authToken) { try { npmrc = await readFile(resolvePath(homedir(), '.npmrc'), 'utf8'); npmrc = parseIni(npmrc); + authToken = npmrc['//registry.npmjs.org/:_authToken']; } catch (err) { // Do nothing } } } - const authToken = npmrc['//registry.npmjs.org/:_authToken']; - if (this._debug) console.time('> [debug] Getting files'); const files = await getFiles(path, pkg, { debug: this._debug }); if (this._debug) console.timeEnd('> [debug] Getting files'); From 7ab8e0d344cef463048bc3444a6cf2aecc961514 Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Sun, 17 Jul 2016 12:54:43 +0300 Subject: [PATCH 5/5] Add cmd line flag to forward NPM auth token --- bin/now-deploy | 9 ++++++--- lib/index.js | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bin/now-deploy b/bin/now-deploy index 22174ff..483fa06 100755 --- a/bin/now-deploy +++ b/bin/now-deploy @@ -14,7 +14,7 @@ import ms from 'ms'; import { handleError, error } from '../lib/error'; const argv = minimist(process.argv.slice(2), { - boolean: ['help', 'version', 'debug', 'force', 'login', 'no-clipboard'], + boolean: ['help', 'version', 'debug', 'force', 'login', 'no-clipboard', 'forward-npm'], alias: { help: 'h', debug: 'd', @@ -22,7 +22,8 @@ const argv = minimist(process.argv.slice(2), { force: 'f', forceSync: 'F', login: 'L', - 'no-clipboard': 'C' + 'no-clipboard': 'C', + 'forward-npm': 'N' } }); @@ -47,6 +48,7 @@ const help = () => { -f, --force force a new deployment even if nothing has changed -L, --login configure login -C, --no-clipboard do not attempt to copy URL to clipboard + -N, --forward-npm Forward login information to install private NPM modules ${chalk.dim('Examples:')} @@ -93,6 +95,7 @@ const exit = (code) => { // options const debug = argv.debug; const clipboard = !argv['no-clipboard']; +const forwardNpm = argv['forward-npm']; const force = argv.force; const forceSync = argv.forceSync; const shouldLogin = argv.login; @@ -138,7 +141,7 @@ async function sync (token) { const now = new Now(apiUrl, token, { debug }); try { - await now.create(path, { forceNew: force, forceSync: forceSync }); + await now.create(path, { forceNew: force, forceSync: forceSync, forwardNpm }); } catch (err) { handleError(err); process.exit(1); diff --git a/lib/index.js b/lib/index.js index 896aacc..571fcbd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -30,7 +30,7 @@ export default class Now extends EventEmitter { this._onRetry = this._onRetry.bind(this); } - async create (path, { forceNew, forceSync }) { + async create (path, { forceNew, forceSync, forwardNpm }) { this._path = path; try { @@ -65,10 +65,12 @@ export default class Now extends EventEmitter { const nowProperties = pkg.now || {}; + forwardNpm = forwardNpm || nowProperties['forward-npm']; + // Read .npmrc let npmrc = {}; let authToken; - if (nowProperties['forward-npm']) { + if (forwardNpm) { try { npmrc = await readFile(resolvePath(path, '.npmrc'), 'utf8'); npmrc = parseIni(npmrc);