This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 336
/
mage2vs.js
304 lines (271 loc) · 9.43 KB
/
mage2vs.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const program = require('commander')
const config = require('config')
const spawn = require('child_process').spawn
function multiStoreConfig(apiConfig, storeCode) {
let confCopy = Object.assign({}, apiConfig)
if (storeCode && config.availableStores.indexOf(storeCode) >= 0)
{
if (config.magento2['api_' + storeCode]) {
confCopy = Object.assign({}, config.magento2['api_' + storeCode]) // we're to use the specific api configuration - maybe even separate magento instance
}
confCopy.url = confCopy.url + '/' + storeCode
} else {
if (storeCode) {
console.error('Unavailable store code', storeCode)
}
}
return confCopy
}
function getMagentoDefaultConfig(storeCode) {
const apiConfig = multiStoreConfig(config.magento2.api, storeCode)
return {
TIME_TO_EXIT: 2000,
PRODUCTS_SPECIAL_PRICES: true,
SKIP_REVIEWS: false,
SKIP_CATEGORIES: false,
SKIP_PRODUCTCATEGORIES: false,
SKIP_ATTRIBUTES: false,
SKIP_TAXRULE: false,
SKIP_PRODUCTS: false,
PRODUCTS_EXCLUDE_DISABLED: config.catalog.excludeDisabledProducts,
MAGENTO_CONSUMER_KEY: apiConfig.consumerKey,
MAGENTO_CONSUMER_SECRET: apiConfig.consumerSecret,
MAGENTO_ACCESS_TOKEN: apiConfig.accessToken,
MAGENTO_ACCESS_TOKEN_SECRET: apiConfig.accessTokenSecret,
MAGENTO_URL: apiConfig.url,
REDIS_HOST: config.redis.host,
REDIS_PORT: config.redis.port,
INDEX_NAME: config.elasticsearch.indices[0],
DATABASE_URL: `${config.elasticsearch.protocol}://${config.elasticsearch.host}:${config.elasticsearch.port}`
}
}
function exec(cmd, args, opts) {
return new Promise((resolve, reject) => {
let child = spawn(cmd, args, opts)
child.stdout.on('data', (data) => {
console.log(data.toString('utf8'));
});
child.stderr.on('data', (data) => {
console.log(data.toString('utf8'));
});
child.on('close', (code) => {
resolve(code)
});
child.on('error', (error) => {
console.error(error)
reject(error)
});
})
}
program
.command('productsdelta')
.option('--store-code <storeCode>', 'storeCode in multistore setup', null)
.option('--adapter <adapter>', 'name of the adapter', 'magento')
.option('--partitions <partitions>', 'number of partitions', 1)
.option('--partitionSize <partitionSize>', 'size of the partitions', 200)
.option('--initQueue <initQueue>', 'use the queue', true)
.option('--skus <skus>', 'comma delimited list of SKUs to fetch fresh informations from', '')
.option('--removeNonExistent <removeNonExistent>', 'remove non existent products', false)
.action((cmd) => {
let magentoConfig = getMagentoDefaultConfig(cmd.storeCode)
magentoConfig.MAGENTO_STORE_ID = 1
magentoConfig.INDEX_META_PATH = '.lastIndex.json'
if (cmd.storeCode) {
const storeView = config.storeViews[cmd.storeCode]
if (!storeView) {
console.error('Wrong storeCode provided - no such store in the config.storeViews[storeCode]', cmd.storeCode)
process.exit(-1)
} else {
magentoConfig.INDEX_NAME = storeView.elasticsearch.index
magentoConfig.INDEX_META_PATH = '.lastIndex-' + cmd.storeCode + '.json'
magentoConfig.MAGENTO_STORE_ID = storeView.storeId
}
}
const env = Object.assign({}, magentoConfig, process.env) // use process env as well
console.log('=== Delta indexer is about to start ===')
exec('node', [
'--harmony',
'node_modules/mage2vuestorefront/src/cli.js',
'productsdelta',
'--adapter=' + cmd.adapter,
'--partitions=' + cmd.partitions,
'--partitionSize=' + cmd.partitionSize,
'--initQueue=' + cmd.initQueue,
'--skus=' + cmd.skus,
'--removeNonExistent=' + cmd.removeNonExistent
], { env: env, shell: true }).then((res) => {
})
})
program
.command('import')
.option('--store-code <storeCode>', 'storeCode in multistore setup', null)
.option('--skip-reviews <skipReviews>', 'skip import of reviews', false)
.option('--skip-categories <skipCategories>', 'skip import of categories', false)
.option('--skip-productcategories <skipProductcategories>', 'skip import of productcategories', false)
.option('--skip-attributes <skipAttributes>', 'skip import of attributes', false)
.option('--skip-taxrule <skipTaxrule>', 'skip import of taxrule', false)
.option('--skip-products <skipProducts>', 'skip import of products', false)
.action((cmd) => {
let magentoConfig = getMagentoDefaultConfig(cmd.storeCode)
if (cmd.storeCode) {
const storeView = config.storeViews[cmd.storeCode]
if (!storeView) {
console.error('Wrong storeCode provided - no such store in the config.storeViews[storeCode]', cmd.storeCode)
process.exit(-1)
} else {
magentoConfig.INDEX_NAME = storeView.elasticsearch.index;
magentoConfig.MAGENTO_STORE_ID = storeView.storeId;
}
}
if (cmd.skipReviews) {
magentoConfig.SKIP_REVIEWS = true;
}
if (cmd.skipCategories) {
magentoConfig.SKIP_CATEGORIES = true;
}
if (cmd.skipProductcategories) {
magentoConfig.SKIP_PRODUCTCATEGORIES = true;
}
if (cmd.skipAttributes) {
magentoConfig.SKIP_ATTRIBUTES = true;
}
if (cmd.skipTaxrule) {
magentoConfig.SKIP_TAXRULE = true;
}
if (cmd.skipProducts) {
magentoConfig.SKIP_PRODUCTS = true;
}
const env = Object.assign({}, magentoConfig, process.env) // use process env as well
console.log('=== The mage2vuestorefront full reindex is about to start. Using the following Magento2 config ===', magentoConfig)
let createDbPromise = function() {
console.log(' == CREATING NEW DATABASE ==')
return exec('node', [
'scripts/db.js',
'new',
`--indexName=${env.INDEX_NAME}`
], { env: env, shell: true })
}
let importReviewsPromise = function() {
if (magentoConfig.SKIP_REVIEWS ) {
return Promise.resolve();
}
else {
console.log(' == REVIEWS IMPORTER ==');
return exec('node', [
'--harmony',
'node_modules/mage2vuestorefront/src/cli.js',
'reviews'
], {env: env, shell: true})
}
}
let importCategoriesPromise = function() {
if (magentoConfig.SKIP_CATEGORIES ) {
return Promise.resolve();
}
else {
console.log(' == CATEGORIES IMPORTER ==');
return exec('node', [
'--harmony',
'node_modules/mage2vuestorefront/src/cli.js',
'categories',
'--removeNonExistent=true',
'--extendedCategories=true'
], { env: env, shell: true })
}
}
let importProductcategoriesPromise = function() {
if (magentoConfig.SKIP_PRODUCTCATEGORIES ) {
return Promise.resolve();
}
else {
console.log(' == PRODUCT-CATEGORIES IMPORTER ==');
return exec('node', [
'--harmony',
'node_modules/mage2vuestorefront/src/cli.js',
'productcategories'
], { env: env, shell: true })
}
}
let importAttributesPromise = function() {
if (magentoConfig.SKIP_ATTRIBUTES ) {
return Promise.resolve();
}
else {
console.log(' == ATTRIBUTES IMPORTER ==');
return exec('node', [
'--harmony',
'node_modules/mage2vuestorefront/src/cli.js',
'attributes',
'--removeNonExistent=true'
], { env: env, shell: true })
}
}
let importTaxrulePromise = function() {
if (magentoConfig.SKIP_TAXRULE ) {
return Promise.resolve();
}
else {
console.log(' == TAXRULE IMPORTER ==');
return exec('node', [
'--harmony',
'node_modules/mage2vuestorefront/src/cli.js',
'taxrule',
'--removeNonExistent=true'
], { env: env, shell: true })
}
}
let importProductsPromise = function() {
if (magentoConfig.SKIP_PRODUCTS ) {
return Promise.resolve();
}
else {
console.log(' == PRODUCTS IMPORTER ==');
return exec('node', [
'--harmony',
'node_modules/mage2vuestorefront/src/cli.js',
'products',
'--removeNonExistent=true',
'--partitions=1'
], { env: env, shell: true })
}
}
let reindexPromise = function() {
console.log(' == REINDEXING DATABASE ==')
return exec('node', [
'scripts/db.js',
'rebuild',
`--indexName=${env.INDEX_NAME}`
], {env: env, shell: true})
}
createDbPromise().then( () => {
importReviewsPromise().then( () => {
importCategoriesPromise().then( () => {
importProductcategoriesPromise().then( () => {
importAttributesPromise().then(() => {
importTaxrulePromise().then(() => {
importProductsPromise().then (() => {
reindexPromise().then( () => {
console.log('Done! Bye Bye!')
process.exit(0)
})
})
})
})
})
})
})
})
});
program
.on('command:*', () => {
console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
process.exit(1);
});
program
.parse(process.argv)
process.on('unhandledRejection', (reason, p) => {
console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason)
})
process.on('uncaughtException', function(exception) {
console.log(exception)
})