From 87003f605a773355872fb056919cb6591e6ca867 Mon Sep 17 00:00:00 2001 From: Shannon Byrne Date: Fri, 9 Feb 2018 16:30:04 -0800 Subject: [PATCH 1/4] Add auth to cli and peerdependencies --- src/cli.js | 19 ++++++++++++------- src/install-peerdeps.js | 18 +++++++++++------- 2 files changed, 23 insertions(+), 14 deletions(-) mode change 100644 => 100755 src/cli.js diff --git a/src/cli.js b/src/cli.js old mode 100644 new mode 100755 index a2ed4ab..a4c004b --- a/src/cli.js +++ b/src/cli.js @@ -53,6 +53,10 @@ program "--dry-run", "Do not install packages, but show the install command that will be run" ) + .option( + "-a, --auth ", + "Provide auth token or basic auth for private packages." + ) .usage("[@], default version is 'latest'") .parse(process.argv); @@ -132,7 +136,8 @@ const options = { onlyPeers: program.onlyPeers, silent: program.silent, packageManager, - dryRun: program.dryRun + dryRun: program.dryRun, + auth: program.auth }; // Disabled this rule so we can hoist the callback @@ -179,13 +184,13 @@ function installCb(err) { console.log(`${C.errorText} ${err.message}`); process.exit(1); } - let successMessage = `${C.successText} ${ - packageName - } and its peerDeps were installed successfully.`; + let successMessage = `${ + C.successText + } ${packageName} and its peerDeps were installed successfully.`; if (program.onlyPeers) { - successMessage = `${C.successText} The peerDeps of ${ - packageName - } were installed successfully.`; + successMessage = `${ + C.successText + } The peerDeps of ${packageName} were installed successfully.`; } console.log(successMessage); process.exit(0); diff --git a/src/install-peerdeps.js b/src/install-peerdeps.js index 7a3f910..a92d86c 100644 --- a/src/install-peerdeps.js +++ b/src/install-peerdeps.js @@ -28,14 +28,19 @@ function encodePackageName(packageName) { * @param {string} requestInfo.registry - the URI of the registry on which the package is hosted * @returns {Promise} - a Promise which resolves to the JSON response from the registry */ -function getPackageData({ encodedPackageName, registry }) { +function getPackageData({ encodedPackageName, registry, auth }) { + const requestHeaders = {}; + if (auth) { + requestHeaders.Authorization = `Bearer ${auth}`; + } return request({ uri: `${registry}/${encodedPackageName}`, resolveWithFullResponse: true, // When simple is true, all non-200 status codes throw an // error. However, we want to handle status code errors in // the .then(), so we make simple false. - simple: false + simple: false, + headers: requestHeaders }).then(response => { const { statusCode } = response; if (statusCode === 404) { @@ -103,12 +108,13 @@ function installPeerDeps( dev, onlyPeers, silent, - dryRun + dryRun, + auth }, cb ) { const encodedPackageName = encodePackageName(packageName); - getPackageData({ encodedPackageName, registry }) + getPackageData({ encodedPackageName, registry, auth }) // Catch before .then because the .then is so long .catch(err => cb(err)) .then(data => { @@ -206,9 +212,7 @@ function installPeerDeps( const commandString = `${packageManager} ${args.join(" ")}\n`; if (dryRun) { console.log( - `This command would have been run to install ${packageName}@${ - version - }:` + `This command would have been run to install ${packageName}@${version}:` ); console.log(commandString); } else { From 7dab37adbad7e58038c3dc69780d72b21aad6548 Mon Sep 17 00:00:00 2001 From: Shannon Byrne Date: Fri, 9 Feb 2018 16:30:37 -0800 Subject: [PATCH 2/4] Update rules for build to exclude test files --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6d816fd..6ed59f2 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "jest", "lint": "eslint src/ --fix", - "build": "babel src --out-dir lib --ignore **/*.test.js", + "build": "babel src --out-dir lib --ignore *.test.js", "prepare": "npm run build" }, "preferGlobal": true, From d361e805c776b6d31e97315378d4bcd3bf3de70a Mon Sep 17 00:00:00 2001 From: Shannon Byrne Date: Fri, 9 Feb 2018 16:36:12 -0800 Subject: [PATCH 3/4] Update instructions to be more clear that it only takes a token. Does not support basic auth --- src/cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli.js b/src/cli.js index a4c004b..c33cf6b 100755 --- a/src/cli.js +++ b/src/cli.js @@ -55,7 +55,7 @@ program ) .option( "-a, --auth ", - "Provide auth token or basic auth for private packages." + "Provide an NPM authToken for private packages." ) .usage("[@], default version is 'latest'") .parse(process.argv); From a2e7d7b82f62f628d01eb6db0aef96c6800e2c3c Mon Sep 17 00:00:00 2001 From: Shannon Byrne Date: Fri, 9 Feb 2018 16:39:56 -0800 Subject: [PATCH 4/4] Update readme to document behavior --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 428bbd2..d677381 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,11 @@ To install from a custom registry, use the `--registry` option: `install-peerdeps my-custom-package --registry https://registry.mycompany.com`. +### Installing a Private Package +To install a private npm package (either from npm or from a registry that uses an authorization header), use the auth option: + +`install-peerdeps my-private-package --auth your-npm-auth-token` + ### Proxies To use this tool with a proxy, set the `HTTPS_PROXY` environment variable (if you're using a custom registry and it is only accessible over HTTP, though, set the `HTTP_PROXY` environment variable).