forked from vendure-ecommerce/real-world-vendure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.ts
78 lines (72 loc) · 2.53 KB
/
populate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* eslint-disable @typescript-eslint/no-var-requires */
import { bootstrap, defaultConfig, mergeConfig, RuntimeVendureConfig } from '@vendure/core';
import { populate } from '@vendure/core/cli';
import { clearAllTables, populateCustomers, SimpleGraphQLClient } from '@vendure/testing';
import { config } from './src/vendure-config';
import path from 'path';
import gql from 'graphql-tag';
import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
const initialData = require('@vendure/create/assets/initial-data.json');
// tslint:disable:no-console
/**
* A CLI script which populates the database with some sample data
*/
if (require.main === module) {
// Running from command line
const populateConfig = mergeConfig(
defaultConfig,
mergeConfig(config, {
authOptions: {
tokenMethod: 'bearer',
requireVerification: false,
},
importExportOptions: {
importAssetsDir: resolveFromCreatePackage('assets/images'),
},
customFields: {},
plugins: config.plugins!.filter(plugin => plugin !== AdminUiPlugin),
}),
);
clearAllTables(populateConfig, true)
.then(() =>
populate(
() => bootstrap(populateConfig),
initialData,
resolveFromCreatePackage('assets/products.csv'),
),
)
.then(async app => {
console.log('populating customers...');
await populateCustomers(10, populateConfig, true);
await populateReview(populateConfig);
return app.close();
})
.then(
() => process.exit(0),
err => {
console.log(err);
process.exit(1);
},
);
}
function resolveFromCreatePackage(target: string): string {
return path.join(path.dirname(require.resolve('@vendure/create')), target);
}
async function populateReview(config: RuntimeVendureConfig) {
const { port, shopApiPath } = config.apiOptions;
const client = new SimpleGraphQLClient(config, `http://localhost:${port}/${shopApiPath}`);
await client.query(gql`
mutation {
submitProductReview(input: {
productId: "1",
summary: "A great laptop!",
body: "The laptop looks great an performance is flawless.",
rating: 5,
authorName: "Randall M",
authorLocation: "Vienna",
}) {
id
}
}
`);
}