populate.ts 4.5 KB

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