2021-11-15 02:19:43 -08:00
|
|
|
import { join, dirname } from 'path';
|
2021-11-19 03:22:01 -08:00
|
|
|
import { readdir } from 'fs/promises';
|
2023-01-27 05:56:56 -08:00
|
|
|
import type { Dirent } from 'fs';
|
2021-11-15 02:19:43 -08:00
|
|
|
|
2022-01-07 13:02:21 -08:00
|
|
|
const ALLOWED_VERSIONED_DIRNAME_LENGTH = [2, 3]; // e.g. v1, v10
|
2021-11-19 03:22:01 -08:00
|
|
|
|
|
|
|
function isVersionedDirname(dirent: Dirent) {
|
|
|
|
if (!dirent.isDirectory()) return false;
|
|
|
|
|
|
|
|
return (
|
|
|
|
ALLOWED_VERSIONED_DIRNAME_LENGTH.includes(dirent.name.length) &&
|
|
|
|
dirent.name.toLowerCase().startsWith('v')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getMaxVersion(from: string) {
|
|
|
|
const entries = await readdir(from, { withFileTypes: true });
|
|
|
|
|
|
|
|
const dirnames = entries.reduce<string[]>((acc, cur) => {
|
|
|
|
if (isVersionedDirname(cur)) acc.push(cur.name);
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (!dirnames.length) return null;
|
|
|
|
|
|
|
|
return Math.max(...dirnames.map((d) => parseInt(d.charAt(1), 10)));
|
|
|
|
}
|
|
|
|
|
2022-01-07 13:02:21 -08:00
|
|
|
/**
|
|
|
|
* Get the full path to a node translation file in `/dist`.
|
|
|
|
*/
|
|
|
|
export async function getNodeTranslationPath({
|
|
|
|
nodeSourcePath,
|
|
|
|
longNodeType,
|
|
|
|
locale,
|
|
|
|
}: {
|
|
|
|
nodeSourcePath: string;
|
|
|
|
longNodeType: string;
|
|
|
|
locale: string;
|
|
|
|
}): Promise<string> {
|
2021-11-19 03:22:01 -08:00
|
|
|
const nodeDir = dirname(nodeSourcePath);
|
|
|
|
const maxVersion = await getMaxVersion(nodeDir);
|
2022-01-07 13:02:21 -08:00
|
|
|
const nodeType = longNodeType.replace('n8n-nodes-base.', '');
|
2021-11-19 03:22:01 -08:00
|
|
|
|
|
|
|
return maxVersion
|
2022-01-07 13:02:21 -08:00
|
|
|
? join(nodeDir, `v${maxVersion}`, 'translations', locale, `${nodeType}.json`)
|
|
|
|
: join(nodeDir, 'translations', locale, `${nodeType}.json`);
|
|
|
|
}
|