populate.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { INestApplication } from '@nestjs/common';
  2. import fs from 'fs-extra';
  3. import path from 'path';
  4. import { logColored } from './cli-utils';
  5. // tslint:disable-next-line:no-var-requires
  6. const { Populator, Importer } = require('@vendure/core');
  7. // tslint:disable:no-console
  8. export async function populate() {
  9. logColored('\nPopulating... (this may take a minute or two)\n');
  10. const app = await getApplicationRef();
  11. if (app) {
  12. const initialData = require('./assets/initial-data.json');
  13. await populateInitialData(app, initialData);
  14. await populateProducts(app, initialData);
  15. logColored('\nDone!');
  16. await app.close();
  17. process.exit(0);
  18. }
  19. }
  20. export async function importProducts(csvPath: string, languageCode: string) {
  21. logColored(`\nImporting from "${csvPath}"...\n`);
  22. const app = await getApplicationRef();
  23. if (app) {
  24. await importProductsFromFile(app, csvPath, languageCode);
  25. logColored('\nDone!');
  26. await app.close();
  27. process.exit(0);
  28. }
  29. }
  30. async function getApplicationRef(): Promise<INestApplication | undefined> {
  31. const tsConfigFile = path.join(process.cwd(), 'vendure-config.ts');
  32. const jsConfigFile = path.join(process.cwd(), 'vendure-config.js');
  33. let isTs = false;
  34. let configFile: string | undefined;
  35. if (fs.existsSync(tsConfigFile)) {
  36. configFile = tsConfigFile;
  37. isTs = true;
  38. } else if (fs.existsSync(jsConfigFile)) {
  39. configFile = jsConfigFile;
  40. }
  41. if (!configFile) {
  42. console.error(`Could not find a config file`);
  43. console.error(`Checked "${tsConfigFile}", "${jsConfigFile}"`);
  44. process.exit(1);
  45. return;
  46. }
  47. if (isTs) {
  48. // we expect ts-node to be available
  49. const tsNode = require('ts-node');
  50. if (!tsNode) {
  51. console.error(`For "populate" to work with TypeScript projects, you must have ts-node installed`);
  52. process.exit(1);
  53. return;
  54. }
  55. require('ts-node').register();
  56. }
  57. const index = require(configFile);
  58. if (!index) {
  59. console.error(`Could not read the contents of "${configFile}"`);
  60. process.exit(1);
  61. return;
  62. }
  63. if (!index.config) {
  64. console.error(`The file "${configFile}" does not export a "config" object`);
  65. process.exit(1);
  66. return;
  67. }
  68. const config = index.config;
  69. config.silent = true;
  70. // Force the sync mode on, so that all the tables are created
  71. // on this initial run.
  72. config.dbConnectionOptions.synchronize = true;
  73. const { bootstrap } = require('@vendure/core');
  74. console.log('Bootstrapping Vendure server...');
  75. const app = await bootstrap(config);
  76. return app;
  77. }
  78. async function populateInitialData(app: INestApplication, initialData: any) {
  79. const populator = app.get(Populator);
  80. try {
  81. await populator.populateInitialData(initialData);
  82. } catch (err) {
  83. console.error(err.message);
  84. }
  85. }
  86. async function populateProducts(app: INestApplication, initialData: any) {
  87. // copy the images to the import folder
  88. const images = path.join(__dirname, 'assets', 'images');
  89. const destination = path.join(process.cwd(), 'vendure', 'import-assets');
  90. await fs.copy(images, destination);
  91. // import the csv of same product data
  92. const sampleProductsFile = path.join(__dirname, 'assets', 'products.csv');
  93. await importProductsFromFile(app, sampleProductsFile, initialData.defaultLanguage);
  94. }
  95. async function importProductsFromFile(app: INestApplication, csvPath: string, languageCode: string) {
  96. // import the csv of same product data
  97. const importer = app.get(Importer);
  98. const productData = await fs.readFile(csvPath, 'utf-8');
  99. const importResult = await importer.parseAndImport(productData, languageCode, true).toPromise();
  100. if (importResult.errors.length) {
  101. const errorFile = path.join(process.cwd(), 'vendure-import-error.log');
  102. console.log(
  103. `${importResult.errors.length} errors encountered when importing product data. See: ${errorFile}`,
  104. );
  105. await fs.writeFile(errorFile, importResult.errors.join('\n'));
  106. }
  107. logColored(`\nImported ${importResult.imported} products`);
  108. }