import-cli.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import path from 'path';
  2. import { devConfig } from '../../dev-config';
  3. import { SimpleGraphQLClient } from '../../mock-data/simple-graphql-client';
  4. import { bootstrap } from '../bootstrap';
  5. import { setConfig } from '../config/config-helpers';
  6. import { VendureConfig } from '../config/vendure-config';
  7. // tslint:disable:no-console
  8. /**
  9. * A CLI script which imports products from a CSV file.
  10. */
  11. if (require.main === module) {
  12. // Running from command line
  13. const csvFile = process.argv[2];
  14. const csvPath = path.join(__dirname, csvFile);
  15. getClient()
  16. .then(client => importProducts(client, csvPath))
  17. .then(
  18. () => process.exit(0),
  19. err => {
  20. console.log(err);
  21. process.exit(1);
  22. },
  23. );
  24. }
  25. async function getClient() {
  26. const config: VendureConfig = {
  27. ...devConfig,
  28. port: 3020,
  29. authOptions: {
  30. tokenMethod: 'bearer',
  31. },
  32. plugins: [],
  33. };
  34. (config.dbConnectionOptions as any).logging = false;
  35. setConfig(config);
  36. const app = await bootstrap(config);
  37. const client = new SimpleGraphQLClient(`http://localhost:${config.port}/${config.apiPath}`);
  38. client.setChannelToken(devConfig.defaultChannelToken || 'no-default-channel-token');
  39. await client.asSuperAdmin();
  40. return client;
  41. }
  42. async function importProducts(client: SimpleGraphQLClient, csvFile: string) {
  43. console.log(`loading data from "${csvFile}"`);
  44. const result = await client.importProducts(csvFile);
  45. console.log(result);
  46. }