Cleaning code, step 1

This commit is contained in:
Pierre
2023-12-05 15:01:48 +01:00
parent 131139515d
commit f5def3da9d
3 changed files with 82 additions and 76 deletions
+48 -43
View File
@@ -21,7 +21,7 @@ const program = commander
(value) => value.split(',')
)
.option('-docPath, --docPath <path_of_target_dir_on_gh_repo>',`The path of the directory to translate, defaults to "docs"`, 'docs')
.option('-docPath, --docPath <path_of_target_dir_on_gh_repo>',`The path of the directory to translate, oftenr is "docs"`, '')
.option('-o, --outputPath <directory_path>',`The directory to output the translated files to, defaults to "./build"`, './build')
@@ -59,15 +59,15 @@ async function run() {
case 'translate':
for (let langCode of options.language) {
console.log("Translating to " + langCode);
await translate.translateDoc(
program.args[1],
program.args[2],
options.docPath,
supportedLanguages[langCode],
options.language,
options.savePath,
loadFile
);
await translate.translateDoc({
repoOwner: program.args[1],
repoName: program.args[2],
repoDocDir: options.docPath,
language : supportedLanguages[langCode],
languageCode: langCode,
savePath: options.savePath,
loadFile: loadFile
});
loadFile=false;
}
@@ -77,15 +77,15 @@ async function run() {
for (let langCode of options.language) {
console.log("Updating translation in " + langCode);
await translate.translateDoc(
program.args[1],
program.args[2],
options.docPath,
supportedLanguages[langCode],
options.language,
options.savePath,
loadFile
);
await translate.translateDoc({
repoOwner: program.args[1],
repoName: program.args[2],
repoDocDir: options.docPath,
language : supportedLanguages[langCode],
languageCode: langCode,
savePath: options.savePath,
loadFile: loadFile
});
loadFile=false;
}
break;
@@ -109,13 +109,15 @@ async function run() {
}
console.log("Building translation Md " + langCode + " to " + options.outputPath);
translate.buildDoc(
program.args[1],
program.args[2],
langCode,
options.savePath,
outPath,
prefixToRemove
translate.buildDoc({
repoOwner: program.args[1],
repoName: program.args[2],
languageCode: langCode,
savePath: options.savePath,
outputPath: outPath,
prefixToRemove: prefixToRemove
}
);
}
break;
@@ -125,15 +127,15 @@ async function run() {
loadFile = true;
for (let langCode of options.language) {
console.log("Translating to " + langCode);
await translate.translateDoc(
program.args[1],
program.args[2],
options.docPath,
supportedLanguages[langCode],
options.language,
options.savePath,
loadFile
);
await translate.translateDoc({
repoOwner: program.args[1],
repoName: program.args[2],
repoDocDir: options.docPath,
language : supportedLanguages[langCode],
languageCode: langCode,
savePath: options.savePath,
loadFile: loadFile
});
loadFile=false;
}
@@ -152,14 +154,17 @@ async function run() {
}
console.log("Building translation Md " + langCode + " to " + options.outputPath);
translate.buildDoc(
program.args[1],
program.args[2],
langCode,
options.savePath,
outPath,
prefixToRemove
);
translate.buildDoc({
repoOwner: program.args[1],
repoName: program.args[2],
languageCode: langCode,
savePath: options.savePath,
outputPath: outPath,
prefixToRemove: prefixToRemove
});
}
break;
}
-7
View File
@@ -1,7 +0,0 @@
function translateDoc(repoName, language, code) {
}
+34 -26
View File
@@ -151,7 +151,7 @@ Guidelines:
Additional Notes:
- it's a computer doc, build mean 'compile', watch mean 'looking at file that change',
- Keep the same structure as the original documentation, and retain ALL the links / images.,
- Keep the same structure as the original documentation, and retain ALL the links / images.
`,
},
{
@@ -231,6 +231,8 @@ Additional Notes:
async function translateFile(file, language, code) {
debugger;
console.log("translating file:", file.name, code);
if (!file.doc) {
file.doc = parseMdStrToTree(file.raw);
}
@@ -354,11 +356,10 @@ async function correctLinkInFile(file, languageCode, docDir) {
async function buildOutputMd(files, languageCode, targetDir, prefixToRemove) {
console.log('Prefix to remove', prefixToRemove)
for (let file of files) {
// check if file is translated in target language
if (!file.doc || file.doc[0][`content_${languageCode}`] === undefined) {
console.log('failed to find translation', file.path);
continue
}
@@ -404,14 +405,24 @@ async function printFiles(files, owner, repoName, repoDocDir) {
}
}
async function translateDoc(owner, repoName, repoDocDir, language, code, savePath, loadFile=true) {
async function translateDoc(options) {
const repoOwner = options.repoOwner;
const repoName = options.repoName;
const repoDocDir = options.repoDocDir;
const language = options.language;
const languageCode = options.languageCode;
const savePath = options.savePath;
const loadFile = options.loadFile || (options.loadFile === undefined) ? true : false; // default to true
console.log(options);
// const files = await listDocFiles(owner, repoName, repoDocDir);
let files = [];
let savepath = `${savePath}/${owner}/${repoName}.json`;
let savepath = `${savePath}/${repoOwner}/${repoName}.json`;
try {
await fs.access(savepath);
files = require(savepath)
console.log("Loading files from save file successfully")
files = require(savepath);
} catch {
// create the save oath
let dirs = savepath.split('/');
@@ -428,28 +439,35 @@ async function translateDoc(owner, repoName, repoDocDir, language, code, savePat
if (loadFile) {
console.log("Loading files from Github");
await listDocFiles(files, owner, repoName, repoDocDir);
await loadFiles(owner, repoName, files);
await listDocFiles(files, repoOwner, repoName, repoDocDir);
await loadFiles(repoOwner, repoName, files);
}
await translateFiles(files, language, code, savepath);
await translateFiles(files, language, languageCode, savepath);
}
async function buildDoc(owner, repoName, code, savePath, outputPath, prefixToRemove) {
async function buildDoc(options) {
const repoOwner = options.repoOwner;
const repoName = options.repoName;
const languageCode = options.languageCode;
const savePath = options.savePath;
const outputPath = options.outputPath;
const prefixToRemove = options.prefixToRemove;
// const files = await listDocFiles(owner, repoName, repoDocDir);
let files = [];
let savepath = `${savePath}/${owner}/${repoName}.json`;
let savepath = `${savePath}/${repoOwner}/${repoName}.json`;
try {
await fs.access(savepath);
files = require(savepath);
// TODO handle legacy save file
console.log("Loading files from save file successfully")
} catch {
throw new Error('No save file found, please run translate first');
}
let outPath = outputPath;
try {
await fs.access(outPath);
@@ -467,19 +485,9 @@ async function buildDoc(owner, repoName, code, savePath, outputPath, prefixToRem
}
}
await buildOutputMd(files, code, outPath, prefixToRemove);
await buildOutputMd(files, languageCode, outputPath, prefixToRemove);
}
// translateDoc('nodejs', 'node', 'doc', 'French', 'fr');
//translateDoc('run-llama', 'llama_index', 'docs', 'French', 'fr')
// translateDoc('run-llama', 'LlamaIndexTS', 'apps/docs/docs', 'Simplified Chinese(zh-Hans)', 'zh-Hans', './save')
// translateDoc('run-llama', 'llama_index', 'docs', 'Simplified Chinese(zh_cn)', 'zh_cn')
// translateDoc('run-llama', 'llama_index', 'docs', 'Italian', 'it', './save')
module.exports =
{
translateDoc: translateDoc,