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); }));