approvals
Version:
Approval Tests Library - Capturing Human Intelligence
176 lines (175 loc) • 6.39 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringifyKeysInOrder = exports.fixFilePathSlashes = exports.searchForExecutable = exports.findProgramOnPath = exports.isBinaryFile = exports.hasCommandLineArgument = exports.assertFileExists = exports.createEmptyFileIfNotExists = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const shell = __importStar(require("shelljs"));
const osTools = __importStar(require("./osTools"));
const _cachedProgramLookups = {};
const createEmptyFileIfNotExists = (file) => {
if (!fs.existsSync(file)) {
const ext = (path.extname(file) || "").toLowerCase();
if (ext === ".png") {
fs.writeFileSync(file, fs.readFileSync(path.join(__dirname, "DummyApprovedFiles", "UnapprovedImage.png")));
}
else {
fs.writeFileSync(file, "");
}
}
};
exports.createEmptyFileIfNotExists = createEmptyFileIfNotExists;
const assertFileExists = (file) => {
if (!fs.existsSync(file)) {
throw new Error("File not found: " + file);
}
};
exports.assertFileExists = assertFileExists;
const hasCommandLineArgument = (arg) => {
return process.argv.some((val) => (val || "").toLowerCase() === arg);
};
exports.hasCommandLineArgument = hasCommandLineArgument;
const isBinaryFile = (buffer) => {
const contentStartUTF8 = buffer.toString("utf8", 0, 24);
for (let i = 0, _ref = contentStartUTF8.length; i < _ref; i++) {
const charCode = contentStartUTF8.charCodeAt(i);
if (charCode === 65533 || charCode <= 8) {
return true;
}
}
return false;
};
exports.isBinaryFile = isBinaryFile;
const trimExtension = (filePath) => {
if (!filePath)
return filePath;
const i = filePath.lastIndexOf(".");
const ext = i < 0 ? "" : filePath.substr(i);
return ext ? filePath.substring(0, filePath.length - ext.length) : filePath;
};
const findProgramOnPath = (programName) => {
if (_cachedProgramLookups.hasOwnProperty(programName)) {
return _cachedProgramLookups[programName];
}
const output = shell.exec(`${osTools.findProgramPathCommand} ${programName}`, { silent: true });
let result = null;
if (output.stdout) {
const file = output.stdout.split("\n")[0].trim();
const fixedFile = fixFilePathSlashes(file);
if (fs.existsSync(fixedFile)) {
result = fixedFile;
}
}
if (!result) {
const pathMinusExtension = trimExtension(programName);
if (pathMinusExtension !== programName) {
result = findProgramOnPath(pathMinusExtension);
}
}
_cachedProgramLookups[programName] = result;
return result;
};
exports.findProgramOnPath = findProgramOnPath;
const searchForExecutable = (folderInProgramInFiles, fileName) => {
if (!fileName) {
fileName = folderInProgramInFiles;
folderInProgramInFiles = undefined;
}
const programOnPath = findProgramOnPath(fileName);
if (programOnPath) {
return programOnPath;
}
const lookInProgramFiles = (fileName_) => {
if (osTools.platform.isWindows) {
let tryVar = findInPath("C:/Program Files", folderInProgramInFiles || "", fileName_);
if (tryVar) {
return tryVar;
}
tryVar = findInPath("C:/Program Files (x86)", folderInProgramInFiles || "", fileName_);
if (tryVar) {
return tryVar;
}
}
return null;
};
const findInPath = (root, dir, file) => {
const fullPath = path.join(root, dir, file);
const fixedFullPath = fixFilePathSlashes(fullPath);
if (fs.existsSync(fixedFullPath)) {
return fixedFullPath;
}
return null;
};
let fileFound = lookInProgramFiles(fileName);
if (fileFound) {
return fileFound;
}
const suffix = ".exe";
if (!fileName.endsWith(suffix)) {
fileFound = lookInProgramFiles(fileName + suffix);
if (fileFound) {
return fileFound;
}
}
return "";
};
exports.searchForExecutable = searchForExecutable;
const fixFilePathSlashes = (path_) => {
return path_.replace(/\\/g, "/");
};
exports.fixFilePathSlashes = fixFilePathSlashes;
const recursivelyOrderKeys = (unordered) => {
if (unordered === null) {
return null;
}
if (Array.isArray(unordered)) {
return unordered.map(recursivelyOrderKeys);
}
if (typeof unordered === "object" && unordered !== null) {
const ordered = {};
Object.keys(unordered)
.sort()
.forEach((key) => {
ordered[key] = recursivelyOrderKeys(unordered[key]);
});
return ordered;
}
return unordered;
};
const stringifyKeysInOrder = (data) => {
const sortedData = recursivelyOrderKeys(data);
return JSON.stringify(sortedData, null, " ");
};
exports.stringifyKeysInOrder = stringifyKeysInOrder;