Files
libloot/scripts/helpers.js
T

104 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

// Helper functions shared across scripts.
2015-12-13 18:27:43 +00:00
'use strict';
const childProcess = require('child_process');
2015-12-13 18:27:43 +00:00
const path = require('path');
const fs = require('fs');
const os = require('os');
2015-12-13 18:27:43 +00:00
function fileExists(filePath) {
try {
// Query the entry
const stats = fs.lstatSync(filePath);
2015-12-13 18:27:43 +00:00
// Is it a directory?
if (stats.isFile()) {
return true;
}
} catch (e) {
/* Don't do anything, it's not an error. */
}
2015-12-13 18:27:43 +00:00
return false;
}
function getBinaryParentPaths(rootPath) {
const paths = [
2015-12-13 18:27:43 +00:00
{
path: path.join(rootPath, 'build'),
label: null,
},
{
path: path.join(rootPath, 'build', '32'),
label: '32-bit',
2015-12-13 18:27:43 +00:00
},
{
path: path.join(rootPath, 'build', '64'),
label: '64-bit',
2015-12-13 18:27:43 +00:00
},
];
2015-12-13 18:27:43 +00:00
if (os.platform() === 'win32') {
paths.forEach(parentPath => {
parentPath.path = path.join(parentPath.path, 'Release');
});
2015-12-13 18:27:43 +00:00
}
2015-12-13 18:27:43 +00:00
return paths;
}
function getAppReleasePaths(rootPath) {
let file = 'LOOT';
if (os.platform() === 'win32') {
file += '.exe';
}
2015-12-13 18:27:43 +00:00
return getBinaryParentPaths(rootPath).filter(
parentPath => fileExists(path.join(parentPath.path, file))
);
}
function getBinaryPaths(rootPath, file) {
return getBinaryParentPaths(rootPath)
.map(parentPath => {
parentPath.path = path.join(parentPath.path, file);
return parentPath;
})
.filter(parentPath => fileExists(parentPath.path));
}
function getApiBinaryPaths(rootPath) {
let file = 'loot_api';
if (os.platform() === 'win32') {
file += '.dll';
} else {
2016-07-22 08:08:35 +01:00
file = `lib${file}.so`;
}
2016-08-07 20:27:11 +01:00
return getBinaryParentPaths(rootPath).filter(
parentPath => fileExists(path.join(parentPath.path, file))
);
}
function getMetadataValidatorBinaryPaths(rootPath) {
let file = 'metadata-validator';
if (os.platform() === 'win32') {
file += '.exe';
2015-12-13 18:27:43 +00:00
}
return getBinaryPaths(rootPath, file);
}
function safeExecFileSync(file, args, options) {
try {
return childProcess.execFileSync(file, args, options);
} catch (error) {
throw new Error(error.message);
}
}
module.exports.fileExists = fileExists;
module.exports.getAppReleasePaths = getAppReleasePaths;
module.exports.getApiBinaryPaths = getApiBinaryPaths;
module.exports.getMetadataValidatorBinaryPaths = getMetadataValidatorBinaryPaths;
module.exports.safeExecFileSync = safeExecFileSync;