From 30adee935c66d310abd1c8b082de298320fa664a Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Fri, 26 Feb 2016 14:40:04 -0800 Subject: [PATCH] use graceful-fs to avoid EMFILE for large projects --- lib/get-files.js | 18 ++++++++++-------- lib/graceful-fs.js | 5 +++++ lib/hash.js | 4 ++-- package.json | 2 ++ 4 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 lib/graceful-fs.js diff --git a/lib/get-files.js b/lib/get-files.js index 3733353..eaca3c2 100644 --- a/lib/get-files.js +++ b/lib/get-files.js @@ -1,4 +1,5 @@ -import fs, { readFile as read } from 'fs-promise'; +import { stat } from 'fs-promise'; +import { readdir, readFile } from './graceful-fs'; import { resolve } from 'path'; import flatten from 'arr-flatten'; import unique from 'array-unique'; @@ -16,7 +17,8 @@ import IGNORED from './ignored'; export default async function getFiles (path, pkg) { if (!pkg) { - const pkgData = await read(resolve(path, 'package.json'), 'utf8'); + const pkgPath = resolve(path, 'package.json'); + const pkgData = await readFile(pkgPath, 'utf8'); pkg = JSON.parse(pkgData); } @@ -103,19 +105,19 @@ const explode = async function (paths) { const list = async (file) => { let path = file; - let stat; + let s; try { - stat = await fs.stat(path); + s = await stat(path); } catch (e) { // in case the file comes from `files` or `main` // and it wasn't specified with `.js` by the user path = file + '.js'; - stat = await fs.stat(path); + s = await stat(path); } - if (stat.isDirectory()) { - const all = await fs.readdir(file); + if (s.isDirectory()) { + const all = await readdir(file); return many(all.map(subdir => asAbsolute(subdir, file))); } else { return path; @@ -133,7 +135,7 @@ const explode = async function (paths) { const maybeRead = async function (path) { try { - return (await fs.readFile(path, 'utf8')); + return (await readFile(path, 'utf8')); } catch (e) { return ''; } diff --git a/lib/graceful-fs.js b/lib/graceful-fs.js new file mode 100644 index 0000000..8963d53 --- /dev/null +++ b/lib/graceful-fs.js @@ -0,0 +1,5 @@ +import fs from 'graceful-fs'; +import promisify from 'es6-promisify'; + +export const readdir = promisify(fs.readdir); +export const readdir = promisify(fs.); diff --git a/lib/hash.js b/lib/hash.js index 176d104..dc4e89f 100644 --- a/lib/hash.js +++ b/lib/hash.js @@ -1,4 +1,4 @@ -import fs from 'fs-promise'; +import { readFile } from './graceful-fs'; import { createHash } from 'crypto'; /** @@ -10,7 +10,7 @@ import { createHash } from 'crypto'; export default async function hashes (files) { const entries = await Promise.all(files.map(async (file) => { - const data = await fs.readFile(file); + const data = await readFile(file); return [hash(data), file]; })); return new Map(entries); diff --git a/package.json b/package.json index 99335ae..3929f8b 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,9 @@ "chalk": "1.1.1", "commander": "2.9.0", "copy-paste": "1.1.4", + "es6-promisify": "3.0.0", "fs-promise": "0.4.1", + "graceful-fs": "4.1.3", "ms": "0.7.1", "node-fetch": "1.3.3", "spdy": "3.2.0"