104 lines
2.2 KiB
JavaScript
104 lines
2.2 KiB
JavaScript
var fs = require('fs');
|
|
var cp = require('child_process');
|
|
var glob = require('glob');
|
|
var ini = require('ini');
|
|
var Cache = require('./cache');
|
|
|
|
/**
|
|
* Load a JSON file
|
|
*
|
|
* @param filePath The path of the json file
|
|
* @return Promise
|
|
*/
|
|
exports.loadJSON = function(filePath) {
|
|
return new Promise(function(resolve, reject) {
|
|
fs.readFile(filePath, 'utf8', function(err, fileContent) {
|
|
if(err) return reject(err);
|
|
try {
|
|
var json = JSON.parse(fileContent);
|
|
return resolve(json);
|
|
} catch(err) {
|
|
return reject(err);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
exports.saveJSON = function(filePath, obj) {
|
|
|
|
var jsonStr = JSON.stringify(obj, null, 2);
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
fs.writeFile(filePath, jsonStr, function(err) {
|
|
if(err) return reject(err);
|
|
return resolve();
|
|
});
|
|
});
|
|
|
|
};
|
|
|
|
/**
|
|
* Load a INI file
|
|
*
|
|
* @param filePath The path of the json file
|
|
* @return Promise
|
|
*/
|
|
exports.loadINIFile = function(filePath) {
|
|
return new Promise(function(resolve, reject) {
|
|
fs.readFile(filePath, 'utf8', function(err, content) {
|
|
if(err) return reject(err);
|
|
try {
|
|
var decoded = ini.decode(content);
|
|
return resolve(decoded);
|
|
} catch(err) {
|
|
return reject(err);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
exports.clearFreeDesktopFlags = function(exec) {
|
|
return exec.replace(/%[uUdDfFnNickvm]/g, '');
|
|
};
|
|
|
|
exports.runApp = function(execPath, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
if(opts.clearFreeDesktopFlags) {
|
|
execPath = exports.clearFreeDesktopFlags(execPath);
|
|
}
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
cp.exec(execPath, function(err) {
|
|
if(err) return reject(err);
|
|
return resolve();
|
|
});
|
|
});
|
|
};
|
|
|
|
var _searchCache = new Cache();
|
|
|
|
exports.findFiles = function(pattern, opts) {
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var cachedResult = _searchCache.get([pattern, opts]);
|
|
if( cachedResult !== undefined) {
|
|
return resolve(cachedResult);
|
|
}
|
|
|
|
glob(pattern, opts, function(err, files) {
|
|
if(err) return reject(err);
|
|
_searchCache.set([pattern, opts], files);
|
|
return resolve(files);
|
|
});
|
|
|
|
});
|
|
};
|
|
|
|
exports.exists = function(filePath) {
|
|
return new Promise(function(resolve) {
|
|
fs.exists(filePath, resolve);
|
|
});
|
|
};
|