populate.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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:no-console
  7. export async function populate() {
  8. logColored('\nPopulating... (this may take a minute or two)\n');
  9. const app = await getApplicationRef();
  10. if (app) {
  11. const { Populator, Importer } = require('vendure');
  12. const initialData = require('./assets/initial-data.json');
  13. await populateInitialData(app, initialData, Populator);
  14. await populateProducts(app, initialData, Importer);
  15. logColored('\nDone!');
  16. await app.close();
  17. process.exit(0);
  18. }
  19. }
  20. async function getApplicationRef(): Promise<INestApplication | undefined> {
  21. const tsConfigFile = path.join(process.cwd(), 'vendure-config.ts');
  22. const jsConfigFile = path.join(process.cwd(), 'vendure-config.js');
  23. let isTs = false;
  24. let configFile: string | undefined;
  25. if (fs.existsSync(tsConfigFile)) {
  26. configFile = tsConfigFile;
  27. isTs = true;
  28. } else if (fs.existsSync(jsConfigFile)) {
  29. configFile = jsConfigFile;
  30. }
  31. if (!configFile) {
  32. console.error(`Could not find a config file`);
  33. console.error(`Checked "${tsConfigFile}", "${jsConfigFile}"`);
  34. process.exit(1);
  35. return;
  36. }
  37. if (isTs) {
  38. // we expect ts-node to be available
  39. const tsNode = require('ts-node');
  40. if (!tsNode) {
  41. console.error(`For "populate" to work with TypeScript projects, you must have ts-node installed`);
  42. process.exit(1);
  43. return;
  44. }
  45. require('ts-node').register();
  46. }
  47. const index = require(configFile);
  48. if (!index) {
  49. console.error(`Could not read the contents of "${configFile}"`);
  50. process.exit(1);
  51. return;
  52. }
  53. if (!index.config) {
  54. console.error(`The file "${configFile}" does not export a "config" object`);
  55. process.exit(1);
  56. return;
  57. }
  58. const config = index.config;
  59. const { bootstrap } = require('vendure');
  60. console.log('Bootstrapping Vendure server...');
  61. const app = await bootstrap(config);
  62. return app;
  63. }
  64. async function populateInitialData(app: INestApplication, initialData: any, Populator: any) {
  65. const populator = app.get(Populator);
  66. try {
  67. await populator.populateInitialData(initialData);
  68. } catch (err) {
  69. console.error(err.message);
  70. }
  71. }
  72. async function populateProducts(app: INestApplication, initialData: any, Importer: any) {
  73. // copy the images to the import folder
  74. const images = path.join(__dirname, 'assets', 'images');
  75. const destination = path.join(process.cwd(), 'vendure', 'import-assets');
  76. await fs.copy(images, destination);
  77. // import the csv of same product data
  78. const importer = app.get(Importer);
  79. const productData = await fs.readFile(path.join(__dirname, 'assets', 'sample-products.csv'), 'utf-8');
  80. const importResult = await importer.parseAndImport(productData, initialData.defaultLanguage);
  81. if (importResult.errors.length) {
  82. console.error(`Error encountered when importing product data:`);
  83. console.error(importResult.errors.join('\n'));
  84. } else {
  85. console.log(`Imported ${importResult.importedCount} products`);
  86. await fs.emptyDir(destination);
  87. }
  88. }