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(readdir): fix abolute path issues and ENOENT error when there a… #10

Merged
merged 1 commit into from
May 21, 2017
Merged
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
38 changes: 26 additions & 12 deletions src/providers/readdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function filter(entry: readdir.IEntry, patterns: string[], options: IOptions): b
}

export function async(task: ITask, options: IOptions): Promise<string[] | readdir.IEntry[]> {
const cwd = path.join(options.cwd, task.base);
const cwd = path.resolve(options.cwd, task.base);
const entries: (string | readdir.IEntry)[] = [];

const api = options.stats ? readdir.readdirStreamStat : readdir.stream;
Expand All @@ -34,23 +34,37 @@ export function async(task: ITask, options: IOptions): Promise<string[] | readdi
});

stream.on('data', (entry) => entries.push(cb(entry)));
stream.on('error', (err) => reject(err));
stream.on('error', (err) => {
if (err.code === 'ENOENT') {
resolve([]);
} else {
reject(err);
}
});
stream.on('end', () => resolve(entries));
});
}

export function sync(task: ITask, options: IOptions): (string | readdir.IEntry)[] {
const cwd = path.join(options.cwd, task.base);
const cwd = path.resolve(options.cwd, task.base);

const api = options.stats ? readdir.readdirSyncStat : readdir.sync;
const cb = options.transform ? options.transform : (entry) => entry;
try {
const api = options.stats ? readdir.readdirSyncStat : readdir.sync;
const cb = options.transform ? options.transform : (entry) => entry;

const entries = api(cwd, {
filter: (entry) => filter(entry, task.patterns, options),
basePath: task.base === '.' ? '' : task.base,
deep: options.deep,
sep: '/'
});
const entries = api(cwd, {
filter: (entry) => filter(entry, task.patterns, options),
basePath: task.base === '.' ? '' : task.base,
deep: options.deep,
sep: '/'
});

return options.transform ? (<any>entries).map(cb) : entries;
return options.transform ? (<any>entries).map(cb) : entries;
} catch (err) {
if (err.code === 'ENOENT') {
return [];
}

throw err;
}
}