Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to pass Auth header #23

Merged
merged 4 commits into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous pattern was not matching because all of the test files are at the root of the src folder. Now the tests are not being built into lib 💃

"prepare": "npm run build"
},
"preferGlobal": true,
Expand Down
19 changes: 12 additions & 7 deletions src/cli.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ program
"--dry-run",
"Do not install packages, but show the install command that will be run"
)
.option(
"-a, --auth <token>",
"Provide an NPM authToken for private packages."
)
.usage("<package>[@<version>], default version is 'latest'")
.parse(process.argv);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the linter can't make up it's mind regarding where it wants to do those line breaks...

} ${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);
Expand Down
18 changes: 11 additions & 7 deletions src/install-peerdeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ function encodePackageName(packageName) {
* @param {string} requestInfo.registry - the URI of the registry on which the package is hosted
* @returns {Promise<Object>} - 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) {
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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}:`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this is something that was picked up by the autolinting and changed.

);
console.log(commandString);
} else {
Expand Down