vendure-cli.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env node
  2. import program from 'commander';
  3. import path from 'path';
  4. import { logColored } from './cli-utils';
  5. import { getApplicationRef, importProducts, populateCollections, populateInitialData } from './populate';
  6. // tslint:disable-next-line:no-var-requires
  7. const version = require('../../package.json').version;
  8. // tslint:disable:no-console
  9. logColored(`
  10. _
  11. | |
  12. __ _____ _ __ __| |_ _ _ __ ___
  13. \\ \\ / / _ \\ '_ \\ / _\` | | | | '__/ _ \\
  14. \\ V / __/ | | | (_| | |_| | | | __/
  15. \\_/ \\___|_| |_|\\__,_|\\__,_|_| \\___|
  16. `);
  17. program.version(`Vendure CLI v${version}`, '-v --version').name('vendure');
  18. program
  19. .command('import-products <csvFile>')
  20. .option('-l, --language', 'Specify ISO 639-1 language code, e.g. "de", "es". Defaults to "en"')
  21. .description('Import product data from the specified csv file')
  22. .action(async (csvPath, command) => {
  23. const filePath = path.join(process.cwd(), csvPath);
  24. await importProducts(filePath, command.language);
  25. });
  26. program
  27. .command('init <initDataFile>')
  28. .description('Import initial data from the specified json file')
  29. .action(async (initDataFile, command) => {
  30. const filePath = path.join(process.cwd(), initDataFile);
  31. logColored(`\nPopulating initial data from "${filePath}"...\n`);
  32. const initialData = require(filePath);
  33. const app = await getApplicationRef();
  34. if (app) {
  35. await populateInitialData(app, initialData);
  36. logColored('\nDone!');
  37. await app.close();
  38. }
  39. process.exit(0);
  40. });
  41. program
  42. .command('create-collections <initDataFile>')
  43. .description('Create collections from the specified json file')
  44. .action(async (initDataFile, command) => {
  45. const filePath = path.join(process.cwd(), initDataFile);
  46. logColored(`\nCreating collections from "${filePath}"...\n`);
  47. const initialData = require(filePath);
  48. const app = await getApplicationRef();
  49. if (app) {
  50. await populateCollections(app, initialData);
  51. logColored('\nDone!');
  52. await app.close();
  53. }
  54. process.exit(0);
  55. });
  56. program.parse(process.argv);
  57. if (!process.argv.slice(2).length) {
  58. program.help();
  59. }