Skip to content

Commit

Permalink
feat(core): Expose init CLI command
Browse files Browse the repository at this point in the history
Used to populate initial data like countries & tax rates
  • Loading branch information
michaelbromley committed May 13, 2019
1 parent 0aa0bc8 commit 4d5f0d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
20 changes: 13 additions & 7 deletions packages/core/cli/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -104,13 +104,19 @@ async function getApplicationRef(): Promise<INestApplication | undefined> {
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) {
Expand Down
16 changes: 15 additions & 1 deletion packages/core/cli/vendure-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,6 +26,20 @@ program
const filePath = path.join(process.cwd(), csvPath);
await importProducts(filePath, command.language);
});
program
.command('init <initDataFile>')
.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();
Expand Down

0 comments on commit 4d5f0d9

Please sign in to comment.