Skip to content

Commit

Permalink
Merge pull request #35 from melfore/33-create-report
Browse files Browse the repository at this point in the history
Create Report
  • Loading branch information
CrisGrud authored Dec 15, 2023
2 parents ff3e5cd + 0dcba75 commit c54ef89
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
git commit -m "Release Ops" -a
- name: Increase Version
run: npm version major
run: npm version patch

- name: Push Updates
uses: ad-m/github-push-action@master
Expand Down
93 changes: 83 additions & 10 deletions src/cli/i18n-msg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from "fs";
import path from "path";
import { EOL } from "os";
import config from "../../config";
import { emitWarning } from "process";

export const CLI_NAME = "i18nMsg";

Expand Down Expand Up @@ -45,9 +46,9 @@ const AUTOGENERATED_HEADER = `// autogenerated with "i18n-msg"
//
// I/O files
//
//const inputFile1 = path.join(process.cwd(),"./test/resources/prova/*");
const inputFile = path.join(process.cwd(), config.get(CLI_NAME).input);
const mal = require(inputFile); // FIXME

const outMsgDir = path.join(process.cwd(), config.get(CLI_NAME).outMsg);
const outTsFile = path.join(process.cwd(), config.get(CLI_NAME).outTs);
const outTempDir = path.join(process.cwd(), config.get(CLI_NAME).outTemp);
Expand Down Expand Up @@ -76,10 +77,12 @@ const writeObjJson = (filename: string, obj: object) => {
}

if (data) {
fs.writeFile(filename, data, (err) => {
if (err) console.log(err);
try {
fs.writeFileSync(filename, data);
console.log(`Successfully written to file "${filename}"`);
});
} catch (err) {
console.log(err);
}
}
};

Expand All @@ -95,10 +98,12 @@ const writeTs = (filename: string, messages: Message[]) => {
return result + `export const ${tsName(msg.code)} = "${msg.code}";${EOL}`;
}, AUTOGENERATED_HEADER);

fs.writeFile(filename, output, (err) => {
if (err) console.log(err);
try {
fs.writeFileSync(filename, output);
console.log(`Successfully written to file "${filename}"`);
});
} catch (err) {
console.log(err);
}
};

const writeKeys = (key: string, translations: Translation[], output: OutputMessagesAllLangs) => {
Expand Down Expand Up @@ -172,21 +177,89 @@ const writeNamespacesFile = (namespaces: MessagesNamespace[]) => {
});
};

const allLanguagesReport = (data: Message[], languages: string[])=>{
data.forEach((value)=>{
value.tr.forEach((i)=>{
languages.push(i.l)
})
if(value.overrides){
value.overrides.forEach((i)=>{
i.tr.forEach((v)=>{
languages.push(v.l)
})
})
}
})
}

const report = (data: Message[], languages:string[])=>{
data.forEach((value)=>{
let valueLanguages: string[] = []
let valueLanguagesOverrites: string[] = []
value.tr.forEach((i)=>{
valueLanguages.push(i.l)
})
value.overrides?.forEach((i)=>{
i.tr.forEach((value)=>{
valueLanguagesOverrites.push(value.l)
})
const differentArrOverrides = languages.filter(x => !valueLanguagesOverrites.includes(x))
if(differentArrOverrides[0]){
console.log(" Code: ",("\x1b[31m" + value.code + "\x1b[0m")," Customer: ",("\x1b[31m" + i.customer + "\x1b[0m")," is missing the following languages: ", differentArrOverrides)
}
valueLanguagesOverrites = []
})
const differentArr = languages.filter(x => !valueLanguages.includes(x))
if(differentArr[0]){
console.log(" Code: ",("\x1b[31m" + value.code + "\x1b[0m")," is missing the following languages: ", differentArr)
}

})
}

const getAllLanguagesUnique = (languages: string[])=>{
return languages.filter((value, index)=>languages.indexOf(value)===index)
}

const multiFileReport = (languages:string[], namespaces:MessagesNamespace[])=>{
console.log()
console.log("-------REPORT-------")
const uniqueLang = getAllLanguagesUnique(languages)
for (const [,messages] of namespaces) {
report(messages,uniqueLang)
}
console.log("All languages: ", uniqueLang)
}

const singleFileReport = (message:Message[],languages:string[])=>{
console.log()
console.log("-----REPORT-------")
const uniqueLang = getAllLanguagesUnique(languages)
report(message,uniqueLang)
console.log("All languages: ", uniqueLang)
}

const checkNamespaces = () => {
fs.mkdirSync(outTempDir, { recursive: true });
fs.mkdirSync(outMsgDir, { recursive: true });
fs.mkdirSync(outTsDir, { recursive: true });

const namespaces: MessagesNamespace[] = mal.namespaces ? Object.entries(mal.namespaces) : [];

const languages: string[] = []
if (namespaces.length > 0) {
namespaces.forEach(([ns, messages]) => {
for (const [ns, messages] of namespaces) {
allLanguagesReport(messages,languages)
splitMessages(messages, ns);
});
}
multiFileReport(languages,namespaces)
writeNamespacesFile(namespaces);
} else {
allLanguagesReport(mal.messages,languages)
splitMessages(mal.messages);
singleFileReport(mal.messages, languages)
}


};

// main
Expand Down

0 comments on commit c54ef89

Please sign in to comment.