vendure-cli.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env node
  2. import { INestApplication } from '@nestjs/common';
  3. import program from 'commander';
  4. import fs from 'fs-extra';
  5. import path from 'path';
  6. import { logColored } from './cli-utils';
  7. import { importProductsFromCsv, populateCollections, populateInitialData } from './populate';
  8. // tslint:disable-next-line:no-var-requires
  9. const version = require('../../package.json').version;
  10. // tslint:disable:no-console
  11. logColored(`
  12. _
  13. | |
  14. __ _____ _ __ __| |_ _ _ __ ___
  15. \\ \\ / / _ \\ '_ \\ / _\` | | | | '__/ _ \\
  16. \\ V / __/ | | | (_| | |_| | | | __/
  17. \\_/ \\___|_| |_|\\__,_|\\__,_|_| \\___|
  18. `);
  19. program.version(`Vendure CLI v${version}`, '-v --version').name('vendure');
  20. program
  21. .command('import-products <csvFile>')
  22. .option('-l, --language', 'Specify ISO 639-1 language code, e.g. "de", "es". Defaults to "en"')
  23. .description('Import product data from the specified csv file')
  24. .action(async (csvPath, command) => {
  25. const filePath = path.join(process.cwd(), csvPath);
  26. await importProducts(filePath, command.language);
  27. });
  28. program
  29. .command('init <initDataFile>')
  30. .description('Import initial data from the specified json file')
  31. .action(async (initDataFile, command) => {
  32. const filePath = path.join(process.cwd(), initDataFile);
  33. logColored(`\nPopulating initial data from "${filePath}"...\n`);
  34. const initialData = require(filePath);
  35. const app = await getApplicationRef();
  36. if (app) {
  37. await populateInitialData(app, initialData);
  38. logColored('\nDone!');
  39. await app.close();
  40. }
  41. process.exit(0);
  42. });
  43. program
  44. .command('create-collections <initDataFile>')
  45. .description('Create collections from the specified json file')
  46. .action(async (initDataFile, command) => {
  47. const filePath = path.join(process.cwd(), initDataFile);
  48. logColored(`\nCreating collections from "${filePath}"...\n`);
  49. const initialData = require(filePath);
  50. const app = await getApplicationRef();
  51. if (app) {
  52. await populateCollections(app, initialData);
  53. logColored('\nDone!');
  54. await app.close();
  55. }
  56. process.exit(0);
  57. });
  58. program.parse(process.argv);
  59. if (!process.argv.slice(2).length) {
  60. program.help();
  61. }
  62. async function importProducts(csvPath: string, languageCode: import('@vendure/core').LanguageCode) {
  63. logColored(`\nImporting from "${csvPath}"...\n`);
  64. const app = await getApplicationRef();
  65. if (app) {
  66. await importProductsFromCsv(app, csvPath, languageCode);
  67. logColored('\nDone!');
  68. await app.close();
  69. process.exit(0);
  70. }
  71. }
  72. async function getApplicationRef(): Promise<INestApplication | undefined> {
  73. const tsConfigFile = path.join(process.cwd(), 'vendure-config.ts');
  74. const jsConfigFile = path.join(process.cwd(), 'vendure-config.js');
  75. let isTs = false;
  76. let configFile: string | undefined;
  77. if (fs.existsSync(tsConfigFile)) {
  78. configFile = tsConfigFile;
  79. isTs = true;
  80. } else if (fs.existsSync(jsConfigFile)) {
  81. configFile = jsConfigFile;
  82. }
  83. if (!configFile) {
  84. console.error(`Could not find a config file`);
  85. console.error(`Checked "${tsConfigFile}", "${jsConfigFile}"`);
  86. process.exit(1);
  87. return;
  88. }
  89. if (isTs) {
  90. // we expect ts-node to be available
  91. const tsNode = require('ts-node');
  92. if (!tsNode) {
  93. console.error(`For "populate" to work with TypeScript projects, you must have ts-node installed`);
  94. process.exit(1);
  95. return;
  96. }
  97. require('ts-node').register();
  98. }
  99. const index = require(configFile);
  100. if (!index) {
  101. console.error(`Could not read the contents of "${configFile}"`);
  102. process.exit(1);
  103. return;
  104. }
  105. if (!index.config) {
  106. console.error(`The file "${configFile}" does not export a "config" object`);
  107. process.exit(1);
  108. return;
  109. }
  110. const config = index.config;
  111. // Force the sync mode on, so that all the tables are created
  112. // on this initial run.
  113. (config.dbConnectionOptions as any).synchronize = true;
  114. const { bootstrap } = require('@vendure/core');
  115. console.log('Bootstrapping Vendure server...');
  116. const app = await bootstrap(config);
  117. return app;
  118. }