Skip to content

Commit

Permalink
fix(commandDir): make dir relative to caller instead of require.main.…
Browse files Browse the repository at this point in the history
…filename (#548)
  • Loading branch information
nexdrew authored and bcoe committed Jul 14, 2016
1 parent 38b64f0 commit 3c2e479
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
9 changes: 2 additions & 7 deletions lib/command.js
Expand Up @@ -42,10 +42,8 @@ module.exports = function (yargs, usage, validation) {
}
}

self.addDirectory = function (dir, context, req, mainFilename, opts) {
self.addDirectory = function (dir, context, req, callerFile, opts) {
opts = opts || {}
// dir should be relative to the command module
dir = path.join(context.dirs[context.commands.join('|')] || '', dir)
// disable recursion to support nested directories of subcommands
if (typeof opts.recurse !== 'boolean') opts.recurse = false
// exclude 'json', 'coffee' from require-directory defaults
Expand All @@ -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
self.addHandler(visited)
}
return visited
}
requireDirectory({ require: req, filename: mainFilename }, dir, opts)
requireDirectory({ require: req, filename: callerFile }, dir, opts)
}

// lookup module object from require()d command and derive name
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"cliui": "^3.2.0",
"decamelize": "^1.1.1",
"get-caller-file": "^1.0.1",
"lodash.assign": "^4.0.3",
"os-locale": "^1.4.0",
"read-pkg-up": "^1.0.1",
Expand Down
18 changes: 6 additions & 12 deletions test/command.js
Expand Up @@ -344,8 +344,7 @@ describe('Command', function () {
it('supports relative dirs', function () {
var r = checkOutput(function () {
return yargs('--help').help().wrap(null)
// assumes cwd is node_modules/mocha/bin
.commandDir('../../../test/fixtures/cmddir')
.commandDir('fixtures/cmddir')
.argv
})
r.should.have.property('exit').and.be.true
Expand All @@ -363,8 +362,7 @@ describe('Command', function () {
it('supports nested subcommands', function () {
var r = checkOutput(function () {
return yargs('dream --help').help().wrap(null)
// assumes cwd is node_modules/mocha/bin
.commandDir('../../../test/fixtures/cmddir')
.commandDir('fixtures/cmddir')
.argv
}, [ './command' ])
r.should.have.property('exit').and.be.true
Expand All @@ -386,8 +384,7 @@ describe('Command', function () {
it('supports a "recurse" boolean option', function () {
var r = checkOutput(function () {
return yargs('--help').help().wrap(null)
// assumes cwd is node_modules/mocha/bin
.commandDir('../../../test/fixtures/cmddir', { recurse: true })
.commandDir('fixtures/cmddir', { recurse: true })
.argv
})
r.should.have.property('exit').and.be.true
Expand All @@ -411,8 +408,7 @@ describe('Command', function () {
var filename
var r = checkOutput(function () {
return yargs('--help').help().wrap(null)
// assumes cwd is node_modules/mocha/bin
.commandDir('../../../test/fixtures/cmddir', {
.commandDir('fixtures/cmddir', {
visit: function (_commandObject, _pathToFile, _filename) {
commandObject = _commandObject
pathToFile = _pathToFile
Expand Down Expand Up @@ -441,8 +437,7 @@ describe('Command', function () {
it('detects and ignores cyclic dir references', function () {
var r = checkOutput(function () {
return yargs('cyclic --help').help().wrap(null)
// assumes cwd is node_modules/mocha/bin
.commandDir('../../../test/fixtures/cmddir_cyclic')
.commandDir('fixtures/cmddir_cyclic')
.argv
}, [ './command' ])
r.should.have.property('exit').and.be.true
Expand All @@ -459,8 +454,7 @@ describe('Command', function () {
it('derives \'command\' string from filename when not exported', function () {
var r = checkOutput(function () {
return yargs('--help').help().wrap(null)
// assumes cwd is node_modules/mocha/bin
.commandDir('../../../test/fixtures/cmddir_noname')
.commandDir('fixtures/cmddir_noname')
.argv
})
r.should.have.property('exit').and.be.true
Expand Down
4 changes: 2 additions & 2 deletions yargs.js
Expand Up @@ -49,7 +49,7 @@ function Yargs (processArgs, cwd, parentRequire) {

// use context object to keep track of resets, subcommand execution, etc
// submodules should modify and check the state of context as necessary
const context = { resets: -1, commands: [], dirs: {}, files: [] }
const context = { resets: -1, commands: [], files: [] }
self.getContext = function () {
return context
}
Expand Down Expand Up @@ -207,7 +207,7 @@ function Yargs (processArgs, cwd, parentRequire) {

self.commandDir = function (dir, opts) {
const req = parentRequire || require
command.addDirectory(dir, self.getContext(), req, requireMainFilename(req), opts)
command.addDirectory(dir, self.getContext(), req, require('get-caller-file')(), opts)
return self
}

Expand Down

0 comments on commit 3c2e479

Please sign in to comment.