Skip to content

Commit

Permalink
Handle options for no-glob patterns (terkelg#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Johnson authored and terkelg committed Dec 3, 2018
1 parent 3c7006c commit b4ceaef
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const globalyzer = require('globalyzer');
const { join, resolve, relative } = require('path');
const isHidden = /(^|[\\\/])\.[^\\\/\.]/g;
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
let CACHE = {};

async function walk(output, prefix, lexer, opts, dirname='', level=0) {
Expand Down Expand Up @@ -57,10 +58,17 @@ module.exports = async function (str, opts={}) {
opts.cwd = opts.cwd || '.';

if (!glob.isGlob) {
let resolved = resolve(opts.cwd, str);
if (!fs.existsSync(resolved)) return []
try {
let resolved = resolve(opts.cwd, str);
let dirent = await stat(resolved);
if (opts.filesOnly && !dirent.isFile()) return []

return opts.absolute ? [resolved] : [str];
return opts.absolute ? [resolved] : [str];
} catch (err) {
if (err.code != 'ENOENT') throw err;

return []
}
}

if (opts.flush) CACHE = {};
Expand Down
15 changes: 14 additions & 1 deletion sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,20 @@ function walk(output, prefix, lexer, opts, dirname='', level=0) {
module.exports = function (str, opts={}) {
let glob = globalyzer(str);

if (!glob.isGlob) return fs.existsSync(str) ? [str] : [];
if (!glob.isGlob) {
try {
let resolved = resolve(opts.cwd, str);
let dirent = fs.statSync(resolved);
if (opts.filesOnly && !dirent.isFile()) return []

return opts.absolute ? [resolved] : [str];
} catch (err) {
if (err.code != 'ENOENT') throw err;

return []
}
}

if (opts.flush) CACHE = {};

let matches = [];
Expand Down
16 changes: 16 additions & 0 deletions test/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ test('glob: glob', async t => {
]);
});

test("glob: path dosen't exist (without glob)", async t => {
t.plan(1);

await isMatch(t, 'z.js', { cwd }, [ ]);
});

test('glob: options.cwd', async t => {
t.plan(2);

Expand Down Expand Up @@ -202,3 +208,13 @@ test('glob: options.filesOnly', async t => {
'test/fixtures/two'
]);
});

test('glob: options.filesOnly (without glob)', async t => {
t.plan(2);

await isMatch(t, 'test/fixtures/one', { filesOnly:true }, []);

await isMatch(t, 'test/fixtures/one', { filesOnly:false }, [
'test/fixtures/one',
]);
});

0 comments on commit b4ceaef

Please sign in to comment.