cybertron-utils
Version:
cybertron components manage
85 lines (75 loc) • 2.28 kB
JavaScript
const path = require('path');
const fs = require('fs');
const axios = require('axios');
const chalk = require('chalk');
const { hasGit, getCommitHash, checkIsPush } = require('../utils/git');
const Require = require('../utils/require');
const cwd = process.cwd();
const COMPONENT_SRC_PATH = path.resolve(cwd, './src');
const COMPONENT_DIST_PATH = path.resolve(cwd, './dist');
const { get } = require('../utils/config');
module.exports = {
command: 'upload',
desc: '上传组件',
handler(argv) {
const config = Require(cwd, './src/config.js');
const pkg = Require(cwd, './package.json');
const { url, token } = get();
// token
if (!token) {
console.log(chalk.red('请先登陆!'));
return;
}
// 检查目录
if (!fs.existsSync(COMPONENT_DIST_PATH) || !fs.existsSync(COMPONENT_DIST_PATH)) {
console.error(chalk.red('没有找到src或者dist目录!'));
return;
}
const fetchUrl = hasGit();
if (!fetchUrl) {
console.error(chalk.red('没有找到git remote!'));
return;
}
const componentHash = getCommitHash();
const isPush = checkIsPush();
if(!isPush){
return console.log(chalk.red('请先提交代码到git仓库'));
}
const uploadComponents = [];
for (let key in config) {
const curr = config[key];
const { version } = curr;
// 检查是否存在文件
if (!fs.existsSync(path.resolve(COMPONENT_SRC_PATH, key, './js/index.jsx'))) {
console.log(chalk.red(`没有找到${key}入口文件!`));
} else {
const installInfo = {
name: key,
version,
dependencies: {
...pkg.dependencies
}
};
uploadComponents.push(installInfo);
}
}
const uploadData = {
group: pkg["cybertron-group"],
gitFetchPath: fetchUrl,
components: uploadComponents,
componentHash
}
axios({
method: 'post',
headers: {
token
},
url: `${url}/component/install`,
data: uploadData,
}).then(function (response) {
console.log(chalk.green(response.data.message));
}).catch(e => {
console.log(chalk.red(e.response ? e.response.data.message : e.message))
});
}
};