Electron多环境打包

正文

客户端打包的时候如果有不同的logo,名称等信息,每次打包都需要替换一遍 ,所以这里就写了个脚本来批量替换文件和文字。

替换文本和复制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const path = require("path");
const fs = require("fs");

const suff_arr = [".html", ".js", ".txt", ".vue", ".json"];

function changefile(mpath, oldword, newword) {
let suff = mpath.substring(mpath.lastIndexOf("."));
if (suff_arr.indexOf(suff) !== -1) {
console.info("mpath", mpath);
try {
let files = fs.readFileSync(mpath, 'utf8')
const result = files.replace(new RegExp(oldword, "g"), newword);
fs.writeFileSync(mpath, result, 'utf8');
} catch (e) {
console.info(e);
}
}
}

function editKeyWord(mpath, oldword, newword) {
var stat = fs.lstatSync(mpath);
if (stat.isFile()) {
changefile(mpath, oldword, newword);
} else if (stat.isDirectory()) {
try {
let data = fs.readdirSync(mpath, 'utf8');
if (data) {
data.forEach(function (item, index) {
let temppath = path.join(mpath, item);
editKeyWord(temppath, oldword, newword);
});
}
} catch (e) {
console.info(e);
}
}
}


/*
* 复制目录、子目录,及其中的文件
* @param src {String} 要复制的目录
* @param dist {String} 复制到目标目录
*/
function copyDir(src, dist, err_callback) {
try {
fs.accessSync(dist);
_copy(src, dist, err_callback);
} catch (e) {
fs.mkdirSync(dist);
}

function _copy(src, dist, err_callback) {
try {
let paths = fs.readdirSync(src);
paths.forEach(function (mpath) {
let _src = path.join(src, mpath);
let _dist = path.join(dist, mpath);
try {
let stat = fs.statSync(_src);
// 判断是文件还是目录
if (stat.isFile()) {
if (fs.existsSync(_dist)) {
fs.unlinkSync(_dist);
}
fs.writeFileSync(_dist, fs.readFileSync(_src));
} else if (stat.isDirectory()) {
// 当是目录是,递归复制
copyDir(_src, _dist, err_callback)
}
} catch (e) {
err_callback(err);
}
})
} catch (err) {
err_callback(err)
}
}
}

exports.editKeyWord = editKeyWord;
exports.copyDir = copyDir;

注意

所有的文件操作,这里都用的同步。异步会导致,如果有多个同时进行的替换,就会文件占用导致替换失败。

保留上一次的文字_oldname.js

1
exports.oldname = "火星直播";

替换文字和文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const {editKeyWord, copyDir} = require("./_common");
const path = require("path");

const {oldname} = require("./_oldname");
let newname = "星火直播测试";

const filePath = path.resolve(__dirname, '../src/');
editKeyWord(filePath, oldname, newname);

let html_arr = [
"_package/_oldname.js",
"main.js",
"package.json",
"v_blackboard.html",
"v_class_center.html",
"v_classtip.html",
"v_device_test.html",
"v_dianming.html",
"v_exit_dialog.html",
"v_home.html",
"v_huamingce.html",
"v_huamingce_mini.html",
"v_jiangping.html",
"v_login.html",
"v_player_win.html",
"v_qiangda.html",
"v_space.html",
"v_stu_head.html",
"v_teacher_head.html",
"v_test.html",
"v_tip.html",
"v_tiwen.html",
"v_tongji.html",
"v_tweb.html"
];

for (const str of html_arr) {
const temppath = path.resolve(__dirname + '/../' + str);
editKeyWord(temppath, oldname, newname);
}

const themepath = path.resolve(__dirname, '../src/config/Config.js');
editKeyWord(themepath, 'themename = "theme_red"', 'themename = "theme_blue"');

const path_origin = path.resolve(__dirname, './xh/image');
const path_target = path.resolve(__dirname, '../image');
copyDir(path_origin, path_target, function (err) {
if (err) {
console.log(err);
}
})

const ico_origin = path.resolve(__dirname, './xh/ico/');
const ico_target = path.resolve(__dirname, '../');
copyDir(ico_origin, ico_target, function (err) {
if (err) {
console.log(err);
}
})

方便执行我们写了批处理文件

test.bat

1
2
node test.js
pause