2015-08-27 22:52:30 +02:00
|
|
|
var fs = require('fs');
|
|
|
|
var cp = require('child_process');
|
2015-08-30 21:29:19 +02:00
|
|
|
var glob = require('glob');
|
|
|
|
var ini = require('ini');
|
2015-09-02 14:26:23 +02:00
|
|
|
var Cache = require('./cache');
|
2015-08-27 22:52:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a JSON file
|
|
|
|
*
|
|
|
|
* @param filePath The path of the json file
|
|
|
|
* @return Promise
|
|
|
|
*/
|
|
|
|
exports.loadJSONFile = 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-27 22:52:30 +02:00
|
|
|
exports.runApp = function(execPath) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
cp.exec(execPath, function(err) {
|
|
|
|
if(err) return reject(err);
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2015-08-30 21:29:19 +02:00
|
|
|
|
2015-09-02 14:26:23 +02:00
|
|
|
var _searchCache = new Cache();
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
exports.findFiles = function(pattern, opts) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
2015-09-02 14:26:23 +02:00
|
|
|
|
|
|
|
var cachedResult = _searchCache.get([pattern, opts]);
|
|
|
|
if( cachedResult !== undefined) {
|
|
|
|
return resolve(cachedResult);
|
|
|
|
}
|
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
glob(pattern, opts, function(err, files) {
|
|
|
|
if(err) return reject(err);
|
2015-09-02 14:26:23 +02:00
|
|
|
_searchCache.set([pattern, opts], files);
|
2015-08-30 21:29:19 +02:00
|
|
|
return resolve(files);
|
|
|
|
});
|
2015-09-02 14:26:23 +02:00
|
|
|
|
2015-08-30 21:29:19 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.exists = function(filePath) {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
fs.exists(filePath, resolve);
|
|
|
|
});
|
|
|
|
};
|