wiki-plugin-mech 0.1.16 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/mech.js +8 -1
- package/package.json +1 -1
- package/server/server.js +71 -8
package/client/mech.js
CHANGED
|
@@ -509,11 +509,18 @@
|
|
|
509
509
|
const url = `//${site}/plugin/mech/run/${slug}/${itemId}?${query}`
|
|
510
510
|
elem.innerHTML = command + ` ⇒ in progress`
|
|
511
511
|
const start = Date.now()
|
|
512
|
+
let result
|
|
512
513
|
try {
|
|
513
|
-
|
|
514
|
+
result = await fetch(url).then(res => res.ok ? res.json() : res.status)
|
|
514
515
|
} catch(err) {
|
|
515
516
|
return trouble(elem,`RUN failed with "${err.message}"`)
|
|
516
517
|
}
|
|
518
|
+
state.result = result
|
|
519
|
+
for(const arg of result.args.flat(9)){
|
|
520
|
+
const elem = document.getElementById(arg.key)
|
|
521
|
+
if('status' in arg) elem.innerHTML = arg.command + ` ⇒ ${arg.status}`
|
|
522
|
+
if('trouble' in arg) trouble(elem,arg.trouble)
|
|
523
|
+
}
|
|
517
524
|
const elapsed = ((Date.now() - start)/1000).toFixed(3)
|
|
518
525
|
elem.innerHTML = command + ` ⇒ ${elapsed} seconds`
|
|
519
526
|
}
|
package/package.json
CHANGED
package/server/server.js
CHANGED
|
@@ -11,26 +11,89 @@
|
|
|
11
11
|
var app = params.app,
|
|
12
12
|
argv = params.argv
|
|
13
13
|
|
|
14
|
-
return app.get('/plugin/mech/run/:slug([a-z-]+)/:itemId', (req, res) => {
|
|
14
|
+
return app.get('/plugin/mech/run/:slug([a-z-]+)/:itemId', async (req, res) => {
|
|
15
15
|
console.log(req.params)
|
|
16
|
-
const uptime = process.uptime()
|
|
17
16
|
try {
|
|
18
17
|
const slug = req.params.slug
|
|
19
18
|
const itemId = req.params.itemId
|
|
20
19
|
const args = JSON.parse(atob(req.query.args || 'W10='))
|
|
21
20
|
const path = `${argv.db}/${slug}`
|
|
22
|
-
fs.readFile(path,(err,data) => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
})
|
|
21
|
+
// fs.readFile(path,(err,data) => {
|
|
22
|
+
// const page = JSON.parse(data)
|
|
23
|
+
// const item = page.story.find(item => item.id == itemId) || null
|
|
24
|
+
// return res.json({err,item,args});
|
|
25
|
+
// })
|
|
26
|
+
const context = {path}
|
|
27
|
+
const state = {context}
|
|
28
|
+
run(args,state)
|
|
29
|
+
return res.json({args,state})
|
|
30
|
+
|
|
27
31
|
} catch(err) {
|
|
28
|
-
return res.json({err:err.message
|
|
32
|
+
return res.json({err:err.message})
|
|
29
33
|
}
|
|
30
34
|
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// I N T E R P R E T E R
|
|
39
|
+
|
|
40
|
+
function status(elem,message) {
|
|
41
|
+
elem.status = message
|
|
42
|
+
}
|
|
31
43
|
|
|
44
|
+
function trouble(elem,message) {
|
|
45
|
+
elem.trouble = message
|
|
32
46
|
}
|
|
33
47
|
|
|
48
|
+
async function run (nest,state={},mock) {
|
|
49
|
+
// const scope = nest.slice()
|
|
50
|
+
// while (scope.length) {
|
|
51
|
+
for (let here = 0; here < nest.length; here++) {
|
|
52
|
+
// const code = scope.shift()
|
|
53
|
+
const code = nest[here]
|
|
54
|
+
if ('command' in code) {
|
|
55
|
+
const command = code.command
|
|
56
|
+
const elem = code
|
|
57
|
+
const [op, ...args] = code.command.split(/ +/)
|
|
58
|
+
const next = nest[here+1]
|
|
59
|
+
const body = next && ('command' in next) ? null : nest[++here]
|
|
60
|
+
const stuff = {command,op,args,body,elem,state}
|
|
61
|
+
if(state.debug) console.log(stuff)
|
|
62
|
+
if (blocks[op])
|
|
63
|
+
blocks[op].emit.apply(null,[stuff])
|
|
64
|
+
else
|
|
65
|
+
if (op.match(/^[A-Z]+$/))
|
|
66
|
+
trouble(elem,`${op} doesn't name a block we know.`)
|
|
67
|
+
else if (code.command.match(/\S/))
|
|
68
|
+
trouble(elem, `Expected line to begin with all-caps keyword.`)
|
|
69
|
+
} else if(typeof code == 'array') {
|
|
70
|
+
console.warn(`this can't happen.`)
|
|
71
|
+
run(code,state) // when does this even happen?
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// B L O C K S
|
|
77
|
+
|
|
78
|
+
function hello_emit ({elem,args,state}) {
|
|
79
|
+
const world = args[0] == 'world' ? ' 🌎' : ' 😀'
|
|
80
|
+
status(elem,world)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function uptime_emit ({elem,args,state}) {
|
|
84
|
+
const uptime = process.uptime()
|
|
85
|
+
status(elem,uptime)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
// C A T A L O G
|
|
90
|
+
|
|
91
|
+
const blocks = {
|
|
92
|
+
HELLO: {emit:hello_emit},
|
|
93
|
+
UPTIME: {emit:uptime_emit}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
34
97
|
module.exports = {startServer}
|
|
35
98
|
|
|
36
99
|
}).call(this)
|