nuke-debug-util
Version:
qap调试库函数
160 lines (143 loc) • 5.73 kB
JavaScript
;
const path = require('path');
const fs = require('fs-extra');
const archiver = require('archiver');
const work_path = process.cwd();
const util = require('./util');
const fetch = require('node-fetch');
const FallbackPort = require('fallback-port');
const fallback = new FallbackPort(5500);
const chalk = require('chalk');
const PORT = fallback.getPort(); // qrzip服务的默认端口号
/**
* 生成二维码用于将当前环境下的某个文件夹上传至客户端,模拟线上环境
* @param {string} projectPath 项目根目录路径
* @param {string} filename zip包名字 默认为项目名.zip
* @param {string} outputPath 文件输出路径
* @return {object} {status:true,false,url:'',info}
*/
module.exports.qrzip = function (projectPath, filename, outputPath, port) {
try {
var pkgObj = require(path.join(projectPath, 'package.json'));
if (!filename || filename.length == 0) {
filename = pkgObj.name;
}
var _outputDir = path.join(projectPath, outputPath);
var zipUrl = path.join(_outputDir, filename.split('.')[0] + '.zip');
if (!fs.existsSync(zipUrl)) {
return { status: false, info: 'zip not fount' }
} else {
port || PORT;
outputPath ? outputPath : outputPath = '_output';
return { status: true, url: local2NetUrl(projectPath, zipUrl, filename, port), info: 'success' };
}
} catch (e) {
return { status: false, info: JSON.stringify(e) };
}
}
/**
* qap压缩zip包
* @param {string} zipPath 打包源地址
* @param {string} destZipFile 目标地址
*/
module.exports.zip = function (zipPath, destZipFile) {
if (fs.existsSync(destZipFile)) {
fs.removeSync(destZipFile);
}
fs.ensureDirSync(path.resolve(work_path, '_output/'));
var zip_output = fs.createWriteStream(destZipFile);
var archive = archiver('zip', {
level: 9
});
zip_output.on('close', function () {
console.log(destZipFile + ":" + (archive.pointer() / 1000).toFixed(2) + ' total KB');
});
archive.on('error', function (err) {
console.log('compress error', err);
});
archive.pipe(zip_output);
let h5_path = path.join(work_path, 'h5');
let image_path = path.join(work_path, 'image');
let icon_path = path.join(work_path, 'iconfont');
let public_path = path.join(work_path, 'public');
let lifecycle_path = path.join(work_path,'lifecycle');
if (fs.existsSync(h5_path)) {
warningEmptyPath(h5_path);
archive.directory(h5_path, 'h5');
}
if (fs.existsSync(image_path)){
warningEmptyPath(image_path);
archive.directory(image_path, 'image');
}
if (fs.existsSync(path.join(work_path, 'qap.json'))) {
archive.file('qap.json');
}
archive.directory(zipPath, 'qap');
if (fs.existsSync(icon_path)) {
warningEmptyPath(icon_path);
archive.directory(icon_path, 'iconfont');
}
if (fs.existsSync(public_path)) {
warningEmptyPath(public_path);
archive.directory(public_path, 'public');
}
if (fs.existsSync(lifecycle_path)) {
warningEmptyPath(lifecycle_path);
archive.directory(lifecycle_path, 'lifecycle');
}
archive.finalize();
}
function local2NetUrl(projectPath, zipUrl, filename, port) {
var relativePath = path.relative(projectPath, zipUrl);
var localProxy = 'http://' + util.getIp() + ':' + port + '/' + relativePath;
var _qap_package = '?_qap_package=true&_app_id=' + filename.split('.')[0];
var timeStamp = "&t=" + (new Date().getTime());
return (localProxy + _qap_package + timeStamp).replace('\\', '/')
}
/**
* 返回ios与android的下载地址。
*/
module.exports.qianniu = function () {
return new Promise((resolve, reject) => {
fetch('http://open.taobao.com/docs/doc.htm?isPublish=0&docType=1&articleId=105523').then((res) => {
return res.text()
}).then(body => {
resolve({
status: true,
content: {
androidDev: getAndroidDownloadUrl(body, ['qap_android_dev_download', '_blank', 'http', '.apk"']),
androidStable: getAndroidDownloadUrl(body, ['qap_android_download', '_blank', 'http', '.apk"']),
androidX86: getAndroidDownloadUrl(body, ['qap_android_x86_download', '_blank', 'http', '.apk"']),
iosDev: getIOSDownloadUrl(body, ['qap_ios_dev_download', '_blank', 'href="', 'href="']),
iosStable: getIOSDownloadUrl(body, ['qap_ios_download', '_blank', 'href="', 'href="'])
}
})
}).catch((err) => {
reject({
status: false,
content: err
})
})
})
}
/**
* @author 小明
*/
function getAndroidDownloadUrl(body, tags) {
var android_url_index = body.indexOf(tags[0]);
var android_url = body.substring(android_url_index, body.indexOf(tags[1], android_url_index));
return android_url.substring(android_url.indexOf(tags[2]), android_url.indexOf(tags[3]) + 4);
}
function getIOSDownloadUrl(body, tags) {
var ios_url_index = body.indexOf(tags[0]);
var ios_url = body.substring(ios_url_index, body.indexOf(tags[1], ios_url_index));
return ios_url.substring(ios_url.indexOf(tags[2]) + 6,
ios_url.indexOf('"', ios_url.indexOf(tags[3]) + 10));
}
function warningEmptyPath(aPath){
let emptyFolder = util.getEmptyFolder(aPath);
if(emptyFolder.length >0){
console.log(chalk.yellow(`[WARN]:${aPath}目录下存在空文件夹,强烈建议删除后重新打包`));
console.log(JSON.stringify(emptyFolder));
};
}