|
@ -11,6 +11,57 @@ const { stat, readdir, readFile } = require('fs-promise'); |
|
|
// Ours
|
|
|
// Ours
|
|
|
const IGNORED = require('./ignored'); |
|
|
const IGNORED = require('./ignored'); |
|
|
|
|
|
|
|
|
|
|
|
const glob = async function(pattern, options) { |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
_glob(pattern, options, (error, files) => { |
|
|
|
|
|
if (error) { |
|
|
|
|
|
reject(error); |
|
|
|
|
|
} else { |
|
|
|
|
|
resolve(files); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Remove leading `./` from the beginning of ignores |
|
|
|
|
|
* because our parser doesn't like them :| |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
const clearRelative = function(str) { |
|
|
|
|
|
return str.replace(/(\n|^)\.\//g, '$1'); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Returns the contents of a file if it exists. |
|
|
|
|
|
* |
|
|
|
|
|
* @return {String} results or `''` |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
const maybeRead = async function(path, default_ = '') { |
|
|
|
|
|
try { |
|
|
|
|
|
return await readFile(path, 'utf8'); |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
return default_; |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Transform relative paths into absolutes, |
|
|
|
|
|
* and maintains absolutes as such. |
|
|
|
|
|
* |
|
|
|
|
|
* @param {String} maybe relative path |
|
|
|
|
|
* @param {String} parent full path |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
const asAbsolute = function(path, parent) { |
|
|
|
|
|
if (path[0] === '/') { |
|
|
|
|
|
return path; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return resolve(parent, path); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 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 |
|
@ -36,10 +87,10 @@ async function npm( |
|
|
) { |
|
|
) { |
|
|
const whitelist = (nowConfig && nowConfig.files) || pkg.files; |
|
|
const whitelist = (nowConfig && nowConfig.files) || pkg.files; |
|
|
|
|
|
|
|
|
// the package.json `files` whitelist still
|
|
|
// The package.json `files` whitelist still
|
|
|
// honors ignores: https://docs.npmjs.com/files/package.json#files
|
|
|
// honors ignores: https://docs.npmjs.com/files/package.json#files
|
|
|
const search_ = whitelist || ['.']; |
|
|
const search_ = whitelist || ['.']; |
|
|
// convert all filenames into absolute paths
|
|
|
// Convert all filenames into absolute paths
|
|
|
const search = Array.prototype.concat.apply( |
|
|
const search = Array.prototype.concat.apply( |
|
|
[], |
|
|
[], |
|
|
await Promise.all( |
|
|
await Promise.all( |
|
@ -47,12 +98,12 @@ async function npm( |
|
|
) |
|
|
) |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
// always include the "main" file
|
|
|
// Always include the "main" file
|
|
|
if (pkg.main) { |
|
|
if (pkg.main) { |
|
|
search.push(require.resolve(resolve(path, pkg.main), 'may-exclude')); // pkg: may-exclude suppresses warnings
|
|
|
search.push(require.resolve(resolve(path, pkg.main), 'may-exclude')); // Pkg: may-exclude suppresses warnings
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// compile list of ignored patterns and files
|
|
|
// Compile list of ignored patterns and files
|
|
|
const npmIgnore = await maybeRead(resolve(path, '.npmignore'), null); |
|
|
const npmIgnore = await maybeRead(resolve(path, '.npmignore'), null); |
|
|
const gitIgnore = npmIgnore === null |
|
|
const gitIgnore = npmIgnore === null |
|
|
? await maybeRead(resolve(path, '.gitignore')) |
|
|
? await maybeRead(resolve(path, '.gitignore')) |
|
@ -66,7 +117,7 @@ async function npm( |
|
|
|
|
|
|
|
|
const prefixLength = path.length + 1; |
|
|
const prefixLength = path.length + 1; |
|
|
|
|
|
|
|
|
// the package.json `files` whitelist still
|
|
|
// The package.json `files` whitelist still
|
|
|
// honors npmignores: https://docs.npmjs.com/files/package.json#files
|
|
|
// honors npmignores: https://docs.npmjs.com/files/package.json#files
|
|
|
// but we don't ignore if the user is explicitly listing files
|
|
|
// but we don't ignore if the user is explicitly listing files
|
|
|
// under the now namespace, or using files in combination with gitignore
|
|
|
// under the now namespace, or using files in combination with gitignore
|
|
@ -88,7 +139,7 @@ async function npm( |
|
|
return accepted; |
|
|
return accepted; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// locate files
|
|
|
// Locate files
|
|
|
if (debug) { |
|
|
if (debug) { |
|
|
console.time(`> [debug] locating files ${path}`); |
|
|
console.time(`> [debug] locating files ${path}`); |
|
|
} |
|
|
} |
|
@ -103,7 +154,7 @@ async function npm( |
|
|
console.timeEnd(`> [debug] locating files ${path}`); |
|
|
console.timeEnd(`> [debug] locating files ${path}`); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// always include manifest as npm does not allow ignoring it
|
|
|
// Always include manifest as npm does not allow ignoring it
|
|
|
// source: https://docs.npmjs.com/files/package.json#files
|
|
|
// source: https://docs.npmjs.com/files/package.json#files
|
|
|
files.push(asAbsolute('package.json', path)); |
|
|
files.push(asAbsolute('package.json', path)); |
|
|
|
|
|
|
|
@ -111,26 +162,10 @@ async function npm( |
|
|
files.push(asAbsolute('now.json', path)); |
|
|
files.push(asAbsolute('now.json', path)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// get files
|
|
|
// Get files
|
|
|
return unique(files); |
|
|
return unique(files); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Transform relative paths into absolutes, |
|
|
|
|
|
* and maintains absolutes as such. |
|
|
|
|
|
* |
|
|
|
|
|
* @param {String} maybe relative path |
|
|
|
|
|
* @param {String} parent full path |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
const asAbsolute = function(path, parent) { |
|
|
|
|
|
if (path[0] === '/') { |
|
|
|
|
|
return path; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return resolve(parent, path); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 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 |
|
@ -155,15 +190,15 @@ async function docker( |
|
|
) { |
|
|
) { |
|
|
const whitelist = nowConfig && nowConfig.files; |
|
|
const whitelist = nowConfig && nowConfig.files; |
|
|
|
|
|
|
|
|
// base search path
|
|
|
// Base search path
|
|
|
// the now.json `files` whitelist still
|
|
|
// the now.json `files` whitelist still
|
|
|
// honors ignores: https://docs.npmjs.com/files/package.json#files
|
|
|
// honors ignores: https://docs.npmjs.com/files/package.json#files
|
|
|
const search_ = whitelist || ['.']; |
|
|
const search_ = whitelist || ['.']; |
|
|
|
|
|
|
|
|
// convert all filenames into absolute paths
|
|
|
// Convert all filenames into absolute paths
|
|
|
const search = search_.map(file => asAbsolute(file, path)); |
|
|
const search = search_.map(file => asAbsolute(file, path)); |
|
|
|
|
|
|
|
|
// compile list of ignored patterns and files
|
|
|
// Compile list of ignored patterns and files
|
|
|
const dockerIgnore = await maybeRead(resolve(path, '.dockerignore'), null); |
|
|
const dockerIgnore = await maybeRead(resolve(path, '.dockerignore'), null); |
|
|
|
|
|
|
|
|
const filter = ignore() |
|
|
const filter = ignore() |
|
@ -193,7 +228,7 @@ async function docker( |
|
|
return accepted; |
|
|
return accepted; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// locate files
|
|
|
// Locate files
|
|
|
if (debug) { |
|
|
if (debug) { |
|
|
console.time(`> [debug] locating files ${path}`); |
|
|
console.time(`> [debug] locating files ${path}`); |
|
|
} |
|
|
} |
|
@ -204,7 +239,7 @@ async function docker( |
|
|
console.timeEnd(`> [debug] locating files ${path}`); |
|
|
console.timeEnd(`> [debug] locating files ${path}`); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// always include manifest as npm does not allow ignoring it
|
|
|
// Always include manifest as npm does not allow ignoring it
|
|
|
// source: https://docs.npmjs.com/files/package.json#files
|
|
|
// source: https://docs.npmjs.com/files/package.json#files
|
|
|
files.push(asAbsolute('Dockerfile', path)); |
|
|
files.push(asAbsolute('Dockerfile', path)); |
|
|
|
|
|
|
|
@ -212,22 +247,10 @@ async function docker( |
|
|
files.push(asAbsolute('now.json', path)); |
|
|
files.push(asAbsolute('now.json', path)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// get files
|
|
|
// Get files
|
|
|
return unique(files); |
|
|
return unique(files); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const glob = async function(pattern, options) { |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
_glob(pattern, options, (error, files) => { |
|
|
|
|
|
if (error) { |
|
|
|
|
|
reject(error); |
|
|
|
|
|
} else { |
|
|
|
|
|
resolve(files); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Explodes directories into a full list of files. |
|
|
* Explodes directories into a full list of files. |
|
|
* Eg: |
|
|
* Eg: |
|
@ -254,7 +277,7 @@ async function explode(paths, { accepts, debug }) { |
|
|
try { |
|
|
try { |
|
|
s = await stat(path); |
|
|
s = await stat(path); |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
// in case the file comes from `files` or `main`
|
|
|
// In case the file comes from `files` or `main`
|
|
|
// and it wasn't specified with `.js` by the user
|
|
|
// and it wasn't specified with `.js` by the user
|
|
|
path = file + '.js'; |
|
|
path = file + '.js'; |
|
|
|
|
|
|
|
@ -270,7 +293,9 @@ async function explode(paths, { accepts, debug }) { |
|
|
|
|
|
|
|
|
if (s.isDirectory()) { |
|
|
if (s.isDirectory()) { |
|
|
const all = await readdir(file); |
|
|
const all = await readdir(file); |
|
|
|
|
|
/* eslint-disable no-use-before-define */ |
|
|
return many(all.map(subdir => asAbsolute(subdir, file))); |
|
|
return many(all.map(subdir => asAbsolute(subdir, file))); |
|
|
|
|
|
/* eslint-enable no-use-before-define */ |
|
|
} else if (!s.isFile()) { |
|
|
} else if (!s.isFile()) { |
|
|
if (debug) { |
|
|
if (debug) { |
|
|
console.log('> [debug] ignoring special file "%s"', file); |
|
|
console.log('> [debug] ignoring special file "%s"', file); |
|
@ -282,39 +307,19 @@ async function explode(paths, { accepts, debug }) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const many = async all => { |
|
|
const many = async all => { |
|
|
return await Promise.all( |
|
|
const awaitAll = await Promise.all( |
|
|
all.map(async file => { |
|
|
all.map(async file => { |
|
|
return await list(file); |
|
|
const listed = await list(file); |
|
|
|
|
|
return listed; |
|
|
}) |
|
|
}) |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return awaitAll; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
return flatten(await many(paths)).filter(v => v !== null); |
|
|
return flatten(await many(paths)).filter(v => v !== null); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Returns the contents of a file if it exists. |
|
|
|
|
|
* |
|
|
|
|
|
* @return {String} results or `''` |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
const maybeRead = async function(path, default_ = '') { |
|
|
|
|
|
try { |
|
|
|
|
|
return await readFile(path, 'utf8'); |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
return default_; |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Remove leading `./` from the beginning of ignores |
|
|
|
|
|
* because our parser doesn't like them :| |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
const clearRelative = function(str) { |
|
|
|
|
|
return str.replace(/(\n|^)\.\//g, '$1'); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
module.exports = { |
|
|
npm, |
|
|
npm, |
|
|
docker |
|
|
docker |
|
|