forked from alibaba/ice
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
79 lines (74 loc) · 2 KB
/
utils.js
File metadata and controls
79 lines (74 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const { resolve } = require('path');
const { readFileSync } = require('fs');
const axios = require('axios');
function keys(obj) {
return Object.keys(obj);
}
exports.keys = keys;
function getCwd() {
return process.cwd();
}
function findMaterials() {
const pkg = getPkgJson();
if (!('materials' in pkg)) {
return null;
}
return pkg.materials;
}
exports.findMaterials = findMaterials;
function getPkgJson() {
const cwd = getCwd();
const json = JSON.parse(readFileSync(resolve(cwd, 'package.json'), 'utf-8'));
return json;
}
exports.getPkgJson = getPkgJson;
/**
* 检测 NPM 包是否已发送,并返回包的发布时间
* @param {string} npm package name
* @param {String} version pacage version
* @param {String} registry npm registry
* @return {array}
* [code, resute]
*/
function checkAndQueryNpmTime(
npm,
version = 'latest',
registry = 'http://registry.npmjs.com'
) {
const packageRegistryUrl = `${registry}/${npm.replace(/\//g, '%2f')}/`;
return axios(packageRegistryUrl)
.then((response) => response.data)
.then((data) => {
if (!data.time) {
throw new Error('time 字段不存在');
}
if (
!data.versions ||
typeof data.versions[data['dist-tags'][version] || version] ===
'undefined'
) {
console.log(packageRegistryUrl);
throw new Error(`${npm}@${version} 未发布! 禁止提交!`);
}
return [0, data.time];
})
.catch((err) => {
if (err.response && err.response.status === 404) {
// 这种情况是该 npm 包名一次都没有发布过
return [1, {
error: err,
npm: npm,
version: version,
message: '[ERR checkAndQueryNpmTime] 未发布的 npm 包',
}]
} else {
return [1, {
error: err,
npm: npm,
version: version,
message: `[ERR checkAndQueryNpmTime] ${err.message}`,
}]
}
});
}
exports.checkAndQueryNpmTime = checkAndQueryNpmTime;