|
|
@ -6,18 +6,20 @@ import IGNORED from './ignored'; |
|
|
|
import { resolve } from 'path'; |
|
|
|
import { stat, readdir, readFile } from 'fs-promise'; |
|
|
|
|
|
|
|
const ONEMB = bytes('1mb'); |
|
|
|
|
|
|
|
/** |
|
|
|
* Returns a list of files in the given |
|
|
|
* directory that are subject to be |
|
|
|
* synchronized. |
|
|
|
* |
|
|
|
* @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 |
|
|
|
*/ |
|
|
|
|
|
|
|
export default async function getFiles (path, pkg) { |
|
|
|
export default async function getFiles (path, pkg, { limit = null, debug = false }) { |
|
|
|
if (!pkg) { |
|
|
|
const pkgPath = resolve(path, 'package.json'); |
|
|
|
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); |
|
|
|
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 gitIgnore = npmIgnore |
|
|
|
? '' |
|
|
|
: (await maybeRead(resolve(path, '.gitignore'))); |
|
|
|
|
|
|
|
const ignored = unique(IGNORED |
|
|
|
.concat(gitIgnore.split('\n').filter(invalidFilter)) |
|
|
|
.concat(npmIgnore.split('\n').filter(invalidFilter))) |
|
|
|
.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 |
|
|
|
*/ |
|
|
|
|
|
|
|
const ignoredFilter = (ignored) => (file) => { |
|
|
|
return !ignored.some((test) => { |
|
|
|
const isIgnored = (file, ignored) => { |
|
|
|
return ignored.some((test) => { |
|
|
|
// test that the target file is not under
|
|
|
|
// an ignored directory
|
|
|
|
const dir = test + '/'; |
|
|
@ -101,10 +102,14 @@ const asAbsolute = function (path, parent) { |
|
|
|
* out: ['/a.js', '/b/c.js', '/b/d.js'] |
|
|
|
* |
|
|
|
* @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 |
|
|
|
*/ |
|
|
|
|
|
|
|
const explode = async function (paths) { |
|
|
|
const explode = async function (paths, ignored, { limit, debug }) { |
|
|
|
const many = async (all) => { |
|
|
|
return await Promise.all(all.map(async (file) => { |
|
|
|
return await list(file); |
|
|
@ -128,8 +133,15 @@ const explode = async function (paths) { |
|
|
|
const all = await readdir(file); |
|
|
|
return many(all.map(subdir => asAbsolute(subdir, file))); |
|
|
|
} else { |
|
|
|
if (s.size > ONEMB) { |
|
|
|
console.error(`> \u001b[31mWarning!\u001b[39m Skipping file over 1MB: ${path}`); |
|
|
|
if (isIgnored(file, ignored)) { |
|
|
|
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; |
|
|
|