From 6d8848043cdc14ad5bc6fef6f4766e222e5bd260 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Fri, 1 Mar 2019 20:00:18 +0100 Subject: [PATCH] feat(vhd-cli): repl command --- packages/vhd-cli/src/commands/repl.js | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 packages/vhd-cli/src/commands/repl.js diff --git a/packages/vhd-cli/src/commands/repl.js b/packages/vhd-cli/src/commands/repl.js new file mode 100644 index 000000000..f6818820e --- /dev/null +++ b/packages/vhd-cli/src/commands/repl.js @@ -0,0 +1,31 @@ +import { asCallback, fromCallback, fromEvent } from 'promise-toolbox' +import { getHandler } from '@xen-orchestra/fs' +import { resolve } from 'path' +import { start as createRepl } from 'repl' +import * as Vhd from 'vhd-lib' + +export default async args => { + const handler = getHandler({ url: 'file://' + process.cwd() }) + await handler.sync() + try { + const repl = createRepl({ + prompt: 'vhd> ', + }) + Object.assign(repl.context, Vhd) + repl.context.open = path => new Vhd.Vhd(handler, resolve(path)) + + // Make the REPL waits for promise completion. + repl.eval = (evaluate => (cmd, context, filename, cb) => { + asCallback.call( + fromCallback(cb => { + evaluate.call(repl, cmd, context, filename, cb) + }).then(value => (Array.isArray(value) ? Promise.all(value) : value)), + cb + ) + })(repl.eval) + + await fromEvent(repl, 'exit') + } finally { + await handler.forget() + } +}