Skip to content
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

fix(commandDir): make dir relative to caller instead of require.main.filename #548

Merged
merged 1 commit into from Jul 14, 2016

Conversation

nexdrew
Copy link
Member

@nexdrew nexdrew commented Jul 11, 2016

Pull in get-caller-file to always interpret the directory given to .commandDir() as relative to the caller. This actually helps simplify the logic a good bit since I no longer need to keep track of a command-to-dir mapping between calls.

The require is deliberately lazy, so we only load the dependency if .commandDir() is called.

There are a few different packages that provide the "get filename of caller" functionality, but I chose get-caller-file because it was the only one that already had passing tests for the range of Node we need and includes Windows tests (caller seems to be popular but is lacking the fuller range of testing, and caller-path seems to be broken on Node 4+). An added benefit is that get-caller-file also seems to be the smallest and only pulls in one dependency.

Fixes #529.

@coveralls
Copy link

coveralls commented Jul 11, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling 76da9bb on commandDir-caller into eb1e185 on master.

@@ -62,14 +60,11 @@ module.exports = function (yargs, usage, validation) {
if (~context.files.indexOf(joined)) return visited
// keep track of visited files in context.files
context.files.push(joined)
// map "command path" to the directory path it came from
// so that dir can be relative in the API
context.dirs[context.commands.concat(parseCommand(visited.command || commandFromFilename(filename)).cmd).join('|')] = dir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love how much this simplifies things; except for the fact that yargs' documentation is terrifyingly verbose at this point, I'm really feeling like the command functionality is knocking it out of the park -- phew, that was hard work wasn't it.

@bcoe
Copy link
Member

bcoe commented Jul 14, 2016

love it :shipit:

@bcoe bcoe merged commit 3c2e479 into master Jul 14, 2016
@bcoe bcoe deleted the commandDir-caller branch July 14, 2016 05:47
@maxrimue maxrimue mentioned this pull request Aug 9, 2016
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants