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

WIP: Fixes #42

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,26 @@ module.exports = async function (str, opts={}) {
if (!str) return [];

let glob = globalyzer(str);

opts.cwd = opts.cwd || '.';
opts.cwd = resolve(opts.cwd || '.');

if (!glob.isGlob) {
try {
let resolved = resolve(opts.cwd, str);
let resolved = join(opts.cwd, str);
let dirent = await stat(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 = [];
if (opts.flush) CACHE = {};
const { path } = globrex(glob.glob, { filepath:true, globstar:true, extended:true });

path.globstar = path.globstar.toString();
await walk(matches, glob.base, path, opts, '.', 0);

return opts.absolute ? matches.map(x => resolve(opts.cwd, x)) : matches;
return opts.absolute ? matches.map(x => join(opts.cwd, x)) : matches;
};
12 changes: 4 additions & 8 deletions sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,26 @@ module.exports = function (str, opts={}) {
if (!str) return [];

let glob = globalyzer(str);

opts.cwd = opts.cwd || '.';
opts.cwd = resolve(opts.cwd || '.');

if (!glob.isGlob) {
try {
let resolved = resolve(opts.cwd, str);
let resolved = join(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 = [];
if (opts.flush) CACHE = {};
const { path } = globrex(glob.glob, { filepath:true, globstar:true, extended:true });

path.globstar = path.globstar.toString();
walk(matches, glob.base, path, opts, '.', 0);

return opts.absolute ? matches.map(x => resolve(opts.cwd, x)) : matches;
return opts.absolute ? matches.map(x => join(opts.cwd, x)) : matches;
};
8 changes: 7 additions & 1 deletion test/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('glob: standard', async t => {
});

test('glob: glob', async t => {
t.plan(14);
t.plan(15);

t.same(await glob(''), []);
t.same(await glob('.'), ['.']);
Expand All @@ -34,6 +34,12 @@ test('glob: glob', async t => {
'test/helpers'
]);

await isMatch(t, '*', { cwd: __dirname }, [
'fixtures',
'glob.js',
'helpers'
]);

await isMatch(t, 'test/fixtures/*', {}, [
'test/fixtures/a.js',
'test/fixtures/a.mp3',
Expand Down