Skip to content

Commit f347a66

Browse files
committed
add lib
1 parent a81afbe commit f347a66

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

Diff for: .gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ crashlytics.properties
8282
crashlytics-build.properties
8383

8484

85-
/lib
8685
### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Node.gitignore
8786

8887
# Logs

Diff for: lib/can-npm-publish.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// MIT © 2018 azu
2+
"use strict";
3+
const fetch = require("node-fetch");
4+
const readPkg = require("read-pkg");
5+
/**
6+
* Return rejected promise if the package is not `private:true`
7+
* @param packagePath
8+
* @returns {Promise}
9+
*/
10+
const checkPrivateField = packagePath => {
11+
return readPkg(packagePath).then(pkg => {
12+
if (pkg["private"] === true) {
13+
return Promise.reject(new Error("This package is private."));
14+
}
15+
});
16+
};
17+
18+
const checkAlreadyPublish = packagePath => {
19+
return readPkg(packagePath).then(pkg => {
20+
const name = pkg["name"];
21+
const version = pkg["version"];
22+
if (name === undefined) {
23+
return Promise.reject(new Error("This package has not `name`."));
24+
}
25+
if (version === undefined) {
26+
return Promise.reject(new Error("This package has not `version`."));
27+
}
28+
// https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#getpackageversion
29+
return fetch(`https://registry.npmjs.com/${encodeURIComponent(name)}`)
30+
.then(response => {
31+
if (response.status === 404) {
32+
// not published yet
33+
return {
34+
versions: []
35+
};
36+
}
37+
if (!response.ok) {
38+
return Promise.reject(new Error(response.statusText));
39+
}
40+
return response.json();
41+
})
42+
.then(json => {
43+
if (json.error) {
44+
// {"error":"version not found: 18.0.0"}
45+
return Promise.reject(new Error(json.error));
46+
}
47+
const versions = json["versions"];
48+
if (versions[version]) {
49+
return Promise.reject(new Error(`${name}@${versions} is already published`));
50+
}
51+
return;
52+
});
53+
});
54+
};
55+
const canNpmPublish = packagePath => {
56+
return Promise.all([checkAlreadyPublish(packagePath), checkPrivateField(packagePath)]);
57+
};
58+
module.exports.canNpmPublish = canNpmPublish;

0 commit comments

Comments
 (0)