@ices/locale-webpack-plugin
Version:
webpack plugin for parsing locale files
139 lines • 4.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeFileSync = exports.getEntries = exports.normalizeLocale = exports.isSamePath = exports.normalizePath = exports.getIdentifierMaker = exports.capitalize = exports.escapeRegExpCharacters = exports.getSelfContext = void 0;
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
/**
* 获取当前模块的根路径。
*/
function getSelfContext() {
const cwd = fs_1.default.realpathSync(process.cwd());
let file = __filename;
while (!fs_1.default.existsSync(path_1.default.join((file = path_1.default.dirname(file)), 'package.json'))) {
if (file === cwd || path_1.default.basename(file) === 'node_modules') {
file = '';
break;
}
}
if (file && path_1.default.dirname(file) !== file) {
return file;
}
try {
if (require(path_1.default.join(file, 'package.json')).name === '@ices/locale-webpack-plugin') {
return file;
}
}
catch (e) { }
return '';
}
exports.getSelfContext = getSelfContext;
/**
* 转义处理正则元字符。
* @param str 待处理的字符串。
*/
function escapeRegExpCharacters(str) {
return str.replace(/[|/\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
}
exports.escapeRegExpCharacters = escapeRegExpCharacters;
/**
* 首字母大写。
* @param str 待处理的字符串。
*/
function capitalize(str) {
return str[0].toUpperCase() + str.substring(1);
}
exports.capitalize = capitalize;
/**
* 获取一个变量标识符生成器。
* @param prefix 标识符前缀。
* @param counter 用于同名变量计数的容器。
*/
function getIdentifierMaker(prefix = '', counter = {}) {
return (namespace = 'space') => {
const identifier = `${prefix}${capitalize(namespace !== '/' ? namespace.replace(/[/\\](.)/g, ($0, $1) => $1.toUpperCase()) : 'glob')}}`.replace(/[^_a-z$\d]/gi, '') || 'ident';
const count = counter[identifier] || 0;
counter[identifier] = count + 1;
return `${identifier}${count || ''}`;
};
}
exports.getIdentifierMaker = getIdentifierMaker;
/**
* 格式化路径。
* @param filepath 文件路径
* @param base 相对于该路径
*/
function normalizePath(filepath, base) {
if (base) {
filepath = path_1.default.relative(base, filepath);
}
filepath = filepath.replace(/\\/g, '/');
if (base && filepath && !path_1.default.isAbsolute(filepath) && !filepath.startsWith('.')) {
filepath = './' + filepath;
}
return filepath || './';
}
exports.normalizePath = normalizePath;
/**
* 判断两个路径是不是指向同一个文件
* @param a a路径
* @param b b路径
* @param base 相对于该路径
*/
function isSamePath(a, b, base = process.cwd()) {
a = !path_1.default.isAbsolute(a) ? path_1.default.join(base, a) : path_1.default.normalize(a);
b = !path_1.default.isAbsolute(b) ? path_1.default.join(base, b) : path_1.default.normalize(b);
return a.replace(/[/\\]+$/, '') === b.replace(/[/\\]+$/, '');
}
exports.isSamePath = isSamePath;
function getCharString(str) {
return /^[a-z]+$/i.test(str) ? str : '';
}
/**
* 获取格式化后的语言区域。
* @param locale 需要格式化的区域语言代码字符串。
* @return [lang-AREA, lang, AREA]
*/
function normalizeLocale(locale) {
if (typeof locale !== 'string') {
locale = '';
}
const [langArea] = locale.split('.');
const [lang, area = ''] = langArea.split(/[-_]/);
const lowerLang = getCharString(lang.toLowerCase());
const upperArea = getCharString(area.toUpperCase());
return [`${lowerLang}${upperArea ? `-${upperArea}` : ''}`, lowerLang, upperArea];
}
exports.normalizeLocale = normalizeLocale;
/**
* 获取entries
* @param obj
*/
function getEntries(obj) {
if (obj === null || typeof obj !== 'object') {
return [];
}
return Object.entries(obj);
}
exports.getEntries = getEntries;
// 获取指定路径上不存在的目录
function getUnExistsDirs(file) {
const unExistsDirs = [];
while (!fs_1.default.existsSync((file = path_1.default.dirname(file)))) {
unExistsDirs.unshift(file);
}
return unExistsDirs;
}
/**
* 同步写入文件。
* @param filePath
* @param content
*/
function writeFileSync(filePath, content) {
for (const dir of getUnExistsDirs(filePath)) {
fs_1.default.mkdirSync(dir);
}
fs_1.default.writeFileSync(filePath, content);
}
exports.writeFileSync = writeFileSync;
//# sourceMappingURL=utils.js.map