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 83 84 85 86 87 88 89 90
| const fs = require('fs'); const nodeCmd = require('node-cmd');
let arguments = process.argv.slice(2); let targetBranch = arguments[0] || 11; let shouldBuild = arguments[1] == undefined ? true : arguments[1] == 'true'; let menusStation = arguments[2] == undefined ? 'changzhou' : arguments[2]; let filePath = arguments[3] || './pubspec.yaml'; let buildFilePath = arguments[4] || './android/app/build.gradle'; let menusConfigPath = arguments[5] || './lib/configuration/menus.dart';
console.table({ arguments, targetBranch, shouldBuild, filePath, buildFilePath });
fs.readFile(buildFilePath, 'utf8', function (err, data) { if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
let result = data.replace(/minSdkVersion\s\d{2}/g, `minSdkVersion ${targetBranch == 11 ? 26 : 21}`);
console.log('正在修改安卓 build 配置文件......');
fs.writeFile(buildFilePath, result, 'utf8', function (err) { if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
setMenus(); }); });
function setMenus() { fs.readFile(menusConfigPath, 'utf8', function (err, data) { if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
let result = data.replace( /static\sList\smenuList\s=\sX_MENU_\w+\;/g, `static List menuList = X_MENU_${menusStation};` );
console.log(`正在修改菜单 build 配置文件 [X_MENU_${menusStation}]......`);
fs.writeFile(menusConfigPath, result, 'utf8', function (err) { if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
buildStart(); }); }); }
function buildStart() { fs.readFile(filePath, 'utf8', function (err, data) { if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
let result = data.replace(/_android_\d{1,2}_scan/g, `_android_${targetBranch}_scan`);
console.log('正在修改 Flutter 配置文件......');
fs.writeFile(filePath, result, 'utf8', function (err) { if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
getFlutterPackages(); }); }); }
function getFlutterPackages() { console.log('执行下载命令 flutter pub get');
nodeCmd.run('flutter pub get', (err, data, stderr) => { if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
shouldBuild && buildFlutterApk(); }); }
function buildFlutterApk() { console.log('打包中... flutter build apk --target-platform android-arm64');
nodeCmd.run('flutter build apk --target-platform android-arm64', (err, data, stderr) => { if (err) return console.log(`%c出错啦!${data}`, 'color:red;');
console.log(`%c${data} %c成功,请检查 apk 文件!`, 'color:green;', 'color:chocolate;'); }); }
|