2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

chore: type getLanguageMap

- remove undefined.csv error
This commit is contained in:
18alantom 2022-04-02 04:39:37 +05:30
parent 1c8a70819e
commit 233798ae3c

View File

@ -14,19 +14,25 @@ const { splitCsvLine } = require('../scripts/helpers');
const VALENTINES_DAY = 1644796800000;
async function getLanguageMap(code, isDevelopment = false) {
type Translation = { translation: string; context?: string };
type TranslationMap = Record<string, Translation>;
export async function getLanguageMap(
code: string,
isDevelopment: boolean = false
): Promise<TranslationMap> {
const contents = await getContents(code, isDevelopment);
return getMapFromContents(contents);
}
async function getContents(code, isDevelopment) {
async function getContents(code: string, isDevelopment: boolean) {
if (isDevelopment) {
const filePath = path.resolve('translations', `${code}.csv`);
const contents = await fs.readFile(filePath, { encoding: 'utf-8' });
return ['', contents].join('\n');
}
let contents = await getContentsIfExists();
let contents = await getContentsIfExists(code);
if (contents.length === 0) {
contents = (await fetchAndStoreFile(code)) ?? contents;
} else {
@ -40,31 +46,31 @@ async function getContents(code, isDevelopment) {
return contents;
}
function getMapFromContents(contents) {
contents = contents.split('\n').slice(1);
return contents
.map(splitCsvLine)
function getMapFromContents(contents: string): TranslationMap {
const lines: string[] = contents.split('\n').slice(1);
return lines
.map((l) => splitCsvLine(l) as string[])
.filter((l) => l.length >= 2)
.reduce((acc, l) => {
const key = l[0].slice(1, -1);
const translation = l[1].slice(1, -1);
acc[key] = { translation };
const context = l.slice(2);
if (context.length) {
acc.context = context;
const context = l.slice(2)[0];
if (context?.length) {
acc[key].context = context;
}
return acc;
}, {});
}, {} as TranslationMap);
}
async function getContentsIfExists(code) {
async function getContentsIfExists(code: string): Promise<string> {
const filePath = getFilePath(code);
try {
return await fs.readFile(filePath, { encoding: 'utf-8' });
} catch (err) {
if (err.errno !== -2) {
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') {
throw err;
}
@ -72,12 +78,12 @@ async function getContentsIfExists(code) {
}
}
async function fetchAndStoreFile(code, date) {
async function fetchAndStoreFile(code: string, date?: Date) {
let res = await fetch(
`https://api.github.com/repos/frappe/books/contents/translations/${code}.csv`
);
let contents = undefined;
let contents: string | undefined = undefined;
if (res.status === 200) {
const resJson = await res.json();
contents = Buffer.from(resJson.content, 'base64').toString();
@ -96,14 +102,14 @@ async function fetchAndStoreFile(code, date) {
}
if (contents) {
contents = [date.toISOString(), contents].join('\n');
contents = [date!.toISOString(), contents].join('\n');
await storeFile(code, contents);
}
return contents;
}
async function getUpdatedContent(code, contents) {
const [shouldUpdate, date] = await shouldUpdateFile(code, contents);
async function getUpdatedContent(code: string, contents: string) {
const { shouldUpdate, date } = await shouldUpdateFile(code, contents);
if (!shouldUpdate) {
return contents;
}
@ -111,17 +117,17 @@ async function getUpdatedContent(code, contents) {
return await fetchAndStoreFile(code, date);
}
async function shouldUpdateFile(code, contents) {
async function shouldUpdateFile(code: string, contents: string) {
const date = await getLastUpdated(code);
const oldDate = new Date(contents.split('\n')[0]);
const shouldUpdate = date > oldDate || +oldDate === VALENTINES_DAY;
return [shouldUpdate, date];
return { shouldUpdate, date };
}
async function getLastUpdated(code) {
async function getLastUpdated(code: string): Promise<Date> {
const url = `https://api.github.com/repos/frappe/books/commits?path=translations%2F${code}.csv&page=1&per_page=1`;
const resJson = await fetch(url).then((res) => res.json());
const resJson = await fetch(url).then((res: Response) => res.json());
try {
return new Date(resJson[0].commit.author.date);
@ -130,19 +136,17 @@ async function getLastUpdated(code) {
}
}
function getFilePath(code) {
function getFilePath(code: string) {
return path.resolve(process.resourcesPath, 'translations', `${code}.csv`);
}
function throwCouldNotFetchFile(code) {
function throwCouldNotFetchFile(code: string) {
throw new Error(`Could not fetch translations for '${code}'.`);
}
async function storeFile(code, contents) {
async function storeFile(code: string, contents: string) {
const filePath = getFilePath(code);
const dirname = path.dirname(filePath);
await fs.mkdir(dirname, { recursive: true });
await fs.writeFile(filePath, contents, { encoding: 'utf-8' });
}
module.exports = { getLanguageMap };