Bläddra i källkod

feat(core): Export populate-collections CLI command

Michael Bromley 6 år sedan
förälder
incheckning
0aef0b7875
2 ändrade filer med 28 tillägg och 18 borttagningar
  1. 9 15
      packages/core/cli/populate.ts
  2. 19 3
      packages/core/cli/vendure-cli.ts

+ 9 - 15
packages/core/cli/populate.ts

@@ -29,7 +29,7 @@ export async function populate(
         throw new Error('Could not bootstrap the Vendure app');
     }
     const initialData = require(initialDataPath);
-    await populateInitialData(initialData, app);
+    await populateInitialData(app, initialData);
     if (productsCsvPath) {
         await importProductsFromFile(app, productsCsvPath, initialData.defaultLanguage);
         await populateCollections(app, initialData);
@@ -49,7 +49,7 @@ export async function importProducts(csvPath: string, languageCode: string) {
     }
 }
 
-async function getApplicationRef(): Promise<INestApplication | undefined> {
+export async function getApplicationRef(): Promise<INestApplication | undefined> {
     const tsConfigFile = path.join(process.cwd(), 'vendure-config.ts');
     const jsConfigFile = path.join(process.cwd(), 'vendure-config.js');
     let isTs = false;
@@ -104,22 +104,16 @@ async function getApplicationRef(): Promise<INestApplication | undefined> {
     return app;
 }
 
-export async function populateInitialData(initialData: object, app?: INestApplication) {
-    if (!app) {
-        app = await getApplicationRef();
-    }
-    if (app) {
-        const populator = app.get(Populator);
-        try {
-            await populator.populateInitialData(initialData);
-        } catch (err) {
-            console.error(err.message);
-        }
+export async function populateInitialData(app: INestApplication, initialData: object) {
+    const populator = app.get(Populator);
+    try {
+        await populator.populateInitialData(initialData);
+    } catch (err) {
+        console.error(err.message);
     }
-    return app;
 }
 
-async function populateCollections(app: INestApplication, initialData: object) {
+export async function populateCollections(app: INestApplication, initialData: object) {
     const populator = app.get(Populator);
     try {
         await populator.populateCollections(initialData);

+ 19 - 3
packages/core/cli/vendure-cli.ts

@@ -3,7 +3,7 @@ import program from 'commander';
 import path from 'path';
 
 import { logColored } from './cli-utils';
-import { importProducts, populateInitialData } from './populate';
+import { getApplicationRef, importProducts, populateCollections, populateInitialData } from './populate';
 // tslint:disable-next-line:no-var-requires
 const version = require('../../package.json').version;
 
@@ -33,9 +33,25 @@ program
         const filePath = path.join(process.cwd(), initDataFile);
         logColored(`\nPopulating initial data from "${filePath}"...\n`);
         const initialData = require(filePath);
-        const app = await populateInitialData(initialData);
-        logColored('\nDone!');
+        const app = await getApplicationRef();
         if (app) {
+            await populateInitialData(app, initialData);
+            logColored('\nDone!');
+            await app.close();
+        }
+        process.exit(0);
+    });
+program
+    .command('create-collections <initDataFile>')
+    .description('Create collections from the specified json file')
+    .action(async (initDataFile, command) => {
+        const filePath = path.join(process.cwd(), initDataFile);
+        logColored(`\nCreating collections from "${filePath}"...\n`);
+        const initialData = require(filePath);
+        const app = await getApplicationRef();
+        if (app) {
+            await populateCollections(app, initialData);
+            logColored('\nDone!');
             await app.close();
         }
         process.exit(0);