From 804b0de4c5e72ebb27e65672d56123b505756391 Mon Sep 17 00:00:00 2001 From: Youke XIANG Date: Sat, 30 Oct 2021 10:32:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E6=9C=AC=E5=9C=B0=E5=8C=96=E6=A8=A1=E6=9D=BFjs?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 39 ++++++++++++++++++++++++++++++++------- install-runtime.js | 4 ++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 5b5c708..36260b1 100644 --- a/app.js +++ b/app.js @@ -20,12 +20,38 @@ app.use(bodyParser.urlencoded({ })) app.use(bodyParser.json()) -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() - html = html.replace("@@code@@", js) - res.send(html) -}) +const _ip = 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}`); + + // 从Scripts文件夹读取所有文件 + 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('/ping', (req, res) => { console.log('[-] ping..') @@ -117,7 +143,6 @@ function getIPAdress() { } } -const _ip = getIPAdress() const _host = `http://${_ip}:${HTTP_PORT}` console.log('[*] 「小件件」开发服务运行中') diff --git a/install-runtime.js b/install-runtime.js index 9808519..1662a43 100644 --- a/install-runtime.js +++ b/install-runtime.js @@ -1,6 +1,6 @@ const FILE_MGR = FileManager[module.filename.includes('Documents/iCloud~') ? 'iCloud' : 'local'](); -await Promise.all(['「小件件」开发环境.js', '「源码」小组件示例.js'].map(async js => { - const REQ = new Request(`https://gitee.com/im3x/Scriptables/raw/v2-dev/Scripts/${encodeURIComponent(js)}`); +await Promise.all([*|FILES|*].map(async js => { + const REQ = new Request(`http://*|IP_ADDRESS|*/Scripts/${encodeURIComponent(js)}`); const RES = await REQ.load(); FILE_MGR.write(FILE_MGR.joinPath(FILE_MGR.documentsDirectory(), js), RES); }));