Last active
October 11, 2023 11:56
-
-
Save yin1999/ce0bd26fa03000deb197720039f7ee71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from "node:fs/promises"; | |
import * as path from "node:path"; | |
const locale = "ru"; | |
const specHead = /^## (?:Specifications|Specification|Характеристики|Спецификации|Спецификация)$/m; | |
const specTail = /^##/m; // next heading | |
const specName = /\{\{\s*SpecName/i; | |
const Specifications = /\{\{\s*Specifications/i; | |
/** | |
* | |
* @param {string} fpath | |
*/ | |
async function checkEn(fpath) { | |
// ignore conflicting files (they are conflicting file, just replace the deprecated spec macro) | |
if (fpath.includes(`${path.sep}conflicting${path.sep}`) || fpath.includes(`${path.sep}orphaned${path.sep}`)) { | |
return true; | |
} | |
const fpathEn = path.join("..", "content", fpath.replace(`${path.sep}${locale}${path.sep}`, `${path.sep}en-us${path.sep}`)); | |
const content = await fs.readFile(fpathEn, "utf-8"); | |
return content.match(Specifications) !== null; | |
} | |
/** | |
* | |
* @param {string} fpath | |
*/ | |
async function processFile(fpath) { | |
const content = await fs.readFile(fpath, "utf-8"); | |
// we need to replace the spec | |
if (!content.match(specName)) { | |
return; | |
} | |
const head = content.match(specHead); | |
if (!head) { | |
return; | |
} | |
// split the content between the spec head and the next heading/end of file | |
const restContent = content.substring(head.index + head[0].length); | |
let end = restContent.match(specTail); | |
if (!end) { | |
end = restContent.length; | |
} else { | |
end = end.index; | |
} | |
const spec = restContent.substring(0, end-1).trim(); | |
// check if the spec is already in the file | |
if (!spec.match(specName)) { | |
return; | |
} | |
// check if the spec is already in the en-us file | |
if (!await checkEn(fpath)) { | |
return; | |
} | |
const newContent = content.replace(spec, "{{Specifications}}"); | |
// check if there are more than one Specifications macro | |
if ((newContent.match(Specifications) ?? []).length > 1) { | |
console.log(`More than one Specifications macro in ${fpath}`); | |
return; | |
} | |
// write the new content | |
return fs.writeFile(fpath, newContent); | |
} | |
/** | |
* | |
* @param {string} fpath | |
*/ | |
async function worker(fpath) { | |
const files = await fs.readdir(fpath, { withFileTypes: true }); | |
for (const file of files) { | |
if (file.isDirectory()) { | |
if (file.name === "orphaned") { | |
continue; | |
} | |
await worker(path.join(fpath, file.name)); | |
} else if (file.name.endsWith(".md")) { | |
await processFile(path.join(fpath, file.name)); | |
} | |
} | |
} | |
await worker(`files${path.sep}${locale}${path.sep}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment