Commit 9721f73b authored by Stepheno's avatar Stepheno

(feat) scilla-components-cli

parent 97cbe806
File added
exports.prodBaseURI = 'http://10.10.35.241:8080'
exports.devBaseURI = 'http://127.0.0.1:8080'
\ No newline at end of file
const fs = require('fs')
const FormData = require('form-data')
const request = require('request')
const path = require('path')
const os = require('os')
const extract = require('extract-zip')
const chalk = require('chalk')
const userName = require('git-user-name');
const zlib = require('zlib')
const {generateZip, nil, rmdirSync, mkdirAll} = require('../tools')
const componentsDir = path.resolve(os.homedir(), '.scilla/components');
const {devBaseURI, prodBaseURI} = require('./conf')
const baseURI = prodBaseURI
const error = chalk.bold.red
const success = chalk.bold.green
if (!fs.existsSync(componentsDir)) {
fs.mkdirSync(componentsDir)
}
exports.uploadComponent = async (dirname, version, introduction) => {
await generateZip(dirname)
const form = new FormData()
form.append('name', dirname)
form.append('username', userName())
form.append('version', version)
form.append('file', fs.createReadStream(`./${dirname}.zip`))
if (introduction) {
const introductionPath = path.join(process.cwd(), introduction)
form.append(
'introduction',
fs.existsSync(introductionPath)
? fs.readFileSync(introductionPath, 'utf8')
: introduction
)
}
form.submit(`${baseURI}/api/files`, (err, res) => {
if (err) {
throw err
}
if (res.statusCode === 200) {
res.on('data', chunk => {
const {code, msg} = JSON.parse(String(chunk))
if (code === 0) {
console.log(success('上传成功'))
} else {
console.log(error(msg))
}
})
}
fs.unlink(`./${dirname}.zip`, err => {
if (err) {
console.log(error(`./${dirname}.zip 文件删除失败,请手动删除`))
}
})
})
}
exports.downloadComponent = async (componentName, version) => {
if (!componentName) {
console.log(error('请输入组件名'))
return
}
const componentPath = path.join(componentsDir, componentName)
let requestURI = `${baseURI}/api/files/component?name=${componentName}`
if (version) {
requestURI += `?version=${version}`
}
request
.get(requestURI, (err, res, body) => {
if (err) return console.log(error('下载失败,请重试'))
if (res.statusCode === 200) {
body = JSON.parse(body)
if (body.code === 0) {
loadComponent(componentPath, componentName, body.data.version)
} else {
console.log(error(body.msg))
}
} else {
console.log(error('下载失败, 请重试'))
}
})
}
function loadComponent(componentPath, componentName, version) {
const params = `name=${componentName}${version ? `&version=${version}` : ''}`
const zipPath = path.join(componentPath, version) + '.zip'
mkdirAll(componentsDir, path.dirname(zipPath))
// 删掉 已有 zip
if (fs.existsSync(zipPath)) fs.unlinkSync(zipPath)
// 删除 已版本
if (fs.existsSync(zipPath.replace('.zip', ''))) {
rmdirSync(zipPath.replace('.zip', ''))
}
request(`${baseURI}/api/files?${params}`)
.on('response', () => {
try {
const res = JSON.parse(fs.readFileSync(zipPath, 'utf8'))
console.log(error(res.msg))
fs.unlink(zipPath, console.log)
return
} catch (err) {
// 不需处理
}
extract(zipPath, {dir: path.dirname(zipPath)}, err => {
if (err) {
console.log('err')
console.log(error(err));
} else {
fs.unlinkSync(zipPath)
console.log(success('下载成功'))
}
})
})
.pipe(fs.createWriteStream(zipPath))
}
exports.getComponentVersions = componentName => {
if (!componentName) return console.log(error('请输入组件名称'))
request.get(`${baseURI}/api/files/versions?name=${componentName}`, (err, response, body) => {
if (err) return console.log(error(err))
const res = JSON.parse(body)
if (response.statusCode === 200) {
if (res.code === 0) {
if (!res.data.versions) {
return console.log('暂无版本')
}
res.data.versions.forEach((version, index, list) => {
if (index === list.length - 1) {
console.log(success(version))
} else {
console.log(version)
}
})
} else {
console.log(res.msg)
}
}
})
}
exports.deleteComponent = (componentName, version) => {
if (!componentName) return console.log('请输入 想要删除的包名')
request.delete(
`${baseURI}/api/files?name=${componentName}${version ? `&version=${version}` : ''}`,
(err, response, body) => {
if (err) return console.log(error(err))
const res = JSON.parse(body)
if (response.statusCode === 200) {
if (res.code === 0) {
console.log(success(`${componentName} ${version ? `[${version}`: ''} 删除成功`))
} else {
console.log(error(res.msg))
}
}
}
)
}
\ No newline at end of file
const program = require('commander')
const {deleteComponent} = require('./component')
const {exit} = require('./tools')
program
.option('-p, --package [string]', 'PackageName to upload')
.option('-v, --versionCode [string]', 'The version need to be deleted')
.parse(process.argv)
async function execute() {
deleteComponent(program.package, program.versionCode)
}
execute().catch(exit)
\ No newline at end of file
const program = require('commander')
const {getComponentVersions} = require('./component')
const {exit} = require('./tools')
program
.option('-p, --package [string]', 'the package is searched by.')
.parse(process.argv)
async function execute() {
getComponentVersions(program.package)
}
execute().catch(exit)
\ No newline at end of file
const program = require('commander')
const {downloadComponent} = require('./component')
const {exit} = require('./tools')
program
.option('-p, --package [string]', 'Package to download')
.option('-v, --version-code [string]', 'Pakeage version. eg: x.x.x')
.parse(process.argv)
async function execute() {
downloadComponent(program.package, program.versionCode)
}
execute().catch(exit)
\ No newline at end of file
const program = require('commander')
const {uploadComponent} = require('./component')
const {exit} = require('./tools')
program
.option('-p, --package [string]', 'PackageName to upload')
.option('-v, --version [string]', 'Pakeage version. eg: x.x.x')
.option('-m, --introduction [string]', 'Package\'s introduction')
.parse(process.argv)
async function execute() {
uploadComponent(program.package, program.version, program.introduction)
}
execute().catch(exit)
\ No newline at end of file
......@@ -12,4 +12,8 @@ program
.command('dev', 'Build project automatic').alias('d')
.command('build', 'Publish project').alias('b')
.command('upload', 'Upload project to oss').alias('u')
.command('upload-component', 'Upload Component to server').alias('uc')
.command('load-component', 'Download Component to local file-system').alias('l')
.command('get-versions', 'Get versions of the given component').alias('g')
.command('delete-component', 'delete component').alias('rm')
.parse(process.argv);
......@@ -5,6 +5,7 @@
const {spawn} = require('child_process');
const fs = require('fs-extra');
const path = require('path');
const archiver = require('archiver')
exports.exit = function (err, code = 1) {
console.error(err);
......@@ -151,3 +152,51 @@ exports.executeBuildProcess = function (cmd) {
}
)
};
exports.generateZip = dirname => {
return new Promise(resolve => {
const archive = archiver('zip')
archive.on('err', err => {
throw err
})
const output = fs.createWriteStream(`./${dirname}.zip`)
archive.directory(dirname)
archive.pipe(output)
archive.finalize()
archive.on('end', () => {
setTimeout(resolve, 500)
})
})
}
exports.nil = () => {}
/*
* 删除文件夹
*/
exports.rmdirSync = function(p) {
const files = fs.readdirSync(p);
files.forEach(file => {
const target = path.join(p, file);
if (fs.lstatSync(target).isDirectory()) {
rmdirSync(target);
} else {
fs.unlinkSync(target);
}
})
fs.rmdirSync(p);
}
/* 生成文件夹 */
exports.mkdirAll = function(from, to) {
const relative = path.relative(from, to)
const splits = relative.split('/')
splits.reduce((pre, cur) => {
const p = path.join(pre, cur)
if (!fs.existsSync(p)) {
fs.mkdirSync(p)
}
return p
}, from)
}
\ No newline at end of file
......@@ -11,11 +11,16 @@
"license": "ISC",
"dependencies": {
"ali-oss": "^6.0.1",
"chalk": "^2.4.1",
"archiver": "^3.0.0",
"chalk": "^2.4.2",
"commander": "^2.18.0",
"extract-zip": "^1.6.7",
"form-data": "^2.3.3",
"fs-extra": "^6.0.1",
"git-user-name": "^2.0.0",
"global-modules": "^2.0.0",
"progress": "^2.0.0",
"request": "^2.88.0",
"serve-handler": "^5.0.5",
"yarn-config-directory": "^1.0.2"
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment