feat(fs): add basic CLI
This commit is contained in:
62
@xen-orchestra/fs/cli.js
Executable file
62
@xen-orchestra/fs/cli.js
Executable file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
'use strict'
|
||||
|
||||
const Disposable = require('promise-toolbox/Disposable')
|
||||
const { getBoundPropertyDescriptor } = require('bind-property-descriptor')
|
||||
|
||||
const { getSyncedHandler } = require('./')
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// https://gist.github.com/julien-f/18161f6032e808d6fa08782951ce3bfb
|
||||
async function repl({ prompt, context } = {}) {
|
||||
const repl = require('repl').start({
|
||||
ignoreUndefined: true,
|
||||
prompt,
|
||||
})
|
||||
if (context !== undefined) {
|
||||
Object.defineProperties(repl.context, Object.getOwnPropertyDescriptors(context))
|
||||
}
|
||||
const { eval: evaluate } = repl
|
||||
repl.eval = (cmd, context, filename, cb) => {
|
||||
evaluate.call(repl, cmd, context, filename, (error, result) => {
|
||||
if (error != null) {
|
||||
return cb(error)
|
||||
}
|
||||
Promise.resolve(result).then(result => cb(undefined, result), cb)
|
||||
})
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
repl.on('error', reject).on('exit', resolve)
|
||||
})
|
||||
}
|
||||
|
||||
async function* main([url]) {
|
||||
if (url === undefined) {
|
||||
throw new TypeError('missing arg <url>')
|
||||
}
|
||||
|
||||
const handler = yield getSyncedHandler({ url })
|
||||
await repl({
|
||||
prompt: handler.type + '> ',
|
||||
context: Object.create(null, getAllBoundDescriptors(handler)),
|
||||
})
|
||||
}
|
||||
|
||||
Disposable.wrap(main)(process.argv.slice(2)).catch(error => {
|
||||
console.error('FATAL:', error)
|
||||
process.exitCode = 1
|
||||
})
|
||||
@@ -13,6 +13,9 @@
|
||||
},
|
||||
"preferGlobal": true,
|
||||
"main": "dist/",
|
||||
"bin": {
|
||||
"xo-fs": "./cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14"
|
||||
},
|
||||
@@ -26,6 +29,7 @@
|
||||
"@xen-orchestra/async-map": "^0.1.2",
|
||||
"@xen-orchestra/log": "^0.3.0",
|
||||
"aws-sdk": "^2.686.0",
|
||||
"bind-property-descriptor": "^2.0.0",
|
||||
"decorator-synchronized": "^0.6.0",
|
||||
"execa": "^5.0.0",
|
||||
"fs-extra": "^10.0.0",
|
||||
|
||||
@@ -34,6 +34,6 @@
|
||||
|
||||
- xen-api major
|
||||
- @xen-orchestra/xapi minor
|
||||
- @xen-orchestra/fs patch
|
||||
- @xen-orchestra/fs major
|
||||
- vhd-cli minor
|
||||
- xo-server patch
|
||||
|
||||
Reference in New Issue
Block a user