forked from opp100/bmw-scriptable-widgets
/
app.js
174 lines (150 loc) · 5.37 KB
/
app.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
require('dotenv').config();
const fs = require('fs');
const os = require('os');
const path = require('path');
const express = require('express');
const child_process = require('child_process');
const multer = require('multer');
const bodyParser = require('body-parser');
const HTTP_PORT = process.env.DEV_SERVER_PORT || 5566;
const WORK_DIR = path.dirname(__filename);
const SCRIPTS_DIR = path.join(WORK_DIR, 'Scripts');
const PUBLISH_DIR = path.join(WORK_DIR, 'Publish');
const app = express();
const upload = multer({
dest: os.tmpdir()
});
app.use(upload.any());
app.use(
bodyParser.urlencoded({
extended: false
})
);
app.use(bodyParser.json());
const _ip = process.env.DEV_SERVER || getIPAdress();
app.get('/', (req, res) => {
let html = fs.readFileSync(path.join(WORK_DIR, 'guide.html')).toString();
let js = fs.readFileSync(path.join(WORK_DIR, 'install-runtime.js')).toString();
js = js.replace('*|IP_ADDRESS|*', `${_ip}:${HTTP_PORT}`);
// load all files from scripts table
let files = fs.readdirSync(SCRIPTS_DIR);
let filesStr = '';
for (const file of files) {
filesStr += `'${file}',`;
}
js = js.replace('*|FILES|*', filesStr);
html = html.replace('@@code@@', js);
res.send(html);
});
app.get('/Scripts/:fileName', (req, res) => {
try {
let js = fs.readFileSync(path.join(SCRIPTS_DIR, req.params['fileName'])).toString();
res.send(js);
} catch (e) {
res.send('//访问文件错误');
}
});
app.get('/Publish/:fileName*', (req, res) => {
try {
let filePath = req.params['fileName'];
for (const key in req.params) {
if (Object.hasOwnProperty.call(req.params, key) && key != 'fileName') {
filePath += req.params[key];
}
}
console.warn(filePath);
res.sendFile(path.join(PUBLISH_DIR, filePath));
} catch (e) {
res.send('//访问文件错误');
}
});
app.get('/ping', (req, res) => {
console.log('[-] ping..');
setTimeout(() => {
res.send('pong').end();
}, 1000);
});
let FILE_DATE = null;
app.get('/sync', (req, res) => {
// console.log('[-] 等待同步到手机..')
const {name} = req.query;
const WIDGET_FILE = path.join(SCRIPTS_DIR, name + '.js');
if (!fs.existsSync(WIDGET_FILE)) return res.send('nofile').end();
setTimeout(() => {
// 判断文件时间
const _time = fs.statSync(WIDGET_FILE).mtimeMs;
if (_time === FILE_DATE) {
res.send('no').end();
return;
// return console.log("[!] 文件没有更改,不同步")
}
// 同步
res.sendFile(WIDGET_FILE);
console.log('[+] 同步到手机完毕');
FILE_DATE = _time;
}, 1000);
});
app.post('/sync', (req, res) => {
if (req.files.length !== 1) return res.send('no');
console.log('[+] Scriptalbe App 已连接');
const _file = req.files[0];
const FILE_NAME = _file['originalname'] + '.js';
const WIDGET_FILE = path.join(SCRIPTS_DIR, FILE_NAME);
fs.renameSync(_file['path'], WIDGET_FILE);
res.send('ok');
console.log(`[*] 小组件源码(${_file['originalname']})已同步,请打开编辑`);
FILE_DATE = fs.statSync(WIDGET_FILE).mtimeMs;
// 尝试打开
let cmd = `code "${WIDGET_FILE}"`;
if (os.platform() === 'win32') {
cmd = `cmd.exe /c ${cmd}`;
} else if (os.platform() === 'linux') {
let shell = process.env['SHELL'];
cmd = `${shell} -c ${cmd}`;
} else {
cmd = `"/Applications/Visual Studio Code.app/Contents/MacOS/Electron" "${WIDGET_FILE}"`;
}
child_process.execSync(cmd);
});
// 远程 console,调试中把调试输出内容传送到服务端控制台输出
app.post('/console', (req, res) => {
const {t, data} = req.body;
const _time = new Date().toLocaleString().split(' ')[1];
switch (t) {
case 'warn':
console.warn(`[console.warn / ${_time}]`, typeof data === 'string' ? data : '');
if (typeof data === 'object') console.warn(data);
break;
case 'error':
console.error(`[console.error / ${_time}]`, typeof data === 'string' ? data : '');
if (typeof data === 'object') console.error(data);
break;
default:
console.log(`[console.log / ${_time}]`, typeof data === 'string' ? data : '');
if (typeof data === 'object') console.log(data);
}
res.send('ok');
});
// 获取当前电脑IP
function getIPAdress() {
var interfaces = os.networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address;
}
}
}
}
const _host = `http://${_ip}:${HTTP_PORT}`;
console.log('[*] 「小件件」开发服务运行中');
console.log(`[-] 地址:${_host}`);
console.log(`[-] 如果你的手机还没有配置开发环境,请手机 Safari 访问上述地址,查看引导`);
console.log(
'[+] 如果你的手机已经安装好环境和小组件模板,请在 Scriptable 里点击小组件模板->远程开发,服务器地址输入:',
_ip
);
console.log('[*] 更多帮助:https://github.com/im3x/scriptables');
app.listen(HTTP_PORT);