From 5013c72ce2794b11a53f2eb3acb1886f145e2601 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Tue, 16 Jul 2024 14:42:18 +0200 Subject: [PATCH 1/2] refactor: CLI and export core libs for public use. --- package.json | 6 +-- src/bin/cli.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 100 +++---------------------------------------------- 3 files changed, 102 insertions(+), 99 deletions(-) create mode 100755 src/bin/cli.js diff --git a/package.json b/package.json index 4eaf654..2629de7 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,8 @@ "type": "module", "author": "OrbitDB", "license": "MIT", - "exports": { - "./orbiter": "./src/orbiter.js", - "./lander": "./src/lander.js" - }, "bin": { - "voyager": "./src/index.js" + "voyager": "./src/bin/cli.js" }, "dependencies": { "@chainsafe/libp2p-gossipsub": "^12.0.0", diff --git a/src/bin/cli.js b/src/bin/cli.js new file mode 100755 index 0000000..a609a46 --- /dev/null +++ b/src/bin/cli.js @@ -0,0 +1,95 @@ +#!/usr/bin/env node + +import yargs from 'yargs' +import { hideBin } from 'yargs/helpers' +import daemon from './daemon.js' +import RPC from './rpc-client.js' +import { Responses } from './lib/messages/index.js' + +yargs(hideBin(process.argv)) + .scriptName('voyager') + .command( + 'daemon', + 'Launch Voyager', + () => {}, + async (argv) => { + await daemon({ options: argv }) + }) + .command('auth', 'Add/remove authorized addresses', yargs => { + yargs + .command( + 'add ', + 'Add an authorized address', + yargs => { + yargs.positional('id', { + describe: 'The id of the user who is allowed to pin one or more databases (or denied depending on default access settings).', + type: 'string' + }) + }, + async argv => { + const { authAdd } = await RPC(argv) + const res = await authAdd(argv) + if (res.type === Responses.OK) { + console.log('ok') + process.exit(0) + } else { + console.error(res) + process.exit(1) + } + }) + .command( + 'del ', + 'Remove an authorized address', + yargs => { + yargs.positional('id', { + describe: 'The id of the user who will no longer be allowed to pin one or more databases (or denied depending on default access settings).', + type: 'string' + }) + }, + async argv => { + const { authDel } = await RPC(argv) + const res = await authDel(argv) + if (res.type === Responses.OK) { + console.log('ok') + process.exit(0) + } else { + console.error(res) + process.exit(1) + } + }) + .command( + 'list', + 'List authorized addresses', + () => {}, + async argv => { + const { authList } = await RPC(argv) + const res = await authList() + if (res.type === Responses.OK) { + for (const id of res.message) { + console.log(id) + } + process.exit(0) + } else { + console.error(res) + process.exit(1) + } + }) + .demandCommand(1, 'Error: use add or remove') + }) + .option('verbose', { + alias: 'v', + description: 'Be more verbose. Outputs errors and other connection messages. Use multiple -vvv for more verbose logging.', + type: 'count' + }) + .option('directory', { + alias: 'd', + type: 'string', + description: 'Specify a directory to store IPFS and OrbitDB data.' + }).option('allow', { + alias: 'a', + type: 'boolean', + description: 'Allow anyone to pin a database. The default is false.' + }) + .demandCommand(1, 'Error: specify a command.') + .help() + .parse() diff --git a/src/index.js b/src/index.js index a609a46..2d85fe5 100755 --- a/src/index.js +++ b/src/index.js @@ -1,95 +1,7 @@ -#!/usr/bin/env node +import Orbiter from './lib/orbiter.js' +import Lander from './lib/lander.js' -import yargs from 'yargs' -import { hideBin } from 'yargs/helpers' -import daemon from './daemon.js' -import RPC from './rpc-client.js' -import { Responses } from './lib/messages/index.js' - -yargs(hideBin(process.argv)) - .scriptName('voyager') - .command( - 'daemon', - 'Launch Voyager', - () => {}, - async (argv) => { - await daemon({ options: argv }) - }) - .command('auth', 'Add/remove authorized addresses', yargs => { - yargs - .command( - 'add ', - 'Add an authorized address', - yargs => { - yargs.positional('id', { - describe: 'The id of the user who is allowed to pin one or more databases (or denied depending on default access settings).', - type: 'string' - }) - }, - async argv => { - const { authAdd } = await RPC(argv) - const res = await authAdd(argv) - if (res.type === Responses.OK) { - console.log('ok') - process.exit(0) - } else { - console.error(res) - process.exit(1) - } - }) - .command( - 'del ', - 'Remove an authorized address', - yargs => { - yargs.positional('id', { - describe: 'The id of the user who will no longer be allowed to pin one or more databases (or denied depending on default access settings).', - type: 'string' - }) - }, - async argv => { - const { authDel } = await RPC(argv) - const res = await authDel(argv) - if (res.type === Responses.OK) { - console.log('ok') - process.exit(0) - } else { - console.error(res) - process.exit(1) - } - }) - .command( - 'list', - 'List authorized addresses', - () => {}, - async argv => { - const { authList } = await RPC(argv) - const res = await authList() - if (res.type === Responses.OK) { - for (const id of res.message) { - console.log(id) - } - process.exit(0) - } else { - console.error(res) - process.exit(1) - } - }) - .demandCommand(1, 'Error: use add or remove') - }) - .option('verbose', { - alias: 'v', - description: 'Be more verbose. Outputs errors and other connection messages. Use multiple -vvv for more verbose logging.', - type: 'count' - }) - .option('directory', { - alias: 'd', - type: 'string', - description: 'Specify a directory to store IPFS and OrbitDB data.' - }).option('allow', { - alias: 'a', - type: 'boolean', - description: 'Allow anyone to pin a database. The default is false.' - }) - .demandCommand(1, 'Error: specify a command.') - .help() - .parse() +export { + Orbiter, + Lander +} \ No newline at end of file From f2bd034a9766d07ce55a270ceecaef902043b797 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Tue, 16 Jul 2024 14:44:11 +0200 Subject: [PATCH 2/2] chore: Linting. --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 2d85fe5..26d5544 100755 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,6 @@ import Orbiter from './lib/orbiter.js' import Lander from './lib/lander.js' export { - Orbiter, - Lander -} \ No newline at end of file + Orbiter, + Lander +}