HEX
Server: LiteSpeed
System: Linux s3604.bom1.stableserver.net 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64
User: dmstechonline (1480)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /home/dmstechonline/whatsapp.dmstech.online/node_modules/laravel-vue-i18n/dist/loader.mjs
import fs from 'fs';
import path from 'path';
import { Engine } from 'php-parser';
export const hasPhpTranslations = (folderPath) => {
    folderPath = folderPath.replace(/[\\/]$/, '') + path.sep;
    try {
        const folders = fs
            .readdirSync(folderPath)
            .filter((file) => fs.statSync(folderPath + path.sep + file).isDirectory())
            .sort();
        for (const folder of folders) {
            const lang = {};
            const files = fs.readdirSync(folderPath + path.sep + folder).filter((file) => /\.php$/.test(file));
            if (files.length > 0) {
                return true;
            }
        }
    }
    catch (e) { }
    return false;
};
export const parseAll = (folderPath) => {
    folderPath = folderPath.replace(/[\\/]$/, '') + path.sep;
    if (!fs.existsSync(folderPath)) {
        return [];
    }
    const folders = fs
        .readdirSync(folderPath)
        .filter((file) => fs.statSync(folderPath + path.sep + file).isDirectory())
        .sort();
    const data = [];
    for (const folder of folders) {
        const langFolderPath = folderPath + path.sep + folder;
        const lang = readThroughDir(langFolderPath);
        data.push({
            folder,
            translations: convertToDotsSyntax(lang)
        });
    }
    return data
        .filter(({ translations }) => {
        return Object.keys(translations).length > 0;
    })
        .map(({ folder, translations }) => {
        return {
            name: `php_${folder}.json`,
            translations
        };
    });
};
export const parse = (content) => {
    const arr = new Engine({}).parseCode(content, 'lang').children.filter((child) => child.kind === 'return')[0];
    if (arr?.expr?.kind !== 'array') {
        return {};
    }
    return convertToDotsSyntax(parseItem(arr.expr));
};
const parseItem = (expr) => {
    if (expr.kind === 'string') {
        return expr.value;
    }
    if (expr.kind === 'nullkeyword') {
        return null;
    }
    if (expr.kind === 'array') {
        let items = expr.items.map((item) => parseItem(item));
        if (expr.items.every((item) => item.key !== null)) {
            items = items.reduce((acc, val) => Object.assign({}, acc, val), {});
        }
        return items;
    }
    if (expr.kind === 'bin') {
        return parseItem(expr.left) + parseItem(expr.right);
    }
    if (expr.key) {
        return { [expr.key.value]: parseItem(expr.value) };
    }
    return parseItem(expr.value);
};
const convertToDotsSyntax = (list) => {
    const flatten = (items, context = '') => {
        const data = {};
        if (items === null) {
            return data;
        }
        Object.entries(items).forEach(([key, value]) => {
            if (typeof value === 'string') {
                data[context + key] = value;
                return;
            }
            Object.entries(flatten(value, context + key + '.')).forEach(([itemKey, itemValue]) => {
                data[itemKey] = itemValue;
            });
        });
        return data;
    };
    return flatten(list);
};
export const reset = (folderPath) => {
    const dir = fs.readdirSync(folderPath);
    dir
        .filter((file) => file.match(/^php_/))
        .forEach((file) => {
        fs.unlinkSync(folderPath + file);
    });
};
export const readThroughDir = (dir) => {
    const data = {};
    fs.readdirSync(dir).forEach((file) => {
        const absoluteFile = dir + path.sep + file;
        if (fs.statSync(absoluteFile).isDirectory()) {
            const subFolderFileKey = file.replace(/\.\w+$/, '');
            data[subFolderFileKey] = readThroughDir(absoluteFile);
        }
        else {
            data[file.replace(/\.\w+$/, '')] = parse(fs.readFileSync(absoluteFile).toString());
        }
    });
    return data;
};
export const prepareExtendedParsedLangFiles = (langPaths) => langPaths.reduce((acc, langPath) => [...acc, ...parseAll(langPath)], new Array());
export const generateFiles = (langPath, data) => {
    data = mergeData(data);
    if (!fs.existsSync(langPath)) {
        fs.mkdirSync(langPath);
    }
    data.forEach(({ name, translations }) => {
        fs.writeFileSync(langPath + name, JSON.stringify(translations));
    });
    return data;
};
function mergeData(data) {
    const obj = {};
    data.forEach(({ name, translations }) => {
        if (!obj[name]) {
            obj[name] = {};
        }
        obj[name] = { ...obj[name], ...translations };
    });
    const arr = [];
    Object.entries(obj).forEach(([name, translations]) => {
        arr.push({ name, translations });
    });
    return arr;
}
//# sourceMappingURL=loader.mjs.map