Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 62686bf

Browse files
authored
Merge pull request #180 from DivanteLtd/feature/better-seo-redirects-generation
Better seo redirects generation
2 parents f849e0c + b1b1140 commit 62686bf

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

scripts/seo.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ const config = require('config')
33
const common = require('../migrations/.common')
44
const es = require('../src/lib/elastic')
55

6+
const fs = require('fs')
7+
const path = require('path')
8+
69
program
710
.command('redirects')
811
.option('-i|--indexName <indexName>', 'name of the Elasticsearch index', config.elasticsearch.indices[0])
912
.option('-f|--oldFormat <oldFormat>', 'use the old format', true)
1013
.option('-s|--size <size>', 'size', 10000)
11-
.action((cmd) => {
14+
.option('-d|--dest <dest>', 'dest', './')
15+
.action(async (cmd) => {
1216
if (!cmd.indexName) {
1317
console.error('error: indexName must be specified');
1418
process.exit(1);
@@ -18,24 +22,28 @@ program
1822
console.log('** Please check the nginx map module options on how to use this map format: https://serverfault.com/a/441517')
1923
console.log('** The urls will be mapped to the new VS Url format. Please make sure You have "products.useMagentoUrlKeys=true" in Your vue-storefront/config/local.json')
2024

21-
es.search(common.db, {
22-
index: cmd.indexName,
23-
type: 'product',
24-
size: cmd.size,
25-
body: {}
26-
}).then(function (resp) {
27-
const hits = resp.hits.hits
25+
try {
26+
const redirects = []
27+
28+
await es.search(common.db, {
29+
index: cmd.indexName,
30+
type: 'product',
31+
size: cmd.size,
32+
body: {}
33+
}).then(function (resp) {
34+
const hits = resp.hits.hits
2835

29-
for (const hit of hits) {
30-
const product = hit._source
31-
if (cmd.oldFormat) {
32-
console.log(`/${product.url_key} /p/${decodeURIComponent(product.sku)}/${product.url_key}/${decodeURIComponent(product.sku)};`)
33-
} else {
34-
console.log(`/${product.url_key} /${product.url_key}/${decodeURIComponent(product.sku)};`)
36+
for (const hit of hits) {
37+
const product = hit._source
38+
if (cmd.oldFormat) {
39+
redirects.push(`/${product.url_key} /p/${decodeURIComponent(product.sku)}/${product.url_key}/${decodeURIComponent(product.sku)};`)
40+
} else {
41+
redirects.push(`/${product.url_key} /${product.url_key}/${decodeURIComponent(product.sku)};`)
42+
}
3543
}
36-
}
44+
})
3745

38-
es.search(common.db, {
46+
await es.search(common.db, {
3947
index: cmd.indexName,
4048
type: 'category',
4149
size: cmd.size,
@@ -45,13 +53,22 @@ program
4553
for (const hit of hits) {
4654
const category = hit._source
4755
if (cmd.oldFormat) {
48-
console.log(`/${category.url_path} /c/${category.url_key};`)
56+
redirects.push(`/${category.url_path} /c/${category.url_key};`)
4957
} else {
50-
console.log(`/${category.url_path} /${category.url_key};`)
58+
redirects.push(`/${category.url_path} /${category.url_key};`)
5159
}
5260
}
5361
})
54-
})
62+
63+
fs.writeFileSync(
64+
path.join(path.resolve(cmd.dest), `${cmd.indexName}-redirects`),
65+
redirects.join('\n')
66+
)
67+
process.exit(0)
68+
} catch (error) {
69+
console.error(error)
70+
process.exit(1)
71+
}
5572
})
5673

5774
program

0 commit comments

Comments
 (0)