populate.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { INestApplication } from '@nestjs/common';
  2. import * as fs from 'fs-extra';
  3. import * as path from 'path';
  4. import { Connection } from 'typeorm';
  5. import { logColored } from './cli-utils';
  6. // tslint:disable-next-line:no-var-requires
  7. const { Populator, Importer } = require('vendure');
  8. // tslint:disable:no-console
  9. export async function populate() {
  10. logColored('\nPopulating... (this may take a minute or two)\n');
  11. const app = await getApplicationRef();
  12. if (app) {
  13. const initialData = require('./assets/initial-data.json');
  14. await populateInitialData(app, initialData);
  15. await populateProducts(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}"... (this may take a minute or two)\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. const { bootstrap } = require('vendure');
  71. console.log('Bootstrapping Vendure server...');
  72. const app = await bootstrap(config);
  73. return app;
  74. }
  75. async function populateInitialData(app: INestApplication, initialData: any) {
  76. const populator = app.get(Populator);
  77. try {
  78. await populator.populateInitialData(initialData);
  79. } catch (err) {
  80. console.error(err.message);
  81. }
  82. }
  83. async function populateProducts(app: INestApplication, initialData: any) {
  84. // copy the images to the import folder
  85. const images = path.join(__dirname, 'assets', 'images');
  86. const destination = path.join(process.cwd(), 'vendure', 'import-assets');
  87. await fs.copy(images, destination);
  88. // import the csv of same product data
  89. const sampleProductsFile = path.join(__dirname, 'assets', 'sample-products.csv');
  90. await importProductsFromFile(app, sampleProductsFile, initialData.defaultLanguage);
  91. await fs.emptyDir(destination);
  92. }
  93. async function importProductsFromFile(app: INestApplication, csvPath: string, languageCode: string) {
  94. // import the csv of same product data
  95. const importer = app.get(Importer);
  96. const productData = await fs.readFile(csvPath, 'utf-8');
  97. const importResult = await importer.parseAndImport(productData, languageCode);
  98. if (importResult.errors.length) {
  99. console.error(`Error encountered when importing product data:`);
  100. console.error(importResult.errors.join('\n'));
  101. } else {
  102. console.log(`Imported ${importResult.importedCount} products`);
  103. }
  104. }