attranslate
Version:
Text Translator for Websites and Apps
88 lines (87 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.nodeVersionSatisfies = exports.runCommandOrDie = exports.writeUtf8File = exports.readUtf8File = exports.deleteFile = exports.logFatal = exports.getDebugPath = exports.checkNotDir = exports.checkDir = exports.joinDirWithFileName = void 0;
const tslib_1 = require("tslib");
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const path_1 = require("path");
const semver_1 = (0, tslib_1.__importDefault)(require("semver"));
function joinDirWithFileName(dir, fileName) {
checkDir(dir);
return (0, path_1.join)((0, path_1.resolve)(dir), fileName);
}
exports.joinDirWithFileName = joinDirWithFileName;
function isDirectory(path) {
try {
const stat = (0, fs_1.lstatSync)(path);
return stat.isDirectory();
}
catch (e) {
return false;
}
}
function extractHint(hint) {
if (!hint) {
return "";
}
return hint.errorHint + " ";
}
function checkDir(dir, hint) {
checkExists(dir, hint);
if (!isDirectory(dir)) {
logFatal(`${extractHint(hint)}${getDebugPath(dir)} is not a directory.`);
}
}
exports.checkDir = checkDir;
function checkNotDir(path, hint) {
checkExists(path, hint);
if (isDirectory(path)) {
logFatal(`${extractHint(hint)}${getDebugPath(path)} is a directory.`);
}
}
exports.checkNotDir = checkNotDir;
function checkExists(path, hint) {
if (!(0, fs_1.existsSync)(path)) {
logFatal(`${extractHint(hint)}${getDebugPath(path)} does not exist.`);
}
}
function getDebugPath(path) {
return `\'${(0, path_1.resolve)(path)}\'`; // Show an absolute path to users in case of errors.
}
exports.getDebugPath = getDebugPath;
function logFatal(msg) {
console.error(`error: ${msg}`);
return process.exit(1);
}
exports.logFatal = logFatal;
function deleteFile(path) {
checkExists(path);
(0, fs_1.unlinkSync)(path);
console.info(`Deleted ${getDebugPath(path)}`);
}
exports.deleteFile = deleteFile;
function readUtf8File(path) {
checkNotDir(path);
return (0, fs_1.readFileSync)(path, { encoding: "utf8", flag: "r" });
}
exports.readUtf8File = readUtf8File;
function writeUtf8File(path, content) {
(0, fs_1.writeFileSync)(path, content, { encoding: "utf8" });
}
exports.writeUtf8File = writeUtf8File;
function runCommandOrDie(command) {
try {
return (0, child_process_1.execSync)(command).toString();
}
catch (e) {
//console.error(e.stderr.toString());
logFatal(`Failed to run \'${command}\' in current directory \'${process.cwd()}\'.`);
}
}
exports.runCommandOrDie = runCommandOrDie;
function nodeVersionSatisfies(feature, range) {
if (!semver_1.default.satisfies(process.version, range)) {
logFatal(`${feature} requires node ${range}`);
}
}
exports.nodeVersionSatisfies = nodeVersionSatisfies;