cli.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env node
  2. import yargs from 'yargs/yargs';
  3. import { hideBin } from 'yargs/helpers';
  4. import startServer from './server.js';
  5. import { generateKey } from './crypt.js';
  6. import repl from 'repl';
  7. import { getStoreBackend, wrapBackend } from './store/backends/index.js';
  8. import {
  9. STORE_BACKEND,
  10. STORE_PREFIX,
  11. NEDB_BACKEND_DIRNAME,
  12. } from './settings.js';
  13. yargs(hideBin(process.argv))
  14. .usage('Usage: $0 [options]')
  15. .command(
  16. '$0',
  17. 'Start the Ricochet.js server',
  18. () => {},
  19. (argv) => {
  20. if (argv.generateKey) {
  21. const key = generateKey();
  22. console.log(`Key: ${key}`);
  23. return;
  24. }
  25. console.log('Should start the server');
  26. startServer();
  27. }
  28. )
  29. .command(
  30. ['generatekey', 'generateKey'],
  31. 'Generate random encryption key',
  32. () => {},
  33. () => {
  34. const key = generateKey();
  35. console.log(`Key: ${key}`);
  36. }
  37. )
  38. .command(
  39. 'shell <siteId>',
  40. 'Open a shell for specified siteId',
  41. () => {},
  42. (argv) => {
  43. const siteId = argv.siteId;
  44. const storeConfig = {
  45. prefix: STORE_PREFIX,
  46. dirname: NEDB_BACKEND_DIRNAME,
  47. };
  48. // Create JSON wrapped store backend
  49. const storeBackend = getStoreBackend(STORE_BACKEND, storeConfig);
  50. const store = wrapBackend(storeBackend, siteId);
  51. const r = repl.start('> ');
  52. r.context.store = store;
  53. }
  54. )
  55. .boolean(['generate-key'])
  56. .describe('generate-key', 'Generate random encryption key')
  57. .help('h')
  58. .version()
  59. .alias('h', 'help').argv;