populate.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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:no-console
  6. /**
  7. * @description
  8. * Populates the Vendure server with some initial data and (optionally) product data from
  9. * a supplied CSV file.
  10. *
  11. * @docsCategory import-export
  12. */
  13. export async function populate(
  14. bootstrapFn: () => Promise<INestApplication | undefined>,
  15. initialDataPathOrObject: string | object,
  16. productsCsvPath?: string,
  17. ): Promise<INestApplication> {
  18. const app = await bootstrapFn();
  19. if (!app) {
  20. throw new Error('Could not bootstrap the Vendure app');
  21. }
  22. const initialData: import('@vendure/core').InitialData =
  23. typeof initialDataPathOrObject === 'string'
  24. ? require(initialDataPathOrObject)
  25. : initialDataPathOrObject;
  26. await populateInitialData(app, initialData, logColored);
  27. if (productsCsvPath) {
  28. const importResult = await importProductsFromCsv(app, productsCsvPath, initialData.defaultLanguage);
  29. if (importResult.errors && importResult.errors.length) {
  30. const errorFile = path.join(process.cwd(), 'vendure-import-error.log');
  31. console.log(
  32. `${importResult.errors.length} errors encountered when importing product data. See: ${errorFile}`,
  33. );
  34. await fs.writeFile(errorFile, importResult.errors.join('\n'));
  35. }
  36. logColored(`\nImported ${importResult.imported} products`);
  37. await populateCollections(app, initialData, logColored);
  38. }
  39. logColored('\nDone!');
  40. return app;
  41. }
  42. export async function populateInitialData(
  43. app: INestApplication,
  44. initialData: import('@vendure/core').InitialData,
  45. loggingFn?: (message: string) => void,
  46. ) {
  47. const { Populator } = await import('@vendure/core');
  48. const populator = app.get(Populator);
  49. try {
  50. await populator.populateInitialData(initialData);
  51. if (typeof loggingFn === 'function') {
  52. loggingFn(`Populated initial data`);
  53. }
  54. } catch (err) {
  55. console.error(err.message);
  56. }
  57. }
  58. export async function populateCollections(
  59. app: INestApplication,
  60. initialData: import('@vendure/core').InitialData,
  61. loggingFn?: (message: string) => void,
  62. ) {
  63. const { Populator } = await import('@vendure/core');
  64. const populator = app.get(Populator);
  65. try {
  66. if (initialData.collections.length) {
  67. await populator.populateCollections(initialData);
  68. if (typeof loggingFn === 'function') {
  69. loggingFn(`Created ${initialData.collections.length} Collections`);
  70. }
  71. }
  72. } catch (err) {
  73. console.error(err.message);
  74. }
  75. }
  76. export async function importProductsFromCsv(
  77. app: INestApplication,
  78. productsCsvPath: string,
  79. languageCode: import('@vendure/core').LanguageCode,
  80. ): Promise<import('@vendure/core').ImportProgress> {
  81. const { Importer } = await import('@vendure/core');
  82. const importer = app.get(Importer);
  83. const productData = await fs.readFile(productsCsvPath, 'utf-8');
  84. return importer.parseAndImport(productData, languageCode, true).toPromise();
  85. }