Explorar o código

docs: Add docs on stand-alone scripts

Michael Bromley %!s(int64=4) %!d(string=hai) anos
pai
achega
4a0b8d509a

+ 5 - 0
docs/content/developer-guide/importing-product-data.md

@@ -215,3 +215,8 @@ populate(
   },
 );
 ```
+
+{{< alert >}}
+If you require more control over how your data is being imported - for example if you also need to import data into custom entities - you can create your own CLI script to do this: see [Stand-Alone CLI Scripts]({{< relref "stand-alone-scripts" >}}).
+{{< /alert >}} 
+

+ 54 - 0
docs/content/developer-guide/stand-alone-scripts.md

@@ -0,0 +1,54 @@
+---
+title: "Stand-alone CLI Scripts"
+showtoc: true
+---
+
+# Stand-alone CLI Scripts
+
+It is possible to create stand-alone scripts that can be run from the command-line by using the [bootstrapWorker function]({{< relref "bootstrap-worker" >}}). This can be useful for a variety of use-cases such as running cron jobs or importing data.
+
+```TypeScript
+// run-sync.ts
+
+import { bootstrapWorker, Logger } from '@vendure/core';
+
+import { config } from './vendure-config';
+import { DataSyncPlugin, DataSyncService } from './plugins/data-sync';
+
+const loggerCtx = 'DataSync script';
+
+runDataSync()
+  .then(() => process.exit(0))
+  .catch(() => process.exit(1));
+
+async function runDataSync() {
+  // This will bootstrap an instance of the Vendure Worker
+  const { app } = await bootstrapWorker({
+    ...config,
+    plugins: [
+      ...config.plugins,
+      DataSyncPlugin,
+    ]
+  });
+  
+  // Using `app.get()` we can grab an instance of _any_ provider defined in the
+  // Vendure core as well as by our plugins.
+  const dataSyncService = app.get(DataSyncService);
+  
+  Logger.info('Syncing data...', loggerCtx);
+  
+  try {
+    const result = await dataSyncService.runSync();
+    Logger.info(`Completed sync: ${result.count} items processed`, loggerCtx);
+  } catch (e) {
+    Logger.error(e.message, loggerCtx, e.stack);
+    throw e;
+  }
+}
+```
+
+This script can then be run from the command-line:
+
+```shell
+yarn ts-node run-sync.ts
+```