Browse Source

Proper getting of files for static deployments

master
Tony Kovanen 8 years ago
parent
commit
095569969b
  1. 83
      lib/get-files.js
  2. 8
      lib/index.js

83
lib/get-files.js

@ -62,6 +62,86 @@ const asAbsolute = function(path, parent) {
return resolve(parent, path)
}
/**
* Returns a list of files in the given
* directory that are subject to be
* synchronized for static deployments.
*
* @param {String} full path to directory
* @param {Object} options:
* - `limit` {Number|null} byte limit
* - `debug` {Boolean} warn upon ignore
* @return {Array} comprehensive list of paths to sync
*/
async function staticFiles(
path,
nowConfig = {},
{ limit = null, hasNowJson = false, debug = false } = {}
) {
const whitelist = nowConfig.files
// The package.json `files` whitelist still
// honors ignores: https://docs.npmjs.com/files/package.json#files
const search_ = whitelist || ['.']
// Convert all filenames into absolute paths
const search = Array.prototype.concat.apply(
[],
await Promise.all(
search_.map(file => glob(file, { cwd: path, absolute: true, dot: true }))
)
)
// Compile list of ignored patterns and files
const gitIgnore = await maybeRead(resolve(path, '.gitignore'))
const filter = ignore()
.add(IGNORED + '\n' + clearRelative(gitIgnore))
.createFilter()
const prefixLength = path.length + 1
// The package.json `files` whitelist still
// honors npmignores: https://docs.npmjs.com/files/package.json#files
// but we don't ignore if the user is explicitly listing files
// under the now namespace, or using files in combination with gitignore
const accepts = file => {
const relativePath = file.substr(prefixLength)
if (relativePath === '') {
return true
}
const accepted = filter(relativePath)
if (!accepted && debug) {
console.log('> [debug] ignoring "%s"', file)
}
return accepted
}
// Locate files
if (debug) {
console.time(`> [debug] locating files ${path}`)
}
const files = await explode(search, {
accepts,
limit,
debug
})
if (debug) {
console.timeEnd(`> [debug] locating files ${path}`)
}
if (hasNowJson) {
files.push(asAbsolute('now.json', path))
}
// Get files
return unique(files)
}
/**
* Returns a list of files in the given
* directory that are subject to be
@ -299,5 +379,6 @@ async function explode(paths, { accepts, debug }) {
module.exports = {
npm,
docker
docker,
staticFiles
}

8
lib/index.js

@ -16,7 +16,11 @@ const { parse: parseIni } = require('ini')
const { readFile, stat, lstat } = require('fs-promise')
// Ours
const { npm: getNpmFiles, docker: getDockerFiles } = require('./get-files')
const {
staticFiles: getFiles,
npm: getNpmFiles,
docker: getDockerFiles
} = require('./get-files')
const ua = require('./ua')
const hash = require('./hash')
const Agent = require('./agent')
@ -87,7 +91,7 @@ module.exports = class Now extends EventEmitter {
engines = nowConfig.engines || pkg.engines
forwardNpm = forwardNpm || nowConfig.forwardNpm
} else if (type === 'static') {
files = await getNpmFiles(path, pkg, nowConfig, opts)
files = await getFiles(path, nowConfig, opts)
} else if (type === 'docker') {
files = await getDockerFiles(path, nowConfig, opts)
}

Loading…
Cancel
Save