forked from gulpjs/gulp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sttk
committed
Oct 10, 2016
1 parent
9b053ed
commit 92a9107
Showing
28 changed files
with
1,524 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
|
||
description: { | ||
type: 'string', | ||
requiresArg: true, | ||
}, | ||
|
||
flags: { | ||
help: { | ||
type: 'boolean', | ||
}, | ||
|
||
cwd: { | ||
type: 'string', | ||
requiresArg: true, | ||
resolvePath: true, | ||
}, | ||
|
||
configPath: { | ||
type: 'string', | ||
requiresArg: true, | ||
resolvePath: true, | ||
}, | ||
}, | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
var yargs = require('yargs'); | ||
var camelCase = require('lodash.camelcase'); | ||
var eachProps = require('each-props'); | ||
var copyProps = require('copy-props'); | ||
|
||
module.exports = function(cliSpecs, usage) { | ||
var argv = yargs(process.argv.slice(2)).argv; | ||
|
||
var usedNames = []; | ||
var usedSpecs = {}; | ||
|
||
eachProps(cliSpecs, function(spec, name) { | ||
if (argv[name] != null || argv[spec.alias] != null) { | ||
usedNames.push(camelCase(name)); | ||
usedSpecs[name] = spec; | ||
} | ||
return true; | ||
}); | ||
|
||
var argvBySpecs = yargs.usage(usage, usedSpecs).argv; | ||
return copyProps(argvBySpecs, {}, usedNames); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
var eachProps = require('each-props'); | ||
|
||
module.exports = function(env, envOpts) { | ||
var usedEnv = {}; | ||
|
||
eachProps(env, function(value, key) { | ||
switch (key) { | ||
|
||
case 'cwd': | ||
case 'require': | ||
case 'configPath': { | ||
if (value != null && envOpts[key] != null) { | ||
usedEnv[key] = value; | ||
} | ||
break; | ||
} | ||
|
||
case 'configBase': { | ||
if (env.configPath != null && envOpts.configPath != null) { | ||
usedEnv[key] = value; | ||
usedEnv.configBase = value; | ||
} | ||
break; | ||
} | ||
|
||
default: { | ||
if (value != null) { | ||
usedEnv[key] = value; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
return usedEnv; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
var setDeep = require('lodash.set'); | ||
var getDeep = require('lodash.get'); | ||
var endsWith = require('lodash.endswith'); | ||
var isString = require('lodash.isstring'); | ||
var eachProps = require('each-props'); | ||
var copyProps = require('copy-props'); | ||
var validator = require('./validator'); | ||
|
||
function Config(configFiles) { | ||
function Constructor(configFiles) { | ||
this.content = {}; | ||
this.configFiles = configFiles || {}; | ||
} | ||
Constructor.prototype = Config.prototype; | ||
return new Constructor(configFiles); | ||
} | ||
|
||
Config.prototype.loadFiles = function(base, keysInOrder, specs) { | ||
var content = this.content; | ||
|
||
loadDefaultConfig(content, specs); | ||
|
||
var configFileBase = this.configFiles[base] || {}; | ||
var configPathFiles = keysInOrder.map(function(key) { | ||
return configFileBase[key]; | ||
}).filter(isString); | ||
|
||
var validate = validator(specs); | ||
configPathFiles.forEach(function(filePath) { | ||
var thisObj = { filename: filePath, dirname: path.dirname(filePath) }; | ||
copyProps(require(filePath), content, validate.bind(thisObj)); | ||
}); | ||
|
||
return this; | ||
}; | ||
|
||
Config.prototype.copyFrom = function(obj, props) { | ||
copyProps(obj, this.content, props); | ||
return this; | ||
}; | ||
|
||
Config.prototype.copyTo = function(obj, props) { | ||
copyProps(this.content, obj, props); | ||
return this; | ||
}; | ||
|
||
Config.prototype.is = function(key) { | ||
return !!getDeep(this.content, key); | ||
}; | ||
|
||
Config.prototype.get = function(key) { | ||
return getDeep(this.content, key); | ||
}; | ||
|
||
function loadDefaultConfig(content, specs) { | ||
eachProps(specs, function(value, key) { | ||
if (endsWith(key, '.default')) { | ||
var dstKey = key.slice(0, -'.default'.length); | ||
setDeep(content, dstKey, value); | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
module.exports = Config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
var getDeep = require('lodash.get'); | ||
var isString = require('lodash.isstring'); | ||
|
||
module.exports = function(specs) { | ||
return function(value, key) { | ||
try { | ||
return validate.call(this, value, key); | ||
} catch (e) { | ||
e.message += ' in ' + path.resolve(this.filename); | ||
throw e; | ||
} | ||
}; | ||
|
||
/* $lab:coverage:off$ */ | ||
function validate(value, key) { | ||
/* $lab:coverage:on$ */ | ||
|
||
var spec = getDeep(specs, key); | ||
if (!spec) { | ||
throw new Error('Undefined config: ' + key); | ||
} | ||
|
||
switch (spec.type) { | ||
|
||
case 'string': { | ||
if (!isString(value)) { | ||
throw new TypeError('Invalid type of config: ' + key); | ||
} | ||
if (spec.requiresArg && !value) { | ||
throw new Error('Required value of config: ' + key); | ||
} | ||
if (spec.resolvePath) { | ||
return path.resolve(this.dirname, value); | ||
} | ||
return value; | ||
} | ||
|
||
default: { | ||
if (typeof value !== spec.type) { | ||
throw new TypeError('Invalid type of config: ' + key); | ||
} | ||
return value; | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.