@vendure/ngx-translate-extract
Version:
Extract strings from projects using ngx-translate
38 lines (37 loc) • 1.37 kB
JavaScript
import { basename, sep, posix } from 'node:path';
import * as os from 'node:os';
import * as fs from 'node:fs';
import braces from 'braces';
export function normalizeHomeDir(path) {
if (path.substring(0, 1) === '~') {
return `${os.homedir()}/${path.substring(1)}`;
}
return path;
}
export function normalizeFilePath(filePath) {
const cwd = 'process' in globalThis ? process.cwd() : '';
const cwdBaseName = basename(cwd);
if (!filePath.startsWith(cwd)) {
return filePath;
}
return filePath.replace(cwd, cwdBaseName).replaceAll(sep, posix.sep);
}
export function expandPattern(pattern) {
const isWindows = sep === '\\';
const bracesCompatiblePattern = isWindows ? pattern.replaceAll(sep, posix.sep) : pattern;
const output = braces(bracesCompatiblePattern, { expand: true, keepEscaping: true });
return isWindows ? output.map((path) => path.replaceAll(posix.sep, sep)) : output;
}
export function normalizePaths(patterns, defaultPatterns = []) {
return patterns
.map((pattern) => expandPattern(pattern)
.map((path) => {
path = normalizeHomeDir(path);
if (fs.existsSync(path) && fs.statSync(path).isDirectory()) {
return defaultPatterns.map((defaultPattern) => path + defaultPattern);
}
return path;
})
.flat())
.flat();
}