-
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
1 parent
11e6521
commit 99ed661
Showing
11 changed files
with
1,980 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015","es2017"] | ||
} |
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,59 @@ | ||
node_modules/ | ||
design/ | ||
docs/ | ||
dist/ | ||
_site/ | ||
*.bak | ||
mock/salary/vars.js | ||
default.vm | ||
|
||
|
||
# GIT/SVN generated files # | ||
########################### | ||
*.diff | ||
*.patch | ||
.svn/ | ||
.git/ | ||
|
||
# IDE generated files # | ||
####################### | ||
*.iml | ||
.idea/ | ||
.ipr | ||
.iws | ||
*~ | ||
~* | ||
.settings | ||
.project | ||
.lastbuildtime | ||
.*proj | ||
.deploy/ | ||
|
||
# Packages # | ||
############ | ||
# it's better to unpack these files and commit the raw source | ||
# git has its own built in compression methods | ||
*.7z | ||
*.dmg | ||
*.gz | ||
*.iso | ||
*.jar | ||
*.rar | ||
*.tar | ||
*.zip | ||
|
||
# Logs and databases # | ||
###################### | ||
*.log | ||
*.sql | ||
*.sqlite | ||
|
||
# OS generated files # | ||
###################### | ||
.DS_Store | ||
.DS_Store? | ||
._* | ||
.Spotlight-V100 | ||
.Trashes | ||
ehthumbs.db | ||
Thumbs.db |
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,52 @@ | ||
'use strict'; | ||
|
||
var minimist = require('./minimist.js'); | ||
var toString = function toString(obj) { | ||
return Object.prototype.toString.call(obj); | ||
}; | ||
|
||
var Samma = function Samma(command, config) { | ||
if (toString(command) !== '[object String]') { | ||
throw '命令需要求String类型'; | ||
} | ||
var arr = command.split(' '); | ||
var orderStr = arr[0]; | ||
// const args = arr.slice(1); | ||
var argsObj = minimist(arr); | ||
var cmdArr = config.cmd; | ||
var cmdItem = cmdArr.find(function (item) { | ||
return item.name === orderStr; | ||
}); | ||
if (cmdItem) { | ||
cmdItem.callback(argsObj, orderStr); | ||
} else { | ||
throw 'command not found ' + orderStr; | ||
} | ||
}; | ||
|
||
var commander = function commander(config) { | ||
if (toString(config) !== '[object Object]') { | ||
throw 'config要求Object类型'; | ||
} | ||
|
||
if (toString(config.cmd) !== '[object Array]') { | ||
throw 'cmd属性要求Array类型'; | ||
} | ||
|
||
var execute = config.global || 'Samma'; | ||
var executeFn = function executeFn(command) { | ||
Samma(command, config); | ||
}; | ||
|
||
if (typeof window === 'undefined') { | ||
return executeFn; | ||
} | ||
|
||
if (window[execute]) { | ||
throw 'window\u4E0B\u5DF2\u6709' + execute + '\u5C5E\u6027\uFF0C\u8BF7\u6539\u53D8global\u5C5E\u6027\u503C'; | ||
} else { | ||
window[execute] = executeFn; | ||
} | ||
}; | ||
|
||
module.exports = commander; |
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,184 @@ | ||
'use strict'; | ||
|
||
module.exports = function (args, opts) { | ||
if (!opts) opts = {}; | ||
|
||
var flags = { bools: {}, strings: {}, unknownFn: null }; | ||
|
||
if (typeof opts['unknown'] === 'function') { | ||
flags.unknownFn = opts['unknown']; | ||
} | ||
|
||
if (typeof opts['boolean'] === 'boolean' && opts['boolean']) { | ||
flags.allBools = true; | ||
} else { | ||
[].concat(opts['boolean']).filter(Boolean).forEach(function (key) { | ||
flags.bools[key] = true; | ||
}); | ||
} | ||
|
||
var aliases = {}; | ||
Object.keys(opts.alias || {}).forEach(function (key) { | ||
aliases[key] = [].concat(opts.alias[key]); | ||
aliases[key].forEach(function (x) { | ||
aliases[x] = [key].concat(aliases[key].filter(function (y) { | ||
return x !== y; | ||
})); | ||
}); | ||
}); | ||
|
||
[].concat(opts.string).filter(Boolean).forEach(function (key) { | ||
flags.strings[key] = true; | ||
if (aliases[key]) { | ||
flags.strings[aliases[key]] = true; | ||
} | ||
}); | ||
|
||
var defaults = opts['default'] || {}; | ||
|
||
var argv = { _: [] }; | ||
Object.keys(flags.bools).forEach(function (key) { | ||
setArg(key, defaults[key] === undefined ? false : defaults[key]); | ||
}); | ||
|
||
var notFlags = []; | ||
|
||
if (args.indexOf('--') !== -1) { | ||
notFlags = args.slice(args.indexOf('--') + 1); | ||
args = args.slice(0, args.indexOf('--')); | ||
} | ||
|
||
function argDefined(key, arg) { | ||
return flags.allBools && /^--[^=]+$/.test(arg) || flags.strings[key] || flags.bools[key] || aliases[key]; | ||
} | ||
|
||
function setArg(key, val, arg) { | ||
if (arg && flags.unknownFn && !argDefined(key, arg)) { | ||
if (flags.unknownFn(arg) === false) return; | ||
} | ||
|
||
var value = !flags.strings[key] && isNumber(val) ? Number(val) : val; | ||
setKey(argv, key.split('.'), value); | ||
|
||
(aliases[key] || []).forEach(function (x) { | ||
setKey(argv, x.split('.'), value); | ||
}); | ||
} | ||
|
||
function setKey(obj, keys, value) { | ||
var o = obj; | ||
keys.slice(0, -1).forEach(function (key) { | ||
if (o[key] === undefined) o[key] = {}; | ||
o = o[key]; | ||
}); | ||
|
||
var key = keys[keys.length - 1]; | ||
if (o[key] === undefined || flags.bools[key] || typeof o[key] === 'boolean') { | ||
o[key] = value; | ||
} else if (Array.isArray(o[key])) { | ||
o[key].push(value); | ||
} else { | ||
o[key] = [o[key], value]; | ||
} | ||
} | ||
|
||
function aliasIsBoolean(key) { | ||
return aliases[key].some(function (x) { | ||
return flags.bools[x]; | ||
}); | ||
} | ||
|
||
for (var i = 0; i < args.length; i++) { | ||
var arg = args[i]; | ||
|
||
if (/^--.+=/.test(arg)) { | ||
// Using [\s\S] instead of . because js doesn't support the | ||
// 'dotall' regex modifier. See: | ||
// http://stackoverflow.com/a/1068308/13216 | ||
var m = arg.match(/^--([^=]+)=([\s\S]*)$/); | ||
var key = m[1]; | ||
var value = m[2]; | ||
if (flags.bools[key]) { | ||
value = value !== 'false'; | ||
} | ||
setArg(key, value, arg); | ||
} else if (/^--no-.+/.test(arg)) { | ||
var key = arg.match(/^--no-(.+)/)[1]; | ||
setArg(key, false, arg); | ||
} else if (/^--.+/.test(arg)) { | ||
var key = arg.match(/^--(.+)/)[1]; | ||
var next = args[i + 1]; | ||
if (next !== undefined && !/^-/.test(next) && !flags.bools[key] && !flags.allBools && (aliases[key] ? !aliasIsBoolean(key) : true)) { | ||
setArg(key, next, arg); | ||
i++; | ||
} else if (/^(true|false)$/.test(next)) { | ||
setArg(key, next === 'true', arg); | ||
i++; | ||
} else { | ||
setArg(key, flags.strings[key] ? '' : true, arg); | ||
} | ||
} else if (/^-[^-]+/.test(arg)) { | ||
// 魔改部分: 保持行为和/^--.+/一致 | ||
// todo: 抽象成一个函数进行复用 | ||
var key = arg.match(/^-(.+)/)[1]; | ||
var next = args[i + 1]; | ||
if (next !== undefined && !/^-/.test(next) && !flags.bools[key] && !flags.allBools && (aliases[key] ? !aliasIsBoolean(key) : true)) { | ||
setArg(key, next, arg); | ||
i++; | ||
} else if (/^(true|false)$/.test(next)) { | ||
setArg(key, next === 'true', arg); | ||
i++; | ||
} else { | ||
setArg(key, flags.strings[key] ? '' : true, arg); | ||
} | ||
} else { | ||
if (!flags.unknownFn || flags.unknownFn(arg) !== false) { | ||
argv._.push(flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)); | ||
} | ||
if (opts.stopEarly) { | ||
argv._.push.apply(argv._, args.slice(i + 1)); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
Object.keys(defaults).forEach(function (key) { | ||
if (!hasKey(argv, key.split('.'))) { | ||
setKey(argv, key.split('.'), defaults[key]); | ||
|
||
(aliases[key] || []).forEach(function (x) { | ||
setKey(argv, x.split('.'), defaults[key]); | ||
}); | ||
} | ||
}); | ||
|
||
if (opts['--']) { | ||
argv['--'] = new Array(); | ||
notFlags.forEach(function (key) { | ||
argv['--'].push(key); | ||
}); | ||
} else { | ||
notFlags.forEach(function (key) { | ||
argv._.push(key); | ||
}); | ||
} | ||
|
||
return argv; | ||
}; | ||
|
||
function hasKey(obj, keys) { | ||
var o = obj; | ||
keys.slice(0, -1).forEach(function (key) { | ||
o = o[key] || {}; | ||
}); | ||
|
||
var key = keys[keys.length - 1]; | ||
return key in o; | ||
} | ||
|
||
function isNumber(x) { | ||
if (typeof x === 'number') return true; | ||
if (/^0x[0-9a-f]+$/i.test(x)) return true; | ||
return (/^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x) | ||
); | ||
} |
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,26 @@ | ||
/* | ||
命令解析器重构思路: | ||
命令是字符串类型 | ||
对字符串进行split(" ") | ||
1. | ||
取出第一个元素,放入{_:里面} | ||
如果他的下一个元素是普通元素,也放入放入{_:里面} | ||
2. | ||
取出/^--.+=/ | ||
确定其key和value | ||
3. | ||
取出所有/^--.+/及其元素位置 | ||
key就是匹配/^--.+/成功的元素 | ||
value就是其下一个位置的普通元素 | ||
下一个位置不是普通元素 | ||
则value为true | ||
PS:普通元素是指非/^-[^-]+/和非/^--.+=/ | ||
minimist的bug是 | ||
-x3=4解析不出来 | ||
-x3解析成{x:3} | ||
*/ | ||
"use strict"; |
Oops, something went wrong.