Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@contentstack/cli-utilities": "^1.7.1",
"@contentstack/json-rte-serializer": "^2.0.7",
"@contentstack/marketplace-sdk": "^1.2.4",
"axios": "^1.7.8",
"axios": "^1.8.2",
"chokidar": "^3.6.0",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
Expand Down
51 changes: 33 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
"validate-branch-name": "^1.3.0",
"xml2js":"^0.4.14"
"xml2js": "^0.5.0"
},
"husky": {
"hooks": {}
Expand Down
7 changes: 5 additions & 2 deletions upload-api/migration-wordpress/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable @typescript-eslint/no-var-requires */

const extractContentTypes = require('./libs/content_types.js');
const contentTypeMaker = require('./libs/contenttypemapper.js');
const extractLocale = require('./libs/extractLocale.js')

module.exports = {
extractContentTypes, //
contentTypeMaker //
extractContentTypes,
contentTypeMaker,
extractLocale
};
42 changes: 42 additions & 0 deletions upload-api/migration-wordpress/libs/extractLocale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');

/**
* Extracts unique languages/locales from a WordPress exported JSON file.
*
* @param {string} path - The file path to the WordPress JSON export.
* @returns {string[]} - An array of unique language codes found in the JSON data.
* @throws {Error} - Throws an error if the file cannot be read or parsed.
*/
const extractLocale = (path) => {
try {
const rawData = fs.readFileSync(path, 'utf8');
const jsonData = JSON.parse(rawData);
const uniqueLanguages = new Set();

// Extract global language (if exists)
if (jsonData.rss?.channel?.language) {
uniqueLanguages.add(jsonData.rss.channel.language);
}

// Extract entry-level languages (if available)
const items = jsonData?.rss?.channel?.item || [];
items.forEach((item) => {
if (item['wp:postmeta']) {
const postMeta = Array.isArray(item['wp:postmeta']) ? item['wp:postmeta'] : [item['wp:postmeta']];
postMeta.forEach((meta) => {
if (meta['wp:meta_key']?.toLowerCase() === 'language' && meta['wp:meta_value']) {
uniqueLanguages.add(meta['wp:meta_value']);
}
});
}
});

return [...uniqueLanguages];
} catch (err) {
throw new Error(`Error reading JSON file: ${err.message}`);
}
};


module.exports = extractLocale;
9 changes: 4 additions & 5 deletions upload-api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion upload-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@aws-sdk/client-s3": "^3.529.0",
"@contentstack/cli-utilities": "^1.5.12",
"@typescript-eslint/parser": "^7.7.1",
"axios": "^1.7.8",
"axios": "^1.8.2",
"chalk": "^4.1.2",
"cheerio": "^1.0.0-rc.12",
"cors": "^2.8.5",
Expand Down
29 changes: 27 additions & 2 deletions upload-api/src/controllers/wordpress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import axios from "axios";
import logger from "../../utils/logger";
import { HTTP_CODES, HTTP_TEXTS } from "../../constants";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { extractContentTypes, contentTypeMaker } = require('migration-wordpress')
const { extractContentTypes, contentTypeMaker, extractLocale } = require('migration-wordpress')



const createWordpressMapper = async (filePath: string = "", projectId: string | string[], app_token: string | string[], affix: string | string[], config: object) => {
try {

const localeData = await extractLocale(filePath);

await extractContentTypes(affix);
const contentTypeData = await contentTypeMaker(affix)
if(contentTypeData){
Expand All @@ -27,7 +32,27 @@ const createWordpressMapper = async (filePath: string = "", projectId: string |
data: JSON.stringify(fieldMapping),
};
const response = await axios.request(config)
// console.log(response);

const mapperConfig = {
method: 'post',
maxBodyLength: Infinity,
url: `${process.env.NODE_BACKEND_API}/v2/migration/localeMapper/${projectId}`,
headers: {
app_token,
'Content-Type': 'application/json'
},
data: {
locale:Array.from(localeData)
},
};

const mapRes = await axios.request(mapperConfig)
if(mapRes?.status==200){
logger.info('Legacy CMS', {
status: HTTP_CODES?.OK,
message: HTTP_TEXTS?.LOCALE_SAVED,
});
}
}
} catch (err: any) {
console.error("🚀 ~ createWordpressMapper ~ err:", err?.response?.data ?? err)
Expand Down