Skip to content

Commit

Permalink
feat: Support private packages (#3)
Browse files Browse the repository at this point in the history
* use npm view to get remote package information

* fix small typo in README.md

* unify name of parameter to packageName

* reject with error message
  • Loading branch information
mroswald authored and azu committed Apr 12, 2018
1 parent e8ea937 commit 7a43e19
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ All check list is passed, exit status will be `0`.
- [x] Check that the package's name is valid
- [validate-npm-package-name](https://github.com/npm/validate-npm-package-name "validate-npm-package-name")
- [x] Check that the package is not `private:true`
- [x] Check that `pacakge@version` is already published in npm registry
- [x] Check that `package@version` is already published in npm registry

## Install

Expand Down
66 changes: 38 additions & 28 deletions lib/can-npm-publish.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT © 2018 azu
"use strict";
const fetch = require("node-fetch");
const spawn = require("child_process").spawn;
const readPkg = require("read-pkg");
const validatePkgName = require("validate-npm-package-name");
/**
Expand Down Expand Up @@ -31,6 +31,37 @@ const checkPrivateField = packagePath => {
});
};

/**
* Return Promise which resolves with an array of version numbers for the package
* or rejects if anything failed
* @param packageName
* @returns {Promise}
*/
const viewPackage = packageName => {
return new Promise((resolve, reject) => {
const view = spawn("npm", ["view", packageName, "versions", "--json"]);
let result = "";
let errorResult = "";

view.stdout.on("data", data => {
result += data.toString();
});

view.stderr.on("data", err => {
errorResult += err.toString();
});

view.on("close", code => {
if (code > 0) {
reject(new Error(errorResult));
return;
}

resolve(JSON.parse(result));
});
});
};

const checkAlreadyPublish = packagePath => {
return readPkg(packagePath).then(pkg => {
const name = pkg["name"];
Expand All @@ -41,33 +72,12 @@ const checkAlreadyPublish = packagePath => {
if (version === undefined) {
return Promise.reject(new Error("This package has not `version`."));
}
// https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#getpackageversion
// @scope/name => @scope%2Fname
const encodedName = name.replace(/\//g, "%2F");
return fetch(`https://registry.npmjs.com/${encodedName}`)
.then(response => {
if (response.status === 404) {
// not published yet
return {
versions: []
};
}
if (!response.ok) {
return Promise.reject(new Error(response.statusText));
}
return response.json();
})
.then(json => {
if (json.error) {
// {"error":"version not found: 18.0.0"}
return Promise.reject(new Error(json.error));
}
const versions = json["versions"];
if (versions[version]) {
return Promise.reject(new Error(`${name}@${version} is already published`));
}
return;
});
return viewPackage(name).then(versions => {
if (versions.includes(version)) {
return Promise.reject(new Error(`${name}@${version} is already published`));
}
return;
});
});
};
const canNpmPublish = packagePath => {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
},
"dependencies": {
"meow": "^4.0.0",
"node-fetch": "^1.7.3",
"read-pkg": "^3.0.0",
"validate-npm-package-name": "^3.0.0"
},
Expand Down

0 comments on commit 7a43e19

Please sign in to comment.