-
Notifications
You must be signed in to change notification settings - Fork 0
/
dshd.js
56 lines (50 loc) · 1.56 KB
/
dshd.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const { exec } = require('child_process')
const Dat = require('util').promisify(require('dat-node'))
const mkdirp = require('util').promisify(require('mkdirp'))
const readFile = require('util').promisify(require('fs').readFile)
const writeFile = require('util').promisify(require('fs').writeFile)
const DAT_INPUT = '.din'
const DAT_OUTPUT = '.dout'
const QUEUE_FILE = '.dshell'
;(async function () {
let key = process.argv[2]
if (!key) {
try {
key = await readFile(path.join(DAT_INPUT, '.key'))
} catch (e) {}
}
if (!key) {
console.log(`Must provide a remote key`)
process.exit(1)
}
await writeFile(path.join(DAT_INPUT, '.key'), key)
// make folders
await mkdirp(DAT_INPUT)
await mkdirp(DAT_OUTPUT)
// get input
const datIn = await Dat(DAT_INPUT, { key })
datIn.joinNetwork()
fs.watchFile(DAT_INPUT, async (current, previous) => {
// read command
let command
try {
command = await readFile(path.join(DAT_INPUT, QUEUE_FILE), 'utf8')
} catch (e) {}
command && exec(command, async (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`)
return
}
await writeFile(path.join(DAT_OUTPUT, '.stdout'), stdout)
await writeFile(path.join(DAT_OUTPUT, '.stderr'), stderr)
})
})
// save output
const datOut = await Dat(DAT_OUTPUT)
datOut.importFiles({ watch: true, ignoreHidden: false })
datOut.joinNetwork()
console.log('Dat link is: dat://' + datOut.key.toString('hex'))
}())