whisper
Version:
A task-based automation app. Leiningen style.
128 lines (127 loc) • 3.7 kB
JavaScript
(function(){
var fs, path, log, home, read, readJson, existsP, callableP, rootP, findLocalConfig, findRootConfig, findConfig, loadSingleConfig, loadConfig, packageFromWhisper, findRootPackage, findLocalPackage, findPackages, loadSinglePackageConfig, loadPackageConfig, whisperConfig;
fs = require('fs');
path = require('path');
log = require('./log');
home = (function(env){
switch (process.platform) {
case 'win32':
return env.USERPROFILE || env.HOMEDRIVE + env.HOMEPATH;
default:
return env.HOME;
}
}.call(this, process.env));
read = function(name){
return fs.readFileSync(name, 'utf-8');
};
readJson = compose$([JSON.parse, read]);
existsP = fs.existsSync;
callableP = function(it){
return typeof it === 'function';
};
rootP = function(dir){
return path.resolve(dir) === path.resolve('/');
};
findLocalConfig = function(dir, initial){
var whisperFile, dirAbove;
initial == null && (initial = dir);
whisperFile = path.resolve(dir, '.whisper');
dirAbove = path.resolve(dir, '..');
switch (false) {
case !existsP(whisperFile):
return whisperFile;
case !rootP(dirAbove):
return null;
default:
return findLocalConfig(dirAbove, initial);
}
};
findRootConfig = function(){
var whisperDir, whisperFile;
whisperDir = path.resolve(home, '.whisper.d', '.whisper');
whisperFile = path.resolve(home, '.whisper');
switch (false) {
case !existsP(whisperDir):
return whisperDir;
case !existsP(whisperFile):
return whisperFile;
default:
return null;
}
};
findConfig = function(dir){
return [findRootConfig(), findLocalConfig(dir)].filter(Boolean);
};
loadSingleConfig = function(name){
var module;
module = require(name);
if (!callableP(module)) {
log.fatal("The configuration file \"" + name + "\" doesn't export the expected configuration interface `Whisper -> ()`");
}
return module;
};
loadConfig = function(dir){
return findConfig(dir).map(loadSingleConfig);
};
packageFromWhisper = function(name){
return path.resolve(name, '..', 'package.json');
};
findRootPackage = function(){
var file;
file = path.resolve(home, '.whisper.d', 'package.json');
switch (false) {
case !existsP(file):
return file;
default:
return null;
}
};
findLocalPackage = function(dir){
var local, pkg;
local = findLocalConfig(dir);
pkg = packageFromWhisper(local);
switch (false) {
case !local:
return existsP(pkg) && pkg;
case !existsP('package.json'):
return path.resolve('package.json');
default:
return null;
}
};
findPackages = function(dir){
return [findRootPackage(), findLocalPackage(dir)].filter(Boolean);
};
loadSinglePackageConfig = function(name){
switch (false) {
case !existsP(name):
return readJson(name);
default:
return null;
}
};
loadPackageConfig = function(dir){
return findPackages(dir).map(loadSinglePackageConfig);
};
whisperConfig = function(config){
return config.whisper || {};
};
module.exports = {
home: home,
findLocalConfig: findLocalConfig,
findRootConfig: findRootConfig,
findConfig: findConfig,
loadSingleConfig: loadSingleConfig,
loadConfig: loadConfig,
loadSinglePackageConfig: loadSinglePackageConfig,
loadPackageConfig: loadPackageConfig,
whisperConfig: whisperConfig
};
function compose$(fs){
return function(){
var i, args = arguments;
for (i = fs.length; i > 0; --i) { args = [fs[i-1].apply(this, args)]; }
return args[0];
};
}
}).call(this);