feat: 打包为可执行文件

This commit is contained in:
shanmite 2021-06-06 13:57:58 +08:00
parent aefda1af3d
commit 820ac89dba
9 changed files with 1070 additions and 32 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
node_modules/
dyids/
dist/
lib/dyid*.txt
dyid.zip
env.js

View File

@ -11,6 +11,7 @@
- [操作步骤](#操作步骤)
- [获取COOKIE](#获取cookie)
- [本地运行](#本地运行)
- [可执行文件](#可执行文件)
- [Windows](#windows)
- [Linux](#linux)
- [Docker](#docker)
@ -71,7 +72,11 @@ Chrome浏览器:
↓↓
### 本地运行
具体操作详见[env.example.js](env.example.js)文件内注释
具体操作详见[env.example.js](env.example.js)文件内注释
#### 可执行文件
[可执行文件下载](https://github.com/shanmite/LotteryAutoScript/releases)
#### Windows
step1: 下载代码到本地

View File

@ -6,6 +6,12 @@ const { HttpRequest } = require("./HttpRequest");
* 基础工具
*/
const Base = {
/**环境变量设置文件 */
env_file: path.join(process.cwd(), "env.js"),
/**配置文件 */
config_file: path.join(process.cwd(), "my_config.json"),
/**dyid存储文件 */
dyids_dir: path.join(process.cwd(), "dyids"),
/**
* 安全的将JSON字符串转为对象
* 超出精度的数转为字符串
@ -159,7 +165,9 @@ const Base = {
},
success: res => {
try {
resolve(JSON.parse(res.body).config)
const body = JSON.parse(res.body);
if (body.node_msg) console.log(body.node_msg);
resolve(body.config)
} catch (error) {
resolve(JSON.parse('{}'))
}
@ -193,13 +201,13 @@ const Base = {
},
/**
* CreateFile
* @param {string} filepath 相对于lib的文件路径
* @param {string} filepath 相对于dyids的文件路径
* @param {string} [defaultValue] 写入默认值
* @param {string} flag
* @returns {Promise<void>}
*/
createFile(filepath, defaultValue, flag) {
const fpath = path.join('dyids/', filepath);
const fpath = path.join(this.dyids_dir, filepath);
const buffer = Buffer.from(defaultValue);
return new Promise((resolve, rejects) => {
fs.open(fpath, flag, (err, fd) => {
@ -223,7 +231,7 @@ const Base = {
* @returns {fs.ReadStream}
*/
readDyidFile(num) {
const fpath = num < 2 ? path.join('dyids/', 'dyid.txt') : path.join('dyids/', `dyid${num}.txt`);
const fpath = num < 2 ? path.join(this.dyids_dir, 'dyid.txt') : path.join(this.dyids_dir, `dyid${num}.txt`);
return fs.createReadStream(fpath, { encoding: 'utf8', highWaterMark: 19 * 1000 })
},
/**
@ -232,7 +240,7 @@ const Base = {
* @returns {fs.WriteStream}
*/
writeDyidFile(num) {
const fpath = num < 2 ? path.join('dyids/', 'dyid.txt') : path.join('dyids/', `dyid${num}.txt`);
const fpath = num < 2 ? path.join(this.dyids_dir, 'dyid.txt') : path.join(this.dyids_dir, `dyid${num}.txt`);
return fs.createWriteStream(fpath, { flags: 'a' })
}
};

View File

@ -1,4 +1,4 @@
const { tooltip } = require('./Base');
const { config_file, tooltip } = require('./Base');
const default_script = {
author: '@shanmite',
@ -22,7 +22,7 @@ const my_config = (() => {
let _my_config = {}
if (process.env.LOCALLAUNCH) {
try {
const { UIDs, TAGs } = require('../my_config.json');
const { UIDs, TAGs } = require(config_file);
if (UIDs) _my_config.UIDs = UIDs;
if (TAGs) _my_config.TAGs = TAGs;
} catch (e) {

View File

@ -1,4 +1,4 @@
const { tooltip } = require('./Base');
const { config_file, tooltip } = require('./Base');
/**
* 默认设置 具体含义参见README
@ -46,7 +46,7 @@ const my_config = (() => {
let _my_config = {}
if (process.env.LOCALLAUNCH) {
try {
_my_config = require('../my_config.json')
_my_config = require(config_file)
} catch (e) {
tooltip.log("[config]无自定义设置\n" + e);
}

31
main.js
View File

@ -1,10 +1,12 @@
const { tooltip, delay } = require("./lib/Base");
const { env_file, tooltip, delay } = require("./lib/Base");
let multiple_account = [];
if (!process.env.CI) {
const { initEnv, multiple_account_parm } = require("./env");
multiple_account = multiple_account_parm;
const { initEnv, multiple_account_parm } = require(env_file);
if (multiple_account_parm) {
multiple_account = multiple_account_parm;
}
initEnv()
}
@ -12,7 +14,7 @@ async function main() {
const { COOKIE, NUMBER, CLEAR, PAT, LOCALLAUNCH, ENABLE_MULTIPLE_ACCOUNT, MULTIPLE_ACCOUNT } = process.env;
if (LOCALLAUNCH || PAT) {
if (ENABLE_MULTIPLE_ACCOUNT) {
let muti_acco = multiple_account && multiple_account.length
let muti_acco = multiple_account.length
? multiple_account
: JSON.parse(MULTIPLE_ACCOUNT);
@ -36,7 +38,11 @@ async function main() {
tooltip.log('[LotteryAutoScript] 账号' + NUMBER);
if (await checkCookie(NUMBER)) {
switch (process.argv.slice(2)[0]) {
let argvs = process.argv;
if (/node/.test(argvs[0])) {
argvs = argvs.splice(1);
}
switch (argvs[1]) {
case 'start':
tooltip.log('开始参与抽奖');
await start();
@ -53,7 +59,7 @@ async function main() {
}
break;
default:
break;
console.log(`Usage: lottery-in-bili [OPTIONS]`)
}
}
} else {
@ -66,6 +72,19 @@ async function main() {
}
(async function () {
let metainfo = '';
metainfo += ` _ _ _ _____ _ _ \n`;
metainfo += ` | | | | | | / ____| (_) | | \n`;
metainfo += ` | | ___ | |_| |_ ___ _ __ _ _| (___ ___ _ __ _ _ __ | |_ \n`;
metainfo += ` | | / _ \\| __| __/ _ \\ '__| | | |\\___ \\ / __| '__| | '_ \\| __|\n`;
metainfo += ` | |___| (_) | |_| || __/ | | |_| |____) | (__| | | | |_) | |_ \n`;
metainfo += ` |______\\___/ \\__|\\__\\___|_| \\__, |_____/ \\___|_| |_| .__/ \\__|\n`;
metainfo += ` __/ | | | \n`;
metainfo += ` |___/ |_| \n`;
metainfo += ` \n`;
metainfo += ` by shanmite\n`;
console.log(metainfo);
await main();
await delay(5 * 1000);
process.exit(0)
})()

1005
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,22 @@
{
"name": "lottery-in-bilibili",
"version": "2.5.0",
"name": "lottery-in-bili",
"version": "3.0.0",
"description": "自动抽奖",
"main": "main.js",
"scripts": {
"start": "node main.js start",
"clear": "node main.js clear",
"check": "node main.js check"
"check": "node main.js check",
"pkg": "npx pkg ."
},
"bin": "main.js",
"pkg": {
"targets": [
"node14-linux-x64",
"node14-win-x64",
"node12-macos-x64"
],
"outputPath": "dist"
},
"repository": {
"type": "git",
@ -23,10 +33,11 @@
},
"homepage": "https://github.com/shanmite/AutoScript#readme",
"devDependencies": {
"eslint": "^7.17.0"
"eslint": "^7.17.0",
"pkg": "^5.2.1"
},
"dependencies": {
"nodemailer": "^6.5.0",
"unzipper": "^0.10.11"
}
}
}

View File

@ -11,17 +11,6 @@ CONFIG_FILE=my_config.json
# docker仓库
DOCKER_REPO=shanmite/lottery_auto_docker
echo " _ _ _ _____ _ _ ";
echo " | | | | | | / ____| (_) | | ";
echo " | | ___ | |_| |_ ___ _ __ _ _| (___ ___ _ __ _ _ __ | |_ ";
echo " | | / _ \| __| __/ _ \ '__| | | |\___ \ / __| '__| | '_ \| __|";
echo " | |___| (_) | |_| || __/ | | |_| |____) | (__| | | | |_) | |_ ";
echo " |______\___/ \__|\__\___|_| \__, |_____/ \___|_| |_| .__/ \__|";
echo " __/ | | | ";
echo " |___/ |_| ";
echo " ";
echo " by shanmite";
if [ ! -d "$SCRIPT_FOLDER" ]; then
echo "create $SCRIPT_FOLDER"
mkdir $SCRIPT_FOLDER