Skip to content

Commit

Permalink
feat: add mdb sync
Browse files Browse the repository at this point in the history
  • Loading branch information
wssgcg1213 committed May 4, 2018
1 parent 53e2370 commit 4aebaa8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
6 changes: 6 additions & 0 deletions scripts/sync-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const co = require('co');
const { readdirSync } = require('fs');
const { resolve, join } = require('path');

if (process.env.TRAVIS_BRANCH !== 'master') {
console.log('当前分支非 Master, 不执行物料源同步脚本');
console.log('TRAVIS_BRANCH=' + process.env.TRAVIS_BRANCH);
process.exit(0);
}

const bucket = 'iceworks';
const accessKeyId = process.env.ACCESS_KEY_ID;
const accessKeySecret = process.env.ACCESS_KEY_SECRET;
Expand Down
51 changes: 43 additions & 8 deletions tools/ice-devtools/database/generate-marterials-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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');

function generatePartciple(payload, source) {
if (process.env.PARTICIPLE) {
Expand All @@ -30,7 +31,7 @@ function generatePartciple(payload, source) {
* @param {*} SPACE
* @param {String} type | block or react
*/
function generateBlocks(files, SPACE, type) {
function generateBlocks(files, SPACE, type, done) {
const result = [];
files.forEach((pkgPath) => {
const pkg = JSON.parse(fs.readFileSync(path.join(SPACE, pkgPath)));
Expand Down Expand Up @@ -80,7 +81,7 @@ function generateBlocks(files, SPACE, type) {
screenshot: pkgConfig.screenshot || pkgConfig.snapshot,

categories: pkgConfig.categories || [],
publishTime: pkg.publishTime || new Date().toISOString(),
// publishTime: pkg.publishTime || new Date().toISOString(),
features: {
useComponents,
},
Expand Down Expand Up @@ -131,11 +132,30 @@ function generateBlocks(files, SPACE, type) {
result.push(payload);
});

return result;
Promise.all(
result.map((item) => {
if (item.source.type !== 'npm') {
return Promise.resolve();
} else {
return queryNpmTime(item.source.npm)
.then(({ created, modified }) => {
item.publishTime = created;
item.updateTime = modified;
})
.catch((err) => {
item.publishTime = null;
item.updateTime = null;
});
}
})
).then(() => {
done(result);
});
}

function generateScaffolds(files, SPACE) {
return files.map((pkgPath) => {
function generateScaffolds(files, SPACE, done) {
const tasks = [];
const result = files.map((pkgPath) => {
const pkg = JSON.parse(fs.readFileSync(path.join(SPACE, pkgPath)));
const dependencies = pkg.dependencies || {};
const devDependencies = pkg.devDependencies || {};
Expand Down Expand Up @@ -163,10 +183,22 @@ function generateScaffolds(files, SPACE) {
screenshot: pkg.scaffoldConfig.screenshot || pkg.scaffoldConfig.snapshot,

categories: pkg.scaffoldConfig.categories || [],
publishTime: pkg.publishTime || new Date().toISOString(),
// publishTime: pkg.publishTime || new Date().toISOString(),
features: {},
};

tasks.push(
queryNpmTime(pkg.name)
.then(({ created, modified }) => {
payload.publishTime = created;
payload.updateTime = modified;
})
.catch((err) => {
item.publishTime = null;
item.updateTime = null;
})
);

generatePartciple(payload, {
title: pkg.scaffoldConfig.title,
content: pkg.description,
Expand Down Expand Up @@ -211,6 +243,9 @@ function generateScaffolds(files, SPACE) {

return payload;
});
Promise.all(tasks).then(() => {
done(result);
});
}

/**
Expand All @@ -231,7 +266,7 @@ function gatherBlocksOrLayouts(pattern, SPACE, type) {
console.log('err:', err);
reject(err);
} else {
resolve(generateBlocks(files, SPACE, type));
generateBlocks(files, SPACE, type, resolve);
}
}
);
Expand All @@ -256,7 +291,7 @@ function gatherScaffolds(pattern, SPACE) {
console.log('err:', err);
reject(err);
} else {
resolve(generateScaffolds(files, SPACE));
generateScaffolds(files, SPACE, resolve);
}
}
);
Expand Down
1 change: 1 addition & 0 deletions tools/ice-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"license": "MIT",
"dependencies": {
"async": "^2.6.0",
"axios": "^0.18.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-plugin-component": "^1.1.0",
Expand Down
26 changes: 26 additions & 0 deletions tools/ice-devtools/shared/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { resolve } = require('path');
const { readFileSync } = require('fs');
const axios = require('axios');

function keys(obj) {
return Object.keys(obj);
Expand All @@ -25,3 +26,28 @@ function getPkgJson() {
return json;
}
exports.getPkgJson = getPkgJson;

function queryNpmTime(
npm,
version = 'latest',
registry = 'http://registry.npmjs.com'
) {
return axios(`${registry}/${npm.replace(/\//g, '%2f')}/`)
.then((response) => response.data)
.then((data) => data.time)
.catch((err) => {
if (err.response && err.response.status === 404) {
console.error(
'[WARN queryNpmTime] 未发布的 npm 包',
npm,
'@',
version,
'发布时间和更新时间为 Null'
);
} else {
console.error('[WARN queryNpmTime] failed request with err');
}
throw err;
});
}
exports.queryNpmTime = queryNpmTime;

0 comments on commit 4aebaa8

Please sign in to comment.