Browse Source

refactor(core): Move non-exported function into vendure-cli.ts

Michael Bromley 5 years ago
parent
commit
35c4c2e445
2 changed files with 82 additions and 83 deletions
  1. 12 82
      packages/core/src/cli/populate.ts
  2. 70 1
      packages/core/src/cli/vendure-cli.ts

+ 12 - 82
packages/core/src/cli/populate.ts

@@ -25,95 +25,25 @@ export async function populate(
         typeof initialDataPathOrObject === 'string'
             ? require(initialDataPathOrObject)
             : initialDataPathOrObject;
-    await populateInitialData(app, initialData, logColored);
-    if (productsCsvPath) {
-        await importProductsWithLogging(app, productsCsvPath, initialData.defaultLanguage);
-        await populateCollections(app, initialData, logColored);
-    }
-    logColored('\nDone!');
-    return app;
-}
 
-export async function importProducts(csvPath: string, languageCode: import('@vendure/core').LanguageCode) {
-    logColored(`\nImporting from "${csvPath}"...\n`);
-    const app = await getApplicationRef();
-    if (app) {
-        await importProductsFromCsv(app, csvPath, languageCode);
-        logColored('\nDone!');
-        await app.close();
-        process.exit(0);
-    }
-}
-
-async function importProductsWithLogging(
-    app: INestApplication,
-    productsCsvPath: string,
-    languageCode: import('@vendure/core').LanguageCode,
-) {
-    const importResult = await importProductsFromCsv(app, productsCsvPath, languageCode);
-    if (importResult.errors && importResult.errors.length) {
-        const errorFile = path.join(process.cwd(), 'vendure-import-error.log');
-        console.log(
-            `${importResult.errors.length} errors encountered when importing product data. See: ${errorFile}`,
-        );
-        await fs.writeFile(errorFile, importResult.errors.join('\n'));
-    }
-
-    logColored(`\nImported ${importResult.imported} products`);
-}
-
-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;
-    let configFile: string | undefined;
-    if (fs.existsSync(tsConfigFile)) {
-        configFile = tsConfigFile;
-        isTs = true;
-    } else if (fs.existsSync(jsConfigFile)) {
-        configFile = jsConfigFile;
-    }
-
-    if (!configFile) {
-        console.error(`Could not find a config file`);
-        console.error(`Checked "${tsConfigFile}", "${jsConfigFile}"`);
-        process.exit(1);
-        return;
-    }
+    await populateInitialData(app, initialData, logColored);
 
-    if (isTs) {
-        // we expect ts-node to be available
-        const tsNode = require('ts-node');
-        if (!tsNode) {
-            console.error(`For "populate" to work with TypeScript projects, you must have ts-node installed`);
-            process.exit(1);
-            return;
+    if (productsCsvPath) {
+        const importResult = await importProductsFromCsv(app, productsCsvPath, initialData.defaultLanguage);
+        if (importResult.errors && importResult.errors.length) {
+            const errorFile = path.join(process.cwd(), 'vendure-import-error.log');
+            console.log(
+                `${importResult.errors.length} errors encountered when importing product data. See: ${errorFile}`,
+            );
+            await fs.writeFile(errorFile, importResult.errors.join('\n'));
         }
-        require('ts-node').register();
-    }
 
-    const index = require(configFile);
+        logColored(`\nImported ${importResult.imported} products`);
 
-    if (!index) {
-        console.error(`Could not read the contents of "${configFile}"`);
-        process.exit(1);
-        return;
-    }
-    if (!index.config) {
-        console.error(`The file "${configFile}" does not export a "config" object`);
-        process.exit(1);
-        return;
+        await populateCollections(app, initialData, logColored);
     }
 
-    const config = index.config;
-
-    // Force the sync mode on, so that all the tables are created
-    // on this initial run.
-    (config.dbConnectionOptions as any).synchronize = true;
-
-    const { bootstrap } = require('@vendure/core');
-    console.log('Bootstrapping Vendure server...');
-    const app = await bootstrap(config);
+    logColored('\nDone!');
     return app;
 }
 

+ 70 - 1
packages/core/src/cli/vendure-cli.ts

@@ -1,9 +1,11 @@
 #!/usr/bin/env node
+import { INestApplication } from '@nestjs/common';
 import program from 'commander';
+import fs from 'fs-extra';
 import path from 'path';
 
 import { logColored } from './cli-utils';
-import { getApplicationRef, importProducts, populateCollections, populateInitialData } from './populate';
+import { importProductsFromCsv, populateCollections, populateInitialData } from './populate';
 // tslint:disable-next-line:no-var-requires
 const version = require('../../package.json').version;
 
@@ -18,6 +20,7 @@ logColored(`
                                        `);
 
 program.version(`Vendure CLI v${version}`, '-v --version').name('vendure');
+
 program
     .command('import-products <csvFile>')
     .option('-l, --language', 'Specify ISO 639-1 language code, e.g. "de", "es". Defaults to "en"')
@@ -60,3 +63,69 @@ program.parse(process.argv);
 if (!process.argv.slice(2).length) {
     program.help();
 }
+
+async function importProducts(csvPath: string, languageCode: import('@vendure/core').LanguageCode) {
+    logColored(`\nImporting from "${csvPath}"...\n`);
+    const app = await getApplicationRef();
+    if (app) {
+        await importProductsFromCsv(app, csvPath, languageCode);
+        logColored('\nDone!');
+        await app.close();
+        process.exit(0);
+    }
+}
+
+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;
+    let configFile: string | undefined;
+    if (fs.existsSync(tsConfigFile)) {
+        configFile = tsConfigFile;
+        isTs = true;
+    } else if (fs.existsSync(jsConfigFile)) {
+        configFile = jsConfigFile;
+    }
+
+    if (!configFile) {
+        console.error(`Could not find a config file`);
+        console.error(`Checked "${tsConfigFile}", "${jsConfigFile}"`);
+        process.exit(1);
+        return;
+    }
+
+    if (isTs) {
+        // we expect ts-node to be available
+        const tsNode = require('ts-node');
+        if (!tsNode) {
+            console.error(`For "populate" to work with TypeScript projects, you must have ts-node installed`);
+            process.exit(1);
+            return;
+        }
+        require('ts-node').register();
+    }
+
+    const index = require(configFile);
+
+    if (!index) {
+        console.error(`Could not read the contents of "${configFile}"`);
+        process.exit(1);
+        return;
+    }
+    if (!index.config) {
+        console.error(`The file "${configFile}" does not export a "config" object`);
+        process.exit(1);
+        return;
+    }
+
+    const config = index.config;
+
+    // Force the sync mode on, so that all the tables are created
+    // on this initial run.
+    (config.dbConnectionOptions as any).synchronize = true;
+
+    const { bootstrap } = require('@vendure/core');
+    console.log('Bootstrapping Vendure server...');
+    const app = await bootstrap(config);
+    return app;
+}