Browse Source

use graceful-fs to avoid EMFILE for large projects

master
Guillermo Rauch 9 years ago
parent
commit
30adee935c
  1. 18
      lib/get-files.js
  2. 5
      lib/graceful-fs.js
  3. 4
      lib/hash.js
  4. 2
      package.json

18
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 '';
}

5
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.);

4
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);

2
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"

Loading…
Cancel
Save