Browse Source

support for custom limits

master
Guillermo Rauch 9 years ago
parent
commit
daaaa7161b
  1. 36
      lib/get-files.js

36
lib/get-files.js

@ -6,18 +6,20 @@ import IGNORED from './ignored';
import { resolve } from 'path'; import { resolve } from 'path';
import { stat, readdir, readFile } from 'fs-promise'; import { stat, readdir, readFile } from 'fs-promise';
const ONEMB = bytes('1mb');
/** /**
* Returns a list of files in the given * Returns a list of files in the given
* directory that are subject to be * directory that are subject to be
* synchronized. * synchronized.
* *
* @param {String} full path to directory * @param {String} full path to directory
* @param {String} contents of `package.json` to avoid lookup
* @param {Object} options:
* - `limit` {Number|null} byte limit
* - `debug` {Boolean} warn upon ignore
* @return {Array} comprehensive list of paths to sync * @return {Array} comprehensive list of paths to sync
*/ */
export default async function getFiles (path, pkg) { export default async function getFiles (path, pkg, { limit = null, debug = false }) {
if (!pkg) { if (!pkg) {
const pkgPath = resolve(path, 'package.json'); const pkgPath = resolve(path, 'package.json');
const pkgData = await readFile(pkgPath, 'utf8'); const pkgData = await readFile(pkgPath, 'utf8');
@ -28,19 +30,18 @@ export default async function getFiles (path, pkg) {
if (pkg.main) search = search.concat(pkg.main); if (pkg.main) search = search.concat(pkg.main);
search = search.map((file) => asAbsolute(file, path)); search = search.map((file) => asAbsolute(file, path));
const found = unique((await explode(search))); // compile list of ignored patterns and files
const npmIgnore = await maybeRead(resolve(path, '.npmignore')); const npmIgnore = await maybeRead(resolve(path, '.npmignore'));
const gitIgnore = npmIgnore const gitIgnore = npmIgnore
? '' ? ''
: (await maybeRead(resolve(path, '.gitignore'))); : (await maybeRead(resolve(path, '.gitignore')));
const ignored = unique(IGNORED const ignored = unique(IGNORED
.concat(gitIgnore.split('\n').filter(invalidFilter)) .concat(gitIgnore.split('\n').filter(invalidFilter))
.concat(npmIgnore.split('\n').filter(invalidFilter))) .concat(npmIgnore.split('\n').filter(invalidFilter)))
.map(file => resolve(path, file)); .map(file => resolve(path, file));
return found.filter(ignoredFilter(ignored)); // get files
return unique((await explode(search, ignored, { limit, debug })));
} }
/** /**
@ -51,8 +52,8 @@ export default async function getFiles (path, pkg) {
* @return {Function} filter fn * @return {Function} filter fn
*/ */
const ignoredFilter = (ignored) => (file) => { const isIgnored = (file, ignored) => {
return !ignored.some((test) => { return ignored.some((test) => {
// test that the target file is not under // test that the target file is not under
// an ignored directory // an ignored directory
const dir = test + '/'; const dir = test + '/';
@ -101,10 +102,14 @@ const asAbsolute = function (path, parent) {
* out: ['/a.js', '/b/c.js', '/b/d.js'] * out: ['/a.js', '/b/c.js', '/b/d.js']
* *
* @param {Array} of {String}s representing paths * @param {Array} of {String}s representing paths
* @param {Array} of ignored {String}s.
* @param {Object} options:
* - `limit` {Number|null} byte limit
* - `debug` {Boolean} warn upon ignore
* @return {Array} of {String}s of full paths * @return {Array} of {String}s of full paths
*/ */
const explode = async function (paths) { const explode = async function (paths, ignored, { limit, debug }) {
const many = async (all) => { const many = async (all) => {
return await Promise.all(all.map(async (file) => { return await Promise.all(all.map(async (file) => {
return await list(file); return await list(file);
@ -128,8 +133,15 @@ const explode = async function (paths) {
const all = await readdir(file); const all = await readdir(file);
return many(all.map(subdir => asAbsolute(subdir, file))); return many(all.map(subdir => asAbsolute(subdir, file)));
} else { } else {
if (s.size > ONEMB) { if (isIgnored(file, ignored)) {
console.error(`> \u001b[31mWarning!\u001b[39m Skipping file over 1MB: ${path}`); if (debug) console.log(`> [debug] Ignoring "${file}"`);
return null;
}
if (null != limit && s.size > limit) {
console.error(`> \u001b[31mWarning!\u001b[39m Skipping file ` +
`over ${bytes(limit)}: ${path}`);
return null;
} }
return path; return path;

Loading…
Cancel
Save