Skip to content

Commit

Permalink
feat: check npm published before generate (alibaba#450)
Browse files Browse the repository at this point in the history
* feat: check npm published before generate

* fix: 兼容 tag 方式
  • Loading branch information
wssgcg1213 authored and noyobo committed May 4, 2018
1 parent f4624f1 commit 1100990
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
6 changes: 3 additions & 3 deletions tools/ice-devtools/database/generate-marterials-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const os = require('os');
const uppercamelcase = require('uppercamelcase');
const rp = require('request-promise');
const depAnalyze = require('../shared/dep-analyze');
const { queryNpmTime } = require('../shared/utils');
const { checkAndQueryNpmTime } = require('../shared/utils');

function generatePartciple(payload, source) {
if (process.env.PARTICIPLE) {
Expand Down Expand Up @@ -137,7 +137,7 @@ function generateBlocks(files, SPACE, type, done) {
if (item.source.type !== 'npm') {
return Promise.resolve();
} else {
return queryNpmTime(item.source.npm)
return checkAndQueryNpmTime(item.source.npm, item.source.version)
.then(({ created, modified }) => {
item.publishTime = created;
item.updateTime = modified;
Expand Down Expand Up @@ -188,7 +188,7 @@ function generateScaffolds(files, SPACE, done) {
};

tasks.push(
queryNpmTime(pkg.name)
checkAndQueryNpmTime(pkg.name, pkg.version)
.then(({ created, modified }) => {
payload.publishTime = created;
payload.updateTime = modified;
Expand Down
31 changes: 19 additions & 12 deletions tools/ice-devtools/shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,34 @@ function getPkgJson() {
}
exports.getPkgJson = getPkgJson;

function queryNpmTime(
function checkAndQueryNpmTime(
npm,
version = 'latest',
registry = 'http://registry.npmjs.com'
) {
return axios(`${registry}/${npm.replace(/\//g, '%2f')}/`)
.then((response) => response.data)
.then((data) => data.time)
.then((data) => {
if (!data.time) {
throw new Error('time 字段不存在');
}
if (
!data.version ||
typeof data.versions[data['dist-tags'][version] || version] ===
'undefined'
) {
throw new Error(`${npm}@${version} 未发布! 禁止提交!`);
}
return data.time;
})
.catch((err) => {
if (err.response && err.response.status === 404) {
console.error(
'[WARN queryNpmTime] 未发布的 npm 包',
npm,
'@',
version,
'发布时间和更新时间为 Null'
);
// 这种情况是该 npm 包名一次都没有发布过
console.error('[ERR checkAndQueryNpmTime] 未发布的 npm 包', npm);
} else {
console.error('[WARN queryNpmTime] failed request with err');
console.error(`[ERR checkAndQueryNpmTime] ${err.message}`, err);
}
throw err;
process.exit(1);
});
}
exports.queryNpmTime = queryNpmTime;
exports.checkAndQueryNpmTime = checkAndQueryNpmTime;

0 comments on commit 1100990

Please sign in to comment.