diff --git a/.eslintrc.js b/.eslintrc.js index 43b12bd57..e21f94580 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -19,7 +19,7 @@ module.exports = { overrides: [ { - files: ['packages/*cli*/**/*.js', '*-cli.js'], + files: ['cli.js', '*-cli.js', 'packages/*cli*/**/*.js'], rules: { 'no-console': 'off', }, diff --git a/packages/xen-api/package.json b/packages/xen-api/package.json index d36ae8a98..fb1672a2a 100644 --- a/packages/xen-api/package.json +++ b/packages/xen-api/package.json @@ -33,6 +33,7 @@ "node": ">=6" }, "dependencies": { + "bind-property-descriptor": "^1.0.0", "blocked": "^1.2.1", "debug": "^4.0.1", "event-to-promise": "^0.8.0", diff --git a/packages/xen-api/src/cli.js b/packages/xen-api/src/cli.js index aff116f91..dd6badeff 100755 --- a/packages/xen-api/src/cli.js +++ b/packages/xen-api/src/cli.js @@ -9,6 +9,7 @@ import minimist from 'minimist' import pw from 'pw' import { asCallback, fromCallback } from 'promise-toolbox' import { filter, find, isArray } from 'lodash' +import { getBoundPropertyDescriptor } from 'bind-property-descriptor' import { start as createRepl } from 'repl' import { createClient } from './' @@ -25,6 +26,20 @@ function askPassword(prompt = 'Password: ') { }) } +const { getPrototypeOf, ownKeys } = Reflect +function getAllBoundDescriptors(object) { + const descriptors = { __proto__: null } + let current = object + do { + ownKeys(current).forEach(key => { + if (!(key in descriptors)) { + descriptors[key] = getBoundPropertyDescriptor(current, key, object) + } + }) + } while ((current = getPrototypeOf(current)) !== null) + return descriptors +} + // =================================================================== const usage = 'Usage: xen-api [ []]' @@ -78,11 +93,17 @@ const main = async args => { const repl = createRepl({ prompt: `${xapi._humanId}> `, }) - repl.context.xapi = xapi - repl.context.diff = (a, b) => console.log('%s', diff(a, b)) - repl.context.find = predicate => find(xapi.objects.all, predicate) - repl.context.findAll = predicate => filter(xapi.objects.all, predicate) + { + const ctx = repl.context + ctx.xapi = xapi + + ctx.diff = (a, b) => console.log('%s', diff(a, b)) + ctx.find = predicate => find(xapi.objects.all, predicate) + ctx.findAll = predicate => filter(xapi.objects.all, predicate) + + Object.defineProperties(ctx, getAllBoundDescriptors(xapi)) + } // Make the REPL waits for promise completion. repl.eval = (evaluate => (cmd, context, filename, cb) => {