Skip to content

Commit

Permalink
Implement smart-directory resolution rules:
Browse files Browse the repository at this point in the history
1. If a script name expands to a directory, then:
  1. If there's an /index file, match all the (/index*)
  2. If there's no /index, match all immediate descendents (/*)
2. If the script name expands to a file, match all extensions (*)

I knew this method would get bigger!

This will require we remove ambiguity warnings and embrace
the possibility globs will match multiple scripts.


(sorry folks,using the sync stat methods b/c callback hell 
is getting to me)
  • Loading branch information
searls committed Apr 9, 2016
1 parent 31bb8d4 commit 21e5aea
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
npm-debug.log
24 changes: 23 additions & 1 deletion lib/resolve-script/generate-glob.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
var path = require('path')
var fs = require('fs')

module.exports = function (dir1, dir2) {
return path.resolve(dir1, dir2.replace(/\:/g, '/')) + '*'
var expanded = path.resolve(dir1, dir2.replace(/\:/g, '/'))
if (isDir(expanded)) {
if (containsIndexFile(expanded)) {
return expanded + '/index*'
} else {
return expanded + '/*'
}
} else {
return expanded + '*'
}
}

function isDir (f) {
try {
return fs.statSync(f).isDirectory()
} catch (e) {}
}

function containsIndexFile (f) {
try {
return fs.statSync(f + '/index').isFile()
} catch (e) {}
}
25 changes: 21 additions & 4 deletions lib/resolve-script/generate-glob.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
module.exports = function () {
var subject = require('./generate-glob')
var path = require('path')

assert.equal('/foo/bar*', subject('/foo', 'bar'))
assert.equal('/foo/bar/baz*', subject('/foo', 'bar:baz'))
module.exports = {
beforeEach: function () {
this.subject = require('./generate-glob')
this.root = path.resolve('test/fixtures/user-scripts')
},
fileInADir: function () {
assert.equal(this.root + '/foo/bar*', this.subject(this.root, 'foo:bar'))
},
nonExistentFile: function () {
assert.equal(this.root + '/fake/stuff*', this.subject(this.root, 'fake:stuff'))
},
dirWithNoIndex: function () {
assert.equal(this.root + '/baz/*', this.subject(this.root, 'baz'))
},
dirWithAnIndexFile: function () {
assert.equal(this.root + '/car/index*', this.subject(this.root, 'car'))
},
dirWithADirNamedIndex: function () {
assert.equal(this.root + '/dog/*', this.subject(this.root, 'dog'))
}
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/user-scripts/top/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby

puts 'rubby'
11 changes: 11 additions & 0 deletions test/safe/dir-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var runScripty = require('../run-scripty')

module.exports = {
runsIndexWhenDirIsFound: function (done) {
runScripty('top', {}, function (er, code, stdio) {
assert.equal(code, 0)
assert.includes(stdio.stdout, 'rubby')
done(er)
})
}
}

0 comments on commit 21e5aea

Please sign in to comment.