diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-06-01 03:30:39 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-06-01 03:30:39 +0200 |
| commit | 964ac7009e6db5a06233bdc07fa63778eebf2db7 (patch) | |
| tree | 960ceb019514f960a6597b9b282baf4d5cd77607 /app/relay | |
| parent | dd31a7b9a3af167808b04ffe2af3a66af8b17c33 (diff) | |
async commands!!
Diffstat (limited to 'app/relay')
| -rw-r--r-- | app/relay/remote.js | 20 | ||||
| -rw-r--r-- | app/relay/runner.js | 30 |
2 files changed, 48 insertions, 2 deletions
diff --git a/app/relay/remote.js b/app/relay/remote.js index e468c7d..7c70c26 100644 --- a/app/relay/remote.js +++ b/app/relay/remote.js @@ -74,25 +74,41 @@ remote.on('task', (data) => { remote.on('system', (data) => { console.log('system:', data.cmd) + // console.log(data) switch(data.cmd) { case 'run_system_command': runner.run_system_command(data.payload, (error, stdout, stderr) => { remote.emit('system_res', { - type: 'command_output', + type: 'run_system_command', cmd: data.payload, + uuid: data.uuid, error, stdout, stderr }) }) break + case 'list_directory': + runner.list_directory(data.payload, (files) => { + remote.emit('system_res', { + type: 'list_directory', + dir: data.payload, + uuid: data.uuid, + files, + }) + }) case 'get_status': remote.emit('system_res', { type: 'relay_status', rpc_connected: get_connected(), + uuid: data.uuid, runner: runner.status(), }) break default: - remote.emit('system_res', { type: 'error', error: 'unknown system command' }) + remote.emit('system_res', { + type: 'error', + error: 'unknown system command', + uuid: data.uuid, + }) break } }) diff --git a/app/relay/runner.js b/app/relay/runner.js index 2713a2e..6620bca 100644 --- a/app/relay/runner.js +++ b/app/relay/runner.js @@ -5,6 +5,8 @@ import kill from 'tree-kill' import { remote } from './remote' import { set_connected } from './rpc' import uuidv1 from 'uuid/v1' +import * as fs from 'fs' +import * as path from 'path' const idle_state = { status: 'IDLE', task: {} } @@ -108,6 +110,33 @@ export function run_system_command(cmd, cb) { } } +export function list_directory(opt, cb) { + if (!opt.module || ! modules[opt.module]) { + cb([]) + } + const module = modules[opt.module] + const dir = path.join(module.cwd, opt.dir.replace(/\.\.?\//g, '')) + fs.readdir(dir, (err, files) => { + const statPromises = files.filter(f => f[0] !== '.').map(f => { + return new Promise((resolve, reject) => { + const full_path = path.join(dir, f) + fs.stat(full_path, (err, stat={}) => { + resolve({ + name: f, + path: full_path, + date: stat.ctime, + size: stat.size, + dir: stat.isDirectory(), + }) + }) + }) + }) + Promise.all(statPromises).then(stats => { + cb(stats) + }) + }) +} + export function run_task(task, preempt, watch){ const module = modules[task.module] if (! module) return { type: 'error', error: "No such module: " + task.module } @@ -136,6 +165,7 @@ export function run_task(task, preempt, watch){ console.log(module.cwd) console.log(interpreter.cmd, params) + task.started = new Date().toString() const subprocess = spawn(interpreter.cmd, params, { cwd: module.cwd, }) |
