Skip to content

Commit 40ffd45

Browse files
Merge pull request #517 from contentstack/feature/CMG-545-locale-mapper
feature/CMG-545 locale mapper
2 parents 65827c7 + 8a74880 commit 40ffd45

File tree

9 files changed

+119
-34
lines changed

9 files changed

+119
-34
lines changed

api/package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@contentstack/cli-utilities": "^1.7.1",
3030
"@contentstack/json-rte-serializer": "^2.0.7",
3131
"@contentstack/marketplace-sdk": "^1.2.4",
32-
"axios": "^1.7.8",
32+
"axios": "^1.8.2",
3333
"chokidar": "^3.6.0",
3434
"cors": "^2.8.5",
3535
"dotenv": "^16.3.1",

package-lock.json

Lines changed: 33 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"prettier": "^2.4.1",
2727
"rimraf": "^3.0.2",
2828
"validate-branch-name": "^1.3.0",
29-
"xml2js":"^0.4.14"
29+
"xml2js": "^0.5.0"
3030
},
3131
"husky": {
3232
"hooks": {}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
12

23
const extractContentTypes = require('./libs/content_types.js');
34
const contentTypeMaker = require('./libs/contenttypemapper.js');
5+
const extractLocale = require('./libs/extractLocale.js')
46

57
module.exports = {
6-
extractContentTypes, //
7-
contentTypeMaker //
8+
extractContentTypes,
9+
contentTypeMaker,
10+
extractLocale
811
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const fs = require('fs');
3+
4+
/**
5+
* Extracts unique languages/locales from a WordPress exported JSON file.
6+
*
7+
* @param {string} path - The file path to the WordPress JSON export.
8+
* @returns {string[]} - An array of unique language codes found in the JSON data.
9+
* @throws {Error} - Throws an error if the file cannot be read or parsed.
10+
*/
11+
const extractLocale = (path) => {
12+
try {
13+
const rawData = fs.readFileSync(path, 'utf8');
14+
const jsonData = JSON.parse(rawData);
15+
const uniqueLanguages = new Set();
16+
17+
// Extract global language (if exists)
18+
if (jsonData.rss?.channel?.language) {
19+
uniqueLanguages.add(jsonData.rss.channel.language);
20+
}
21+
22+
// Extract entry-level languages (if available)
23+
const items = jsonData?.rss?.channel?.item || [];
24+
items.forEach((item) => {
25+
if (item['wp:postmeta']) {
26+
const postMeta = Array.isArray(item['wp:postmeta']) ? item['wp:postmeta'] : [item['wp:postmeta']];
27+
postMeta.forEach((meta) => {
28+
if (meta['wp:meta_key']?.toLowerCase() === 'language' && meta['wp:meta_value']) {
29+
uniqueLanguages.add(meta['wp:meta_value']);
30+
}
31+
});
32+
}
33+
});
34+
35+
return [...uniqueLanguages];
36+
} catch (err) {
37+
throw new Error(`Error reading JSON file: ${err.message}`);
38+
}
39+
};
40+
41+
42+
module.exports = extractLocale;

upload-api/package-lock.json

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

upload-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@aws-sdk/client-s3": "^3.529.0",
3636
"@contentstack/cli-utilities": "^1.5.12",
3737
"@typescript-eslint/parser": "^7.7.1",
38-
"axios": "^1.7.8",
38+
"axios": "^1.8.2",
3939
"chalk": "^4.1.2",
4040
"cheerio": "^1.0.0-rc.12",
4141
"cors": "^2.8.5",

upload-api/src/controllers/wordpress/index.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import axios from "axios";
22
import logger from "../../utils/logger";
33
import { HTTP_CODES, HTTP_TEXTS } from "../../constants";
44
// eslint-disable-next-line @typescript-eslint/no-var-requires
5-
const { extractContentTypes, contentTypeMaker } = require('migration-wordpress')
5+
const { extractContentTypes, contentTypeMaker, extractLocale } = require('migration-wordpress')
6+
7+
68

79
const createWordpressMapper = async (filePath: string = "", projectId: string | string[], app_token: string | string[], affix: string | string[], config: object) => {
810
try {
11+
12+
const localeData = await extractLocale(filePath);
13+
914
await extractContentTypes(affix);
1015
const contentTypeData = await contentTypeMaker(affix)
1116
if(contentTypeData){
@@ -27,7 +32,27 @@ const createWordpressMapper = async (filePath: string = "", projectId: string |
2732
data: JSON.stringify(fieldMapping),
2833
};
2934
const response = await axios.request(config)
30-
// console.log(response);
35+
36+
const mapperConfig = {
37+
method: 'post',
38+
maxBodyLength: Infinity,
39+
url: `${process.env.NODE_BACKEND_API}/v2/migration/localeMapper/${projectId}`,
40+
headers: {
41+
app_token,
42+
'Content-Type': 'application/json'
43+
},
44+
data: {
45+
locale:Array.from(localeData)
46+
},
47+
};
48+
49+
const mapRes = await axios.request(mapperConfig)
50+
if(mapRes?.status==200){
51+
logger.info('Legacy CMS', {
52+
status: HTTP_CODES?.OK,
53+
message: HTTP_TEXTS?.LOCALE_SAVED,
54+
});
55+
}
3156
}
3257
} catch (err: any) {
3358
console.error("🚀 ~ createWordpressMapper ~ err:", err?.response?.data ?? err)

0 commit comments

Comments
 (0)