@ccos/ccweb-cli
Version:
Coocaa WebOS APP Command-Line Tools.
259 lines (225 loc) • 7.54 kB
JavaScript
const readline = require('readline');
const path = require('path');
const fs = require('fs');
const compressing = require('compressing');
const extract = require('./makeExample');
const programVersion = 'v1.0.1';
const programName = '酷开 WebOS APP 程序产生器工具';
let args = null;
let projectName = '';
start1();
//console.log(process.argv);
//console.log(args);
function showVersion() {
console.log('');
console.log(programName + ' ' + programVersion);
console.log('');
}
function showHelp() {
showVersion();
console.log('使用方法:');
console.log(' ccweb create [工程名] 新建一个工程');
console.log(' ccweb --version 显示版本号');
}
async function createProject() {
let srcType = 0;
while (true) {
let inputStr = await getNextParamFromUserInput('\n 1: 使用酷开 WebUI \n 2: 使用酷开 WebUI + Vue3\n 3: 使用酷开 WebUI + React\n\n 请选择源码类型(输入上述对应数字):');
inputStr = inputStr.trim();
if (inputStr == '1') {
srcType = 1;
break;
}
else if (inputStr == '2') {
srcType = 2;
break;
}
else if (inputStr == '3') {
srcType = 3;
break;
}
}
// 显示工程名称
console.log('');
console.log('创建工程 ' + projectName);
console.log('');
var readDir = fs.readdirSync('.');
//console.log(readDir);
// 查找是否已经存在了同名的文件或者目录
let foundIdx = readDir.indexOf(projectName);
//console.log(foundIdx);
if (foundIdx >= 0) {
console.log('当前目录已有同名文件或者目录: ' + projectName);
console.log('请更换一个工程名 !');
return;
}
// 创建目录
fs.mkdirSync(projectName);
// 切换新建的工程目录为当前目录
process.chdir(projectName);
//let curDir = process.cwd();
//console.log(curDir);
// 从网上下载样例的源码压缩包并解压
getHttpZipAndUncompress(srcType);
}
async function getNextParamFromUserInput(hint) {
let ret = '';
while (!ret) {
let pms = new Promise((resolve, reject) => {
// 创建readline接口实例
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// question方法
rl.question(hint, function(answer) {
let str = answer.trim();
// 不加close,则不会结束
rl.close();
resolve(str);
});
});
ret = await pms;
}
return ret;
}
function getHttpZipAndUncompress(srcType) {
let httpZipName = '';
if (srcType == 1) {
httpZipName = 'coocaa-webos-ui-example-svelte.zip'
} else if (srcType == 2) {
httpZipName = 'coocaa-webos-ui-example-vue3.zip'
} else if (srcType == 3) {
httpZipName = 'coocaa-webos-ui-example-react.zip'
} else {
httpZipName = 'coocaa-webos-ui-example-svelte.zip'
}
var https = require("https");
var options={
host: 'webx.coocaa.com',
path: '/webos/packages/' + httpZipName,
port: 443,
method:'GET'
};
let first = true;
let havError = false;
let tempZipName = (parseInt(Math.random() * 1000000000)).toString() + '.zip';
var req = https.request(options, (res) => {
//res.setEncoding('utf-8');
res.on('data', function(data) {
//console.log('后台返回数据');
if (first) {
first = false;
fs.writeFileSync(tempZipName, data);
} else {
fs.appendFileSync(tempZipName, data);
}
});
res.on('error', (err) => {
havError = true;
});
res.on('end', () => {
if (havError) {
// 如果发生获取网络文件错误,则使用本地 example 文件解压到当前目录
extract(srcType);
} else {
compressing.zip.uncompress(tempZipName, './')
.then(() => {
fs.unlink(tempZipName, ()=>{} );
})
}
});
});
req.end();
}
function tar2js() {
const filePath = path.join(process.cwd(), projectName);
let buffer = fs.readFileSync(filePath);
let totalSize = buffer.length;
let jsName = 'example.js';
if (totalSize == 0) {
console.log('filesize == 0');
return;
}
fs.writeFileSync(jsName, '\n');
let outputJsFile = (section, nameIdx) => {
let str = section.toString('base64');
let lineStr = 'let s' + nameIdx + ' = \'' + str + '\';\n'
fs.appendFileSync(jsName, lineStr);
};
let ptr = 0;
let symCnt = 0;
let blockNum = parseInt(totalSize / 512);
let yushu = totalSize % 512;
for (let i = 0; i < blockNum; i++) {
let section = buffer.slice(ptr, ptr + 512);
ptr += 512;
outputJsFile(section, symCnt++);
}
if (yushu != 0) {
let section = buffer.slice(ptr, ptr + yushu);
ptr += yushu;
outputJsFile(section, symCnt++);
}
// write array
fs.appendFileSync(jsName, 'let stringArray = [');
for (let i = 0; i < symCnt; i++) {
if (i % 16 == 0) {
fs.appendFileSync(jsName, '\n ');
}
fs.appendFileSync(jsName, 's' + i);
if (i != (symCnt - 1)) {
fs.appendFileSync(jsName, ', ');
}
}
fs.appendFileSync(jsName, '\n];\n\n');
fs.appendFileSync(jsName, 'module.exports = stringArray;\n');
console.log('已经输出 ' + jsName);
}
async function start1() {
let cmdType = '';
// 获得命令行参数
args = process.argv.slice(2);
// 如果没有参数,则直接显示帮助之后就退出
if (args.length == 0) {
showHelp();
process.exit(0);
}
else if (args.length == 1) {
if (args[0] == 'version' || args[0] == '-version' || args[0] == '--version' || args[0] == '-v' || args[0] == '-V') {
showVersion();
}
else if (args[0] == 'create' || args[0] == 'new' || args[0] == 'init') {
projectName = await getNextParamFromUserInput('请输入工程名称: ');
cmdType = 'create';
}
else if (args[0] == 'tar2js') {
projectName = await getNextParamFromUserInput('请输入tar文件名: ');
cmdType = 'tar2js';
}
else {
console.log('无法识别的命令 ' + args[0]);
}
}
else {
if (args[0] == 'create' || args[0] == 'new' || args[0] == 'init') {
projectName = args[1];
cmdType = 'create';
}
else if (args[0] == 'tar2js') {
projectName = args[1];
cmdType = 'tar2js';
}
else {
console.log('无法识别的命令 ' + args[0]);
}
}
// 执行命令类型
if (cmdType == 'create') {
createProject();
}
else if (cmdType == 'tar2js') {
tar2js();
}
}