-
-
Notifications
You must be signed in to change notification settings - Fork 599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP-HELP-WANTED] feat: use arg parsing instead of object-oriented flag setting #1738
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,102 @@ class GroupHelper { | |
this.strategy = undefined; | ||
} | ||
|
||
ifArg(name, fn, init, finalize) { | ||
const isArray = Array.isArray(argv[name]); | ||
const isSet = typeof argv[name] !== "undefined" && argv[name] !== null; | ||
if (!isArray && !isSet) return; | ||
|
||
init && init(); | ||
if (isArray) argv[name].forEach(fn); | ||
else if (isSet) fn(argv[name], -1); | ||
finalize && finalize(); | ||
} | ||
|
||
ifArgPair(name, fn, init, finalize) { | ||
ifArg( | ||
name, | ||
function (content, idx) { | ||
const i = content.indexOf("="); | ||
if (i < 0) { | ||
return fn(null, content, idx); | ||
} else { | ||
return fn(content.substr(0, i), content.substr(i + 1), idx); | ||
} | ||
}, | ||
init, | ||
finalize | ||
); | ||
} | ||
|
||
ifBooleanArg(name, fn) { | ||
ifArg(name, function (bool) { | ||
if (bool) { | ||
fn(); | ||
} | ||
}); | ||
} | ||
|
||
mapArgToBoolean(name, optionName) { | ||
ifArg(name, function (bool) { | ||
if (bool === true) options[optionName || name] = true; | ||
else if (bool === false) options[optionName || name] = false; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very misleading, we wee need this - |
||
} | ||
|
||
loadPlugin(name) { | ||
const loadUtils = require("loader-utils"); | ||
let args; | ||
try { | ||
const p = name && name.indexOf("?"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plugins can't have |
||
if (p > -1) { | ||
args = loadUtils.parseQuery(name.substring(p)); | ||
name = name.substring(0, p); | ||
} | ||
} catch (e) { | ||
console.log("Invalid plugin arguments " + name + " (" + e + ")."); | ||
process.exit(-1); // eslint-disable-line | ||
} | ||
|
||
let path; | ||
try { | ||
const resolve = require("enhanced-resolve"); | ||
path = resolve.sync(process.cwd(), name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this, because you have |
||
} catch (e) { | ||
console.log("Cannot resolve plugin " + name + "."); | ||
process.exit(-1); // eslint-disable-line | ||
} | ||
let Plugin; | ||
try { | ||
Plugin = require(path); | ||
} catch (e) { | ||
console.log("Cannot load plugin " + name + ". (" + path + ")"); | ||
throw e; | ||
} | ||
try { | ||
return new Plugin(args); | ||
} catch (e) { | ||
console.log("Cannot instantiate plugin " + name + ". (" + path + ")"); | ||
throw e; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need it, plugin only for |
||
|
||
ensureObject(parent, name, force) { | ||
if (force || typeof parent[name] !== "object" || parent[name] === null) { | ||
parent[name] = {}; | ||
} | ||
} | ||
|
||
ensureArray(parent, name) { | ||
if (!Array.isArray(parent[name])) { | ||
parent[name] = []; | ||
} | ||
} | ||
|
||
addPlugin(options, plugin) { | ||
ensureArray(options, "plugins"); | ||
options.plugins.unshift(plugin); | ||
} | ||
|
||
hyphenToUpperCase(name) { | ||
if (!name) { | ||
return name; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't understand code