Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加自动导入本地js文件 #43

Open
wants to merge 1 commit into
base: v2-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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..')
Expand Down Expand Up @@ -117,7 +143,6 @@ function getIPAdress() {
}
}

const _ip = getIPAdress()
const _host = `http://${_ip}:${HTTP_PORT}`

console.log('[*] 「小件件」开发服务运行中')
Expand Down
4 changes: 2 additions & 2 deletions install-runtime.js
Original file line number Diff line number Diff line change
@@ -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);
}));
Expand Down