diff --git a/packages/core/cli/populate.ts b/packages/core/cli/populate.ts index e526ba91a3..9356ecd033 100644 --- a/packages/core/cli/populate.ts +++ b/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(app, initialData); + await populateInitialData(initialData, app); if (productsCsvPath) { await importProductsFromFile(app, productsCsvPath, initialData.defaultLanguage); await populateCollections(app, initialData); @@ -104,13 +104,19 @@ async function getApplicationRef(): Promise { return app; } -async function populateInitialData(app: INestApplication, initialData: object) { - const populator = app.get(Populator); - try { - await populator.populateInitialData(initialData); - } catch (err) { - console.error(err.message); +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); + } + } + return app; } async function populateCollections(app: INestApplication, initialData: object) { diff --git a/packages/core/cli/vendure-cli.ts b/packages/core/cli/vendure-cli.ts index fd980d89f6..625ecd1e07 100644 --- a/packages/core/cli/vendure-cli.ts +++ b/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 } from './populate'; +import { importProducts, populateInitialData } from './populate'; // tslint:disable-next-line:no-var-requires const version = require('../../package.json').version; @@ -26,6 +26,20 @@ program const filePath = path.join(process.cwd(), csvPath); await importProducts(filePath, command.language); }); +program + .command('init ') + .description('Import initial data from the specified json file') + .action(async (initDataFile, command) => { + 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!'); + if (app) { + await app.close(); + } + process.exit(0); + }); program.parse(process.argv); if (!process.argv.slice(2).length) { program.help();