Przeglądaj źródła

feat(create): Update to use separate worker process

Michael Bromley 6 lat temu
rodzic
commit
f3560f21e4

+ 15 - 8
packages/create/src/create-vendure-app.ts

@@ -62,22 +62,25 @@ async function createApp(name: string | undefined, useNpm: boolean, logLevel: Lo
 
     const root = path.resolve(name);
     const appName = path.basename(root);
-    const { dbType, usingTs, configSource, indexSource, populateProducts } = await gatherUserResponses(root);
+    const { dbType, usingTs, configSource, indexSource, indexWorkerSource, populateProducts } = await gatherUserResponses(root);
+
+    const useYarn = useNpm ? false : shouldUseYarn();
+    const originalDirectory = process.cwd();
+    process.chdir(root);
+    if (!useYarn && !checkThatNpmCanReadCwd()) {
+        process.exit(1);
+    }
 
     const packageJsonContents = {
         name: appName,
         version: '0.1.0',
         private: true,
         scripts: {
-            start: usingTs ? 'ts-node index.ts' : 'node index.js',
+            'run:server': usingTs ? 'ts-node index.ts' : 'node index.js',
+            'run:worker': usingTs ? 'ts-node index-worker.ts' : 'node index-worker.js',
+            'start': useYarn ? 'concurrently yarn:run:*' : 'concurrently npm:run:*',
         },
     };
-    const useYarn = useNpm ? false : shouldUseYarn();
-    const originalDirectory = process.cwd();
-    process.chdir(root);
-    if (!useYarn && !checkThatNpmCanReadCwd()) {
-        process.exit(1);
-    }
 
     console.log();
     console.log(`Setting up your new Vendure project in ${chalk.green(root)}`);
@@ -124,6 +127,10 @@ async function createApp(name: string | undefined, useNpm: boolean, logLevel: Lo
                         })
                         .then(() => {
                             subscriber.next(`Created index`);
+                            return fs.writeFile(rootPathScript('index-worker'), indexWorkerSource);
+                        })
+                        .then(() => {
+                            subscriber.next(`Created worker`);
                             if (usingTs) {
                                 return fs.copyFile(
                                     assetPath('tsconfig.template.json'),

+ 7 - 4
packages/create/src/gather-user-responses.ts

@@ -94,9 +94,10 @@ export async function gatherUserResponses(root: string): Promise<UserResponses>
         process.exit(0);
     }
 
-    const { indexSource, configSource } = await generateSources(root, answers);
+    const { indexSource, indexWorkerSource, configSource } = await generateSources(root, answers);
     return {
         indexSource,
+        indexWorkerSource,
         configSource,
         usingTs: answers.language === 'ts',
         dbType: answers.dbType,
@@ -105,9 +106,9 @@ export async function gatherUserResponses(root: string): Promise<UserResponses>
 }
 
 /**
- * Create the server index and config source code based on the options specified by the CLI prompts.
+ * Create the server index, worker and config source code based on the options specified by the CLI prompts.
  */
-async function generateSources(root: string, answers: any): Promise<{ indexSource: string; configSource: string; }> {
+async function generateSources(root: string, answers: any): Promise<{ indexSource: string; indexWorkerSource: string; configSource: string; }> {
     const assetPath = (fileName: string) => path.join(__dirname, '../assets', fileName);
 
     const templateContext = {
@@ -124,7 +125,9 @@ async function generateSources(root: string, answers: any): Promise<{ indexSourc
     const configSource = Handlebars.compile(configTemplate)(templateContext);
     const indexTemplate = await fs.readFile(assetPath('index.hbs'), 'utf-8');
     const indexSource = Handlebars.compile(indexTemplate)(templateContext);
-    return { indexSource, configSource };
+    const indexWorkerTemplate = await fs.readFile(assetPath('index-worker.hbs'), 'utf-8');
+    const indexWorkerSource = Handlebars.compile(indexWorkerTemplate)(templateContext);
+    return { indexSource, indexWorkerSource, configSource };
 }
 
 function defaultDBPort(dbType: DbType): number {

+ 4 - 1
packages/create/src/helpers.ts

@@ -229,7 +229,10 @@ export function getDependencies(usingTs: boolean, dbType: DbType): { dependencie
         '@vendure/admin-ui-plugin',
         dbDriverPackage(dbType),
     ];
-    const devDependencies = usingTs ? ['ts-node'] : [];
+    const devDependencies = ['concurrently'];
+    if (usingTs) {
+        devDependencies.push('ts-node');
+    }
 
     return {dependencies, devDependencies};
 }

+ 1 - 0
packages/create/src/types.ts

@@ -5,6 +5,7 @@ export interface UserResponses {
     dbType: DbType;
     populateProducts: boolean;
     indexSource: string;
+    indexWorkerSource: string;
     configSource: string;
 }
 

+ 7 - 0
packages/create/templates/index-worker.hbs

@@ -0,0 +1,7 @@
+{{#if isTs }}import { bootstrapWorker } from '@vendure/core';{{else}}const { bootstrapWorker } = require('@vendure/core');{{/if}}
+{{#if isTs }}import { config } from './vendure-config';{{else}}const { config } = require('./vendure-config');{{/if}}
+
+bootstrapWorker(config).catch(err => {
+    // tslint:disable-next-line:no-console
+    console.log(err);
+});