Skip to content

Commit

Permalink
feat(core): Export populate-collections CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed May 15, 2019
1 parent c996fa7 commit 0aef0b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
24 changes: 9 additions & 15 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(initialData, app);
await populateInitialData(app, initialData);
if (productsCsvPath) {
await importProductsFromFile(app, productsCsvPath, initialData.defaultLanguage);
await populateCollections(app, initialData);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 19 additions & 3 deletions 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, populateInitialData } from './populate';
import { getApplicationRef, importProducts, populateCollections, populateInitialData } from './populate';
// tslint:disable-next-line:no-var-requires
const version = require('../../package.json').version;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0aef0b7

Please sign in to comment.