Skip to content

Commit

Permalink
update: electron-builder conf
Browse files Browse the repository at this point in the history
  • Loading branch information
nojsja committed Nov 6, 2021
1 parent 44aa704 commit eb1d258
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 17 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ It also uses Electron to bring the most powerful cross-platform desktop support

> More architecture will be supported if necessary, just give me an issue.
- Ubuntu (linux x64/x86/arm64)
- MacOS (darwin x64)
- <del>Windows (windows x64)</del>
- Ubuntu
- Deb `x64/arm64`
- AppImage `x64/arm64`
- Snap `x64`
- MacOS
- Dmg `x64`
- <del>Windows</del>

## Development

Expand Down
4 changes: 0 additions & 4 deletions bin/darwin/x64/.gitignore

This file was deleted.

File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions bin/linux/x64/.gitignore

This file was deleted.

87 changes: 87 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const path = require('path');
const fs = require('fs');
const { copyDirSync, exec, execRealtime, console_log, removeDirSync } = require('./build.utils');
const conf = require('./electron-builder.json');

/*
* 函数调用list
* @param web-dist 使用webpack打包前端web代码
* @param build-win 执行win平台打包
* @param build-mac 执行linux平台打包
* @param build-linux 执行mac平台打包
* @param build-all 执行所有平台打包
* @param --help | -h 查看帮助信息
*/
const func = {
/* build for linux platform */
'build:linux': async (env) => {
for (let target of conf.linux.target) {
for (let arch of target.arch) {
fs.writeFileSync('./electron-builder.json', JSON.stringify({
...conf,
...{
files: conf.files.concat(`bin/linux/${arch === 'ia32' ? 'x86' : arch}/*`)
}
}, null, 2));
await execRealtime(`electron-builder --linux ${target.target} --${arch}`, { cwd: '.' });
await execRealtime('git checkout -- electron-builder.json', { cwd: '.' })
}
}
},
/* build for mac platform */
'build:mac': async (env) => {
await execRealtime(`electron-builder --mac`, { cwd: '.' });
},
'clean-build': async (env) => {
if (fs.existsSync('./view/dist')) {
removeDirSync('./view/dist');
}
await execRealtime('git checkout -- dist', { cwd: '.' });
console_log(`\nclean finishied!`);
},
/* build command usage */
'--help': () => {
console_log('\
\n\
description: build command for electron-re.\n\
command: node build.js [action] [config]\n\
|\n\
|\n\
|______ param: [--help | -h ] => show usage info.\n\
|______ param: [build:libs ] => build command for service component.\n\
|______ param: [build:ui ] => build command for ui component.\n\
|______ param: [build ] => build command for ui and service\n\
|______ param: [clean-build ] => clean build directory after build\n\
|\n\
|______ example1: node build.js build:libs\n\
|______ example2: node build.js build:ui\n\
|______ example7: node build.js --help\n\
|______ example8: node build.js clean-build\n\
\n\
')
},
'-h': () => {
func['--help']();
}
};

/* Main */
function Main() {
const params = process.argv.splice(2);
const indexArray = [];
let args;

params.forEach((key, i) => {
if (func[key] && (typeof func[key] === 'function')) indexArray.push(i);
});

indexArray.forEach((index, i) => {
args = indexArray.slice(index + 1, indexArray[i + 1]).map(i => params[i]);
if (args.length)
func[params[index]](...args);
else
func[params[index]]('');
});
}

Main();
219 changes: 219 additions & 0 deletions build.utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
const fs = require('fs');
const path = require('path');
const child = require('child_process');

const compressing = require('compressing');

/**
* removeDirSync [remove dir sync vertion]
* @author nojsja
* @param {[String]} _path [path to a directory]
*/
exports.removeDirSync = function(_path) {
if( fs.existsSync(_path) ) {
fs.readdirSync(_path).forEach(function(file,index){
const curPath = _path + "/" + file;
if(fs.statSync(curPath).isDirectory()) { // recurse
exports.removeDirSync(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(_path);
}
};

/*
* 复制目录、子目录,及其中的文件
* @param src {String} 要复制的目录
* @param dist {String} 复制到目标目录
*/
exports.copyDirSync = (src, dist) => {
const _copy = (src, dist) => {
const paths = fs.readdirSync(src)
paths.forEach((p) => {
let _src = src + '/' +p;
let _dist = dist + '/' +p;
let stat = fs.statSync(_src)
if(stat.isFile()) {// 判断是文件还是目录
fs.writeFileSync(_dist, fs.readFileSync(_src));
} else if(stat.isDirectory()) {
exports.copyDirSync(_src, _dist)// 当是目录是,递归复制
}
})
}

const b = fs.existsSync(dist)
if(!b){
fs.mkdirSync(dist);//创建目录
}
_copy(src, dist);
}

/**
* [exec 执行一个命令,阻塞输出信息到控制台]
* @param { [String] } command [命令]
* @param { [Array | String] } params [参数数组]
* @param { [Object] } options [exec可定制的参数]
* @return { Promise } [返回Promise对象]
*/
exports.exec = (_command, _params=[], _options={}) => {
const params = Array.isArray(_params) ? _params.join(' ') : '';
const options = (String(_params) === '[object Object]') ? _params : (_options);
const command = `${_command} ${params}`;

console.log(params, options, command);

return new Promise((resolve, reject) => {
child.exec(command, options, (_err, _stdout, _stderr) => {
if (_err) {
exports.console_log(_err, 'red');
resolve({code: 1, result: _err});
} else if (_stderr && _stderr.toString()) {
exports.console_log(_stderr, 'red');
resolve({code: 1, result: _stderr});
} else {
console.log(_stdout);
resolve({code: 0, result: _stdout});
}
});
});
}

/**
* [execRealtime 执行一个命令,实时输出信息到控制台]
* @param { [String] } command [命令]
* @param { [Array | String] } params [参数数组]
* @param { [Object] } options [exec可定制的参数]
* @return { Promise } [返回Promise对象]
*/
exports.execRealtime = (_command, _params=[], _options={}) => {
const params = Array.isArray(_params) ? _params.join(' ') : '';
const options = (String(_params) === '[object Object]') ? _params : (_options);
const command = `${_command} ${params}`;
let data = '', error = '';

console.log(params, options, command);

return new Promise((resolve, reject) => {
const result = child.exec(command, options);

result.stdout.on('data', (data) => {
exports.console_log(data, 'white');
data += `${data}`;
});

result.stderr.on('data', (data) => {
exports.console_log(data, 'red');
error += `${data}`;
});

result.on('close', (code) => {
resolve({code, result: data, error});
});
});
}

/**
* [console_log 格式化颜色console.log]
* @param { [String] } info [输出的字符串]
* @param { [String] } _color [字体颜色-black | red | green(default) | yellow | blue | purple | heavyGree | white]
* @param { [Object] } _bgcolor [背景颜色-black(default) | red | green | yellow | blue | purple | heavyGree | white]
*/
exports.console_log = (function() {
const colorMap = {
black:30,
red: 31,
green: 32,
yellow: 33,
blue: 34,
purple: 35,
heavyGree: 36,
white: 37,
}

return (info, _color='green', _bgcolor='black') => {
const color = colorMap[_color];
const bgcolor = colorMap[_bgcolor] + 10;
const colorFormat = color && bgcolor ? `${bgcolor};${color}m` : '\033[0m';
process.stdout.write('\033[' + `${colorFormat}${info}` + '\033[0m') ;
}
})();

/**
* [compress 压缩]
* @param { [String] } origin [源位置-dir/file]
* @param { [String] } target [目的位置-dir/file]
* @param { [Object] } options [配置项]
*/

/*
tar - tar
gzip - gz
tgz - tgz
zip - zip
*/
exports.compress = function(origin, target, options={}) {
const cmap = {
'.tar': 'tar',
'.gz': 'gzip',
'.gzip': 'gzip',
'.tgz': 'tgz',
'.zip': 'zip',
};
const ctype = cmap[path.extname(target)];
let ftype;

return new Promise((resolve) => {
if (!ctype) return resolve(`the filetype of ${origin} must be one of tar/gzip/tgz/zip`);
if (fs.existsSync(origin)) {
ftype = fs.statSync(origin).isFile() ? 'compressFile' : (fs.statSync(origin).isDirectory() ? 'compressDir' : null);
if (!ftype) return resolve(`the path - ${origin} is not a directory or file!`)
} else {
return resolve(`the path - ${origin} does not exist!`);
}
compressing[ctype][ftype](origin, target, options)
.then(() => {
resolve(null)
})
.catch((error) => {
resolve(error.toString());
})

});
}

/**
* [uncompress 解压缩]
* @param { [String] } origin [源位置-dir/file]
* @param { [String] } target [目的位置-dir/file]
* @param { [Object] } options [配置项]
*/
exports.uncompress = function(origin, target, options={}) {
const cmap = {
'.tar': 'tar',
'.gz': 'gzip',
'.gzip': 'gzip',
'.tgz': 'tgz',
'.zip': 'zip',
};
const ctype = cmap[path.extname(origin)];

return new Promise((resolve) => {
if (!ctype) return resolve(`the filetype of ${origin} must be one of tar/gzip/tgz/zip`);
if (fs.existsSync(origin)) {
if(fs.statSync(origin).isDirectory()) return resolve(`the path - ${origin} can not be a directory!`)
} else {
return resolve(`the path - ${origin} does not exist!`);
}

compressing[ctype]['uncompress'](origin, target, options)
.then(() => {
resolve(null)
})
.catch((error) => {
resolve(error.toString());
})

});
}
2 changes: 1 addition & 1 deletion electron-builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"extraFiles": [
{
"from": "bin/${os}/${arch}",
"to": "runtime/bin/${os}/${arch}",
"to": "bin/${os}/${arch}",
"filter": [
"!.gitignore"
]
Expand Down
3 changes: 2 additions & 1 deletion main/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ let ipcMainWindow: IpcMainWindowType;
autoUpdater.logger = logger;
const appDataPath = path.join(app.getPath('appData'), packageName);
const pathRuntime = (global as any).pathRuntime = path.join(appDataPath, 'runtime/');
const pathExecutable = isDev ? app.getAppPath() : path.dirname(app.getPath('exe'));

/* -------------- pre work -------------- */

Expand All @@ -36,7 +37,7 @@ checkEnvFiles(
{ _path: pathRuntime, isDir: true },
{ _path: path.join(pathRuntime, 'bin'), isDir: true,
exec: () => {
copyDir(path.join(app.getAppPath(), 'bin'), path.join(pathRuntime, 'bin'));
copyDir(path.join(pathExecutable, 'bin'), path.join(pathRuntime, 'bin'));
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions main/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { Config } from '../types/extention';

const archMap = new Map([
['aarch64', 'arm64'],
['x86', 'x86'],
['x86', 'ia32'],
['x64', 'x64'],
['ia32', 'x86'],
['ia32', 'ia32'],
['arm64', 'arm64']
]);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@types/react-router-dom": "5.1.3",
"@types/uuid": "7.0.2",
"babel-plugin-import": "1.13.0",
"compressing": "^1.5.1",
"concurrently": "5.1.0",
"customize-cra": "0.9.1",
"dotenv": "8.2.0",
Expand Down
Loading

0 comments on commit eb1d258

Please sign in to comment.