feat: rewrite ee-bin

This commit is contained in:
gaoshuaixing
2024-12-31 17:23:35 +08:00
parent d0214f026e
commit 15fd60cd8d
7 changed files with 196 additions and 456 deletions

119
ee-bin/index.js Normal file → Executable file
View File

@@ -1,10 +1,57 @@
#!/usr/bin/env node
const { program } = require('commander');
const moveScript = require('./tools/move');
const encrypt = require('./tools/encrypt');
const serve = require('./tools/serve');
const updater = require('./tools/incrUpdater');
const { move } = require('./tools/move');
const { encrypt, cleanEncrypt } = require('./tools/encrypt');
const { serveProcess } = require('./tools/serve');
const { incrUpdater } = require('./tools/incrUpdater');
/**
* dev
*/
program
.command('dev')
.description('create frontend-serve and electron-serve')
.option('--config <folder>', 'config file')
.option('--serve <mode>', 'serve mode')
.action(function() {
serveProcess.dev(this.opts());
});
/**
* build
*/
program
.command('build')
.description('building multiple resources')
.option('--config <folder>', 'config file')
.option('--cmds <flag>', 'custom commands')
.action(function() {
serveProcess.build(this.opts());
});
/**
* start
*/
program
.command('start')
.description('preview effect')
.option('--config <folder>', 'config file')
.action(function() {
serveProcess.start(this.opts());
});
/**
* exec
*/
program
.command('exec')
.description('create frontend-serve and electron-serve')
.option('--config <folder>', 'config file')
.option('--cmds <flag>', 'custom commands')
.action(function() {
serveProcess.exec(this.opts());
});
/**
* move - Moves resources
@@ -12,10 +59,10 @@ const updater = require('./tools/incrUpdater');
program
.command('move')
.description('Move multip resources')
.option('--config <folder>', 'config file', './electron/config/bin.js')
.option('--config <folder>', 'config file')
.option('--flag <flag>', 'Custom flag')
.action(function() {
moveScript.run(this.opts());
move(this.opts());
});
/**
@@ -25,9 +72,9 @@ program
.command('encrypt')
.description('Code encryption')
.option('--config <folder>', 'config file')
.option('--out <folder>', 'output directory', './public')
.option('--out <folder>', 'output directory')
.action(function() {
encrypt.run(this.opts());
encrypt(this.opts());
});
/**
@@ -38,7 +85,7 @@ program
.description('Clear the encrypted code')
.option('-d, --dir <folder>', 'clean directory')
.action(function() {
encrypt.clean(this.opts());
cleanEncrypt(this.opts());
});
/**
@@ -54,67 +101,17 @@ program
iconGen.run();
});
/**
* dev
*/
program
.command('dev')
.description('create frontend-serve and electron-serve')
.option('--config <folder>', 'config file', './electron/config/bin.js')
.option('--serve <mode>', 'serve mode')
.action(function() {
serve.dev(this.opts());
});
/**
* build
*/
program
.command('build')
.description('building multiple resources')
.option('--config <folder>', 'config file', './electron/config/bin.js')
.option('--cmds <flag>', 'custom commands')
.action(function() {
serve.build(this.opts());
});
/**
* start
*/
program
.command('start')
.description('preview effect')
.option('--config <folder>', 'config file', './electron/config/bin.js')
.action(function() {
const serve = require('./tools/serve');
serve.start(this.opts());
});
/**
* exec
*/
program
.command('exec')
.description('create frontend-serve and electron-serve')
.option('--config <folder>', 'config file', './electron/config/bin.js')
.option('--command <command>', 'Custom command')
.option('--cmds <flag>', 'custom commands')
.action(function() {
// command 选项是关键字,不再使用,改为 cmds
serve.exec(this.opts());
});
/**
* updater
*/
program
.command('updater')
.description('updater commands')
.option('--config <folder>', 'config file', './electron/config/bin.js')
.option('--config <folder>', 'config file')
.option('--asar-file <file>', 'asar file path')
.option('--platform <flag>', 'platform')
.action(function() {
updater.run(this.opts());
incrUpdater.run(this.opts());
});
program.parse();

View File

@@ -10,18 +10,11 @@ const mkdirp = require('mkdirp');
const OS = require('os');
const _basePath = process.cwd();
const defaultBin = './cmd/bin.js';
function checkConfig(prop) {
const filepath = path.join(_basePath, prop);
if (fs.existsSync(filepath)) {
return true;
}
return false;
}
function loadConfig(prop) {
const configFile = path.join(_basePath, prop);
function loadConfig(binFile) {
const binPath = binFile ? binFile : defaultBin;
const configFile = path.join(_basePath, binPath);
if (!fs.existsSync(configFile)) {
const errorTips = 'config file ' + chalk.blue(`${configFile}`) + ' does not exist !';
throw new Error(errorTips)
@@ -46,24 +39,6 @@ function loadConfig(prop) {
return result || {}
};
function loadEncryptConfig() {
const configFile = './electron/config/encrypt.js';
const filepath = path.join(_basePath, configFile);
if (!fs.existsSync(filepath)) {
const errorTips = 'config file ' + chalk.blue(`${filepath}`) + ' does not exist !';
throw new Error(errorTips)
}
const obj = require(filepath);
if (!obj) return obj;
let ret = obj;
if (is.function(obj) && !is.class(obj)) {
ret = obj();
}
return ret || {};
};
/**
* get electron program
*/
@@ -154,9 +129,7 @@ function rm(name) {
* 获取项目根目录package.json
*/
function getPackage () {
const homeDir = process.cwd();
const content = readJsonSync(path.join(homeDir, 'package.json'));
const content = readJsonSync(path.join(_basePath, 'package.json'));
return content;
}
@@ -209,8 +182,6 @@ function getPlatform(delimiter = "_", isDiffArch = false) {
module.exports = {
loadConfig,
checkConfig,
loadEncryptConfig,
getElectronProgram,
compareVersion,
isWindows,

View File

@@ -12,6 +12,7 @@
"ee-bin": "index.js"
},
"dependencies": {
"adm-zip": "^0.4.11",
"bytenode": "^1.3.6",
"chalk": "^4.1.2",
"commander": "^11.0.0",
@@ -21,8 +22,7 @@
"globby": "^10.0.0",
"is-type-of": "^1.2.1",
"javascript-obfuscator": "^4.0.2",
"mkdirp": "^2.1.3",
"adm-zip": "^0.4.11",
"json5": "^2.2.3"
"json5": "^2.2.3",
"mkdirp": "^2.1.3"
}
}

View File

@@ -5,30 +5,21 @@ const fs = require('fs');
const fsPro = require('fs-extra');
const is = require('is-type-of');
const bytenode = require('bytenode');
const crypto = require('crypto');
const JavaScriptObfuscator = require('javascript-obfuscator');
const globby = require('globby');
const chalk = require('chalk');
const Utils = require('../lib/utils');
const { loadConfig } = require('../lib/utils');
class Encrypt {
constructor(options = {}) {
// cli args
const outputFolder = options.out || './public';
const configFile = options.config || './electron/config/bin.js';
const { config, out } = options;
const outputFolder = out || './public';
this.basePath = process.cwd();
this.encryptCodeDir = path.join(this.basePath, outputFolder);
// 先从 bin config获取没有的话从 config/encrypt.js
const hasConfig = Utils.checkConfig(configFile);
if (hasConfig) {
const cfg = Utils.loadConfig(configFile);
this.config = cfg.encrypt;
}
if (!this.config) {
this.config = Utils.loadEncryptConfig();
}
const cfg = loadConfig(config);
this.config = cfg.encrypt;
this.filesExt = this.config.fileExt || ['.js'];
this.type = this.config.type || 'confusion';
@@ -36,17 +27,7 @@ class Encrypt {
this.cOpt = this.config.confusionOptions || {};
this.cleanFiles = this.config.cleanFiles || ['./public/electron'];
this.patterns = this.config.files || null;
this.specificFiles = [ 'electron/preload/bridge.js' ];
// 旧属性,将废弃
this.dirs = [];
const directory = this.config.directory || ['electron'];
for (let i = 0; i < directory.length; i++) {
let codeDirPath = path.join(this.basePath, directory[i]);
if (fs.existsSync(codeDirPath)) {
this.dirs.push(directory[i]);
}
}
this.specFiles = this.config.specificFiles || [ 'electron/preload/bridge.js' ];
this.codefiles = this._initCodeFiles();
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'cleanFiles:' + this.cleanFiles);
@@ -70,35 +51,13 @@ class Encrypt {
this.cleanCode();
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'backup start');
if (this.patterns) {
this.codefiles.forEach((filepath) => {
let source = path.join(this.basePath, filepath);
if (fs.existsSync(source)) {
let target = path.join(this.encryptCodeDir, filepath);
fsPro.copySync(source, target);
}
})
} else {
// 旧的逻辑,将废弃
for (let i = 0; i < this.dirs.length; i++) {
// check code dir
let codeDirPath = path.join(this.basePath, this.dirs[i]);
if (!fs.existsSync(codeDirPath)) {
console.log('[ee-bin] [encrypt] ERROR: backup %s is not exist', codeDirPath);
return
}
// copy
let targetDir = path.join(this.encryptCodeDir, this.dirs[i]);
console.log('[ee-bin] [encrypt] backup target Dir:', targetDir);
if (!fs.existsSync(targetDir)) {
this.mkdir(targetDir);
this.chmodPath(targetDir, '777');
}
fsPro.copySync(codeDirPath, targetDir);
this.codefiles.forEach((filepath) => {
let source = path.join(this.basePath, filepath);
if (fs.existsSync(source)) {
let target = path.join(this.encryptCodeDir, filepath);
fsPro.copySync(source, target);
}
}
})
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'backup end');
return true;
@@ -110,7 +69,7 @@ class Encrypt {
cleanCode() {
this.cleanFiles.forEach((file) => {
let tmpFile = path.join(this.basePath, file);
this.rmBackup(tmpFile);
fsPro.removeSync(tmpFile);
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'clean up tmp files:' + chalk.magenta(`${tmpFile}`));
})
}
@@ -120,29 +79,18 @@ class Encrypt {
*/
encrypt() {
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'start ciphering');
if (this.patterns) {
for (const file of this.codefiles) {
const fullpath = path.join(this.encryptCodeDir, file);
if (!fs.statSync(fullpath).isFile()) continue;
for (const file of this.codefiles) {
const fullpath = path.join(this.encryptCodeDir, file);
if (!fs.statSync(fullpath).isFile()) continue;
// 特殊文件处理
if (this.specificFiles.includes(file)) {
this.generate(fullpath, 'confusion');
continue;
}
this.generate(fullpath);
}
} else {
// 旧逻辑,将废弃
console.log('[ee-bin] [encrypt] !!!!!! please use the new encryption method !!!!!!');
for (let i = 0; i < this.dirs.length; i++) {
let codeDirPath = path.join(this.encryptCodeDir, this.dirs[i]);
this.loop(codeDirPath);
// 特殊文件处理
if (this.specFiles.includes(file)) {
this.generate(fullpath, 'confusion');
continue;
}
console.log('[ee-bin] [encrypt] !!!!!! please use the new encryption method !!!!!!');
}
this.generate(fullpath);
}
console.log(chalk.blue('[ee-bin] [encrypt] ') + 'end ciphering');
};
@@ -217,87 +165,17 @@ class Encrypt {
}, this.bOpt);
bytenode.compileFile(opt);
//fs.writeFileSync(curPath, 'require("bytenode");module.exports = require("./'+path.basename(jscFile)+'");', 'utf8');
fsPro.removeSync(curPath);
}
/**
* 移除备份
*/
rmBackup(file) {
if (fs.existsSync(file)) {
fsPro.removeSync(file);
}
return;
}
/**
* 检查文件是否存在
*/
fileExist(filePath) {
try {
return fs.statSync(filePath).isFile();
} catch (err) {
return false;
}
};
mkdir(dirpath, dirname) {
// 判断是否是第一次调用
if (typeof dirname === 'undefined') {
if (fs.existsSync(dirpath)) {
return;
}
this.mkdir(dirpath, path.dirname(dirpath));
} else {
// 判断第二个参数是否正常,避免调用时传入错误参数
if (dirname !== path.dirname(dirpath)) {
this.mkdir(dirpath);
return;
}
if (fs.existsSync(dirname)) {
fs.mkdirSync(dirpath);
} else {
this.mkdir(dirname, path.dirname(dirname));
fs.mkdirSync(dirpath);
}
}
};
chmodPath(path, mode) {
let files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach((file, index) => {
const curPath = path + '/' + file;
if (fs.statSync(curPath).isDirectory()) {
this.chmodPath(curPath, mode); // 递归删除文件夹
} else {
fs.chmodSync(curPath, mode);
}
});
fs.chmodSync(path, mode);
}
};
md5(file) {
const buffer = fs.readFileSync(file);
const hash = crypto.createHash('md5');
hash.update(buffer, 'utf8');
const str = hash.digest('hex');
return str;
}
}
const run = (options = {}) => {
function encrypt(options = {}) {
const e = new Encrypt(options);
if (!e.backup()) return;
e.encrypt();
}
const clean = (options = {}) => {
function cleanEncrypt(options = {}) {
let files = options.dir !== undefined ? options.dir : ['./public/electron'];
files = is.string(files) ? [files] : files;
@@ -311,6 +189,6 @@ const clean = (options = {}) => {
}
module.exports = {
run,
clean,
encrypt,
cleanEncrypt,
};

View File

@@ -4,7 +4,7 @@ const path = require('path');
const fs = require('fs');
const crypto = require('crypto')
const chalk = require('chalk');
const Utils = require('../lib/utils');
const { loadConfig, rm, getPackage, writeJsonSync } = require('../lib/utils');
const admZip = require('adm-zip')
/**
@@ -12,15 +12,15 @@ const admZip = require('adm-zip')
* @class
*/
module.exports = {
class IncrUpdater {
/**
* 执行
*/
run(options = {}) {
console.log('[ee-bin] [updater] Start');
const { config, asarFile, platform } = options;
const binCfg = Utils.loadConfig(config);
const binCfg = loadConfig(config);
const cfg = binCfg.updater;
if (!cfg) {
@@ -28,20 +28,21 @@ module.exports = {
return;
}
if (platform) {
this.generateFile(cfg, asarFile, platform);
} else {
this.generateFileOld(cfg, asarFile);
}
this.generateFile(cfg, asarFile, platform);
console.log('[ee-bin] [updater] End');
},
}
/**
* 生成增量升级文件
*/
generateFile(config, asarFile, platform) {
const cfg = config[platform];
if (!cfg) {
console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${platform} config does not exist`));
return;
}
let latestVersionInfo = {}
const homeDir = process.cwd();
console.log(chalk.blue('[ee-bin] [updater] ') + chalk.green(`${platform} config:`), cfg);
@@ -66,7 +67,7 @@ module.exports = {
return;
}
const packageJson = Utils.getPackage();
const packageJson = getPackage();
const version = packageJson.version;
let platformForFilename = platform;
if (platform.indexOf("_") !== -1) {
@@ -79,7 +80,7 @@ module.exports = {
zipName = path.basename(cfg.output.zip, '.zip') + `-${platformForFilename}-${version}.zip`;
const asarZipPath = path.join(homeDir, cfg.output.directory, zipName);
if (fs.existsSync(asarZipPath)) {
Utils.rm(asarZipPath);
rm(asarZipPath);
}
const zip = new admZip();
// 添加 asar 文件
@@ -114,107 +115,17 @@ module.exports = {
const jsonName = path.basename(cfg.output.file, '.json') + `-${platformForFilename}.json`;
latestVersionInfo = item;
const updaterJsonFilePath = path.join(homeDir, cfg.output.directory, jsonName);
Utils.writeJsonSync(updaterJsonFilePath, latestVersionInfo);
writeJsonSync(updaterJsonFilePath, latestVersionInfo);
// 删除缓存文件,防止生成的 zip 是旧版本
if (cfg.cleanCache) {
Utils.rm(path.join(homeDir, cfg.output.directory, 'mac'));
Utils.rm(path.join(homeDir, cfg.output.directory, 'mac-arm64'));
Utils.rm(path.join(homeDir, cfg.output.directory, 'win-unpacked'));
Utils.rm(path.join(homeDir, cfg.output.directory, 'win-ia32-unpacked'));
Utils.rm(path.join(homeDir, cfg.output.directory, 'linux-unpacked'));
rm(path.join(homeDir, cfg.output.directory, 'mac'));
rm(path.join(homeDir, cfg.output.directory, 'mac-arm64'));
rm(path.join(homeDir, cfg.output.directory, 'win-unpacked'));
rm(path.join(homeDir, cfg.output.directory, 'win-ia32-unpacked'));
rm(path.join(homeDir, cfg.output.directory, 'linux-unpacked'));
}
},
/**
* 将废弃
*/
generateFileOld(cfg, asarFile) {
var latestVersionInfo = {}
const homeDir = process.cwd();
console.log(chalk.blue('[ee-bin] [updater] ') + chalk.green('config:'), cfg);
let asarFilePath = "";
if (asarFile) {
asarFilePath = path.normalize(path.join(homeDir, asarFile));
} else if (Array.isArray(cfg.asarFile)) {
// 检查文件列表,如果存在就跳出
for (const filep of cfg.asarFile) {
asarFilePath = path.normalize(path.join(homeDir, filep));
if (fs.existsSync(asarFilePath)) {
break;
}
}
} else {
asarFilePath = path.normalize(path.join(homeDir, cfg.asarFile));
}
if (!fs.existsSync(asarFilePath)) {
console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${asarFilePath} does not exist`));
return;
}
const packageJson = Utils.getPackage();
const version = packageJson.version;
const platformForFilename = Utils.getPlatform("-");
const platformForKey = Utils.getPlatform("_");
// 生成 zip
let zipName = "";
if (cfg.output.noPlatform === true) {
zipName = path.basename(cfg.output.zip, '.zip') + `-${version}.zip`;
} else {
zipName = path.basename(cfg.output.zip, '.zip') + `-${platformForFilename}-${version}.zip`;
}
const asarZipPath = path.join(homeDir, cfg.output.directory, zipName);
if (fs.existsSync(asarZipPath) && cfg.cleanCache) {
Utils.rm(asarZipPath);
}
const zip = new admZip();
zip.addLocalFile(asarFilePath);
zip.writeZip(asarZipPath, (err) => {
if (err) {
console.log(chalk.blue('[ee-bin] [updater] create zip ') + chalk.red(`Error: ${err}`));
}
});
const sha1 = this.generateSha1(asarFilePath);
const date = this._getFormattedDate();
const fileStat = fs.statSync(asarFilePath);
const item = {
version: version,
file: zipName,
size: fileStat.size,
sha1: sha1,
releaseDate: date,
};
let jsonName = "";
if (cfg.output.noPlatform === true) {
jsonName = cfg.output.file;
latestVersionInfo = item;
} else {
// 生成与系统有关的文件
jsonName = path.basename(cfg.output.file, '.json') + `-${platformForFilename}.json`;
if (platformForKey !== "") {
latestVersionInfo[platformForKey] = item;
} else {
console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${platformForFilename} is not supported`));
}
}
const updaterJsonFilePath = path.join(homeDir, cfg.output.directory, jsonName);
Utils.writeJsonSync(updaterJsonFilePath, latestVersionInfo);
// 删除缓存文件,防止生成的 zip 是旧版本
if (cfg.cleanCache) {
Utils.rm(path.join(homeDir, cfg.output.directory, 'mac'));
Utils.rm(path.join(homeDir, cfg.output.directory, 'mac-arm64'));
Utils.rm(path.join(homeDir, cfg.output.directory, 'win-unpacked'));
Utils.rm(path.join(homeDir, cfg.output.directory, 'linux-unpacked'));
}
},
}
generateSha1(filepath = "") {
let sha1 = '';
@@ -239,7 +150,7 @@ module.exports = {
console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${error}`));
}
return sha1;
},
}
_getFormattedDate() {
const date = new Date(); // 获取当前日期
@@ -248,5 +159,10 @@ module.exports = {
const day = date.getDate().toString().padStart(2, '0'); // 获取日
return `${year}-${month}-${day}`;
}
}
}
module.exports = {
IncrUpdater,
incrUpdater: new IncrUpdater()
}

View File

@@ -4,82 +4,66 @@ const path = require('path');
const fs = require('fs');
const fsPro = require('fs-extra');
const chalk = require('chalk');
const Utils = require('../lib/utils');
const { loadConfig, rm } = require('../lib/utils');
/**
* 移动资源
*/
const homeDir = process.cwd();
module.exports = {
// 移动资源
function move(options = {}) {
console.log('[ee-bin] [move] Start moving resources');
const { config, flag } = options;
const binCfg = loadConfig(config);
const moveConfig = binCfg.move;
/**
* 执行
*/
run(options = {}) {
console.log('[ee-bin] [move] Start moving resources');
const homeDir = process.cwd();
const { config, flag } = options;
const binCfg = Utils.loadConfig(config);
let flags;
const flagString = flag.trim();
if (flagString.indexOf(',') !== -1) {
flags = flagString.split(',');
} else {
flags = [flagString];
let flags;
const flagString = flag.trim();
if (flagString.indexOf(',') !== -1) {
flags = flagString.split(',');
} else {
flags = [flagString];
}
for (let i = 0; i < flags.length; i++) {
let f = flags[i];
let cfg = moveConfig[f];
if (!cfg) {
console.log(chalk.blue('[ee-bin] [move] ') + chalk.red(`Error: ${f} config does not exist` ));
return;
}
for (let i = 0; i < flags.length; i++) {
let f = flags[i];
let cfg = binCfg.move[f];
const { src, dest, dist, target } = cfg;
const source = dist ? dist : src;
const destination = target ? target : dest;
if (!cfg) {
console.log(chalk.blue('[ee-bin] [move] ') + chalk.red(`Error: ${f} config does not exist` ));
return;
}
console.log(chalk.blue('[ee-bin] [move] ') + chalk.green(`Move flag: ${f}`));
console.log(chalk.blue('[ee-bin] [move] ') + chalk.green('config:'), cfg);
console.log(chalk.blue('[ee-bin] [move] ') + chalk.green(`Move flag: ${f}`));
console.log(chalk.blue('[ee-bin] [move] ') + chalk.green('config:'), cfg);
const distResource = path.join(homeDir, cfg.dist);
if (!fs.existsSync(distResource)) {
const errorTips = chalk.bgRed('Error') + ` ${cfg.dist} resource does not exist !`;
console.error(errorTips);
return
}
// clear the historical resource and copy it to the ee resource directory
const targetResource = path.join(homeDir, cfg.target);
if (fs.statSync(distResource).isDirectory() && !fs.existsSync(targetResource)) {
fs.mkdirSync(targetResource, {recursive: true, mode: 0o777});
} else {
this._rm(targetResource);
console.log('[ee-bin] [move] Clear history resources:', targetResource);
}
fsPro.copySync(distResource, targetResource);
// [todo] go project, special treatment of package.json, reserved only necessary
console.log(`[ee-bin] [move] Copy ${distResource} to ${targetResource}`);
}
console.log('[ee-bin] [move] End');
},
/**
* Delete a file or folder
*/
_rm(name) {
// check
if (!fs.existsSync(name)) {
const srcResource = path.join(homeDir, source);
if (!fs.existsSync(srcResource)) {
const errorTips = chalk.bgRed('Error') + ` ${source} resource does not exist !`;
console.error(errorTips);
return
}
const nodeVersion = (process.versions && process.versions.node) || null;
if (nodeVersion && Utils.compareVersion(nodeVersion, '14.14.0') == 1) {
fs.rmSync(name, {recursive: true});
// clear the historical resource and copy it to the ee resource directory
const destResource = path.join(homeDir, destination);
if (fs.statSync(srcResource).isDirectory() && !fs.existsSync(destResource)) {
fs.mkdirSync(destResource, {recursive: true, mode: 0o777});
} else {
fs.rmdirSync(name, {recursive: true});
rm(destResource);
console.log('[ee-bin] [move] Clear history resources:', destResource);
}
},
fsPro.copySync(srcResource, destResource);
// [todo] go project, special treatment of package.json, reserved only necessary
console.log(`[ee-bin] [move] Copy ${srcResource} to ${destResource}`);
}
console.log('[ee-bin] [move] End');
}
module.exports = {
move
}

View File

@@ -1,22 +1,24 @@
'use strict';
const path = require('path');
const Utils = require('../lib/utils');
const { loadConfig } = require('../lib/utils');
const is = require('is-type-of');
const chalk = require('chalk');
const crossSpawn = require('cross-spawn');
module.exports = {
class ServeProcess {
execProcess: {},
constructor() {
this.execProcess = {};
}
/**
* 启动前端、主进程服务
*/
dev(options = {}) {
const { config, serve } = options;
const binCfg = loadConfig(config);
const binCmd = 'dev';
const binCfg = Utils.loadConfig(config);
const binCmdConfig = binCfg[binCmd];
let command = serve;
@@ -30,15 +32,15 @@ module.exports = {
command,
}
this.multiExec(opt);
},
}
/**
* 启动主进程服务
*/
start(options = {}) {
const { config } = options;
const binCfg = loadConfig(config);
const binCmd = 'start';
const binCfg = Utils.loadConfig(config);
const binCmdConfig = {
start: binCfg[binCmd]
};
@@ -49,25 +51,23 @@ module.exports = {
command: binCmd,
}
this.multiExec(opt);
},
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
}
/**
* 构建
*/
build(options = {}) {
const { config, cmds } = options;
const binCfg = loadConfig(config);
const binCmd = 'build';
const binCfg = Utils.loadConfig(config);
const binCmdConfig = binCfg[binCmd];
if (!cmds || cmds == "") {
// [todo]
let tip = chalk.bgYellow('Warning') + ' Please modify the ' + chalk.blue('build') + ' config, See: ';
tip += chalk.underline('https://www.kaka996.com/pages/c492f8/');
const tip = chalk.bgYellow('Warning') + ' Please modify the ' + chalk.blue('build') + ' property in the bin file';
console.log(tip);
return
}
@@ -78,34 +78,24 @@ module.exports = {
command: cmds,
}
this.multiExec(opt);
},
}
/**
* 执行自定义命令
*/
exec(options = {}) {
let { config, command, cmds } = options;
const { config, cmds } = options;
const binCfg = loadConfig(config);
const binCmd = 'exec';
const binCfg = Utils.loadConfig(config);
const binCmdConfig = binCfg[binCmd];
// if (typeof command !== "string" || !cmds) {
// console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.red(`Error: Please specify parameters for --cmds` ));
// return
// }
// 兼容
if (typeof command === "string") {
cmds = command;
}
const opt = {
binCmd,
binCmdConfig,
command: cmds,
}
this.multiExec(opt);
},
}
/**
* 支持多个命令
@@ -124,7 +114,7 @@ module.exports = {
for (let i = 0; i < cmds.length; i++) {
let cmd = cmds[i];
let cfg = binCmdConfig[cmd];
const cfg = binCmdConfig[cmd];
if (!cfg) {
console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.red(`Error: [${binCmd} ${cmd}] config does not exist` ));
@@ -139,9 +129,9 @@ module.exports = {
console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + "Run " + chalk.green(`[${binCmd} ${cmd}]` + " command"));
console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.green('config:'), JSON.stringify(cfg));
let execDir = path.join(process.cwd(), cfg.directory);
let execArgs = is.string(cfg.args) ? [cfg.args] : cfg.args;
let stdio = cfg.stdio ? cfg.stdio: 'inherit';
const execDir = path.join(process.cwd(), cfg.directory);
const execArgs = is.string(cfg.args) ? [cfg.args] : cfg.args;
const stdio = cfg.stdio ? cfg.stdio: 'inherit';
const handler = cfg.sync ? crossSpawn.sync : crossSpawn;
@@ -158,10 +148,14 @@ module.exports = {
console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + chalk.green('Press "CTRL+C" to exit'));
return
}
console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + 'The ' + chalk.green(`[${binCmd} ${cmd}]`) + ' command is executed and exits');
console.log(chalk.blue(`[ee-bin] [${binCmd}] `) + 'The ' + chalk.green(`[${binCmd} ${cmd}]`) + ' command has been executed and exited');
});
}
}
},
}
}
module.exports = {
ServeProcess,
serveProcess: new ServeProcess()
}