From 76e4ab503707e43ea7a715fda91d2aa2f10b0b52 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 1 Dec 2016 10:23:59 -0800 Subject: [PATCH] support `now.files` in now.json Needs a test case still... --- lib/get-files.js | 17 ++++++++++++----- lib/index.js | 4 ++-- lib/read-metadata.js | 8 +++----- test/index.js | 14 +++++--------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/get-files.js b/lib/get-files.js index a225344..cf93a8e 100644 --- a/lib/get-files.js +++ b/lib/get-files.js @@ -24,11 +24,14 @@ const IGNORED = require('./ignored') * @return {Array} comprehensive list of paths to sync */ -async function npm(path, pkg, { +async function npm(path, pkg, nowConfig = {}, { limit = null, debug = false } = {}) { - const whitelist = pkg.now && pkg.now.files ? pkg.now.files : pkg.files + const whitelist = nowConfig.files || pkg.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}))))) @@ -125,12 +128,16 @@ const asAbsolute = function (path, parent) { * @return {Array} comprehensive list of paths to sync */ -async function docker(path, { +async function docker(path, nowConfig = {}, { limit = null, debug = false } = {}) { + const whitelist = nowConfig.files + // base search path - const search_ = ['.'] + // the now.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 = search_.map(file => asAbsolute(file, path)) @@ -262,7 +269,7 @@ const explode = async function (paths, {accepts, debug}) { const maybeRead = async function (path, default_ = '') { try { - return (await readFile(path, 'utf8')) + return await readFile(path, 'utf8') } catch (err) { return default_ } diff --git a/lib/index.js b/lib/index.js index 2976753..4caf982 100644 --- a/lib/index.js +++ b/lib/index.js @@ -65,9 +65,9 @@ module.exports = class Now extends EventEmitter { } if (deploymentType === 'npm') { - files = await getNpmFiles(path, pkg, {debug: this._debug}) + files = await getNpmFiles(path, pkg, nowConfig, {debug: this._debug}) } else { - files = await getDockerFiles(path, {debug: this._debug}) + files = await getDockerFiles(path, nowConfig, {debug: this._debug}) } if (this._debug) { diff --git a/lib/read-metadata.js b/lib/read-metadata.js index 21ba172..21d6275 100644 --- a/lib/read-metadata.js +++ b/lib/read-metadata.js @@ -12,7 +12,7 @@ const listPackage = { } } -module.exports = async function (path, { +module.exports = async function getMetadata (path, { deploymentType = 'npm', deploymentName, quiet = false, @@ -26,8 +26,7 @@ module.exports = async function (path, { let description try { - nowConfig = await readFile(resolvePath(path, 'now.json')) - nowConfig = JSON.parse(nowConfig) + nowConfig = JSON.parse(await readFile(resolvePath(path, 'now.json'))) } catch (err) { // if the file doesn't exist then that's fine; any other error bubbles up if (err.code !== 'ENOENT') { @@ -42,8 +41,7 @@ module.exports = async function (path, { pkg = listPackage } else { try { - pkg = await readFile(resolvePath(path, 'package.json')) - pkg = JSON.parse(pkg) + pkg = JSON.parse(await readFile(resolvePath(path, 'package.json'))) } catch (err) { const e = Error(`Failed to read JSON in "${path}/package.json"`) e.userError = true diff --git a/test/index.js b/test/index.js index 33ea08f..8c6faa7 100644 --- a/test/index.js +++ b/test/index.js @@ -7,22 +7,18 @@ const {asc: alpha} = require('alpha-sort') const {readFile} = require('fs-promise') // Ours -const {npm: getNpmFiles_, docker: getDockerFiles} = require('../lib/get-files') const hash = require('../lib/hash') +const readMetadata = require('../lib/read-metadata') +const {npm: getNpmFiles_, docker: getDockerFiles} = require('../lib/get-files') const prefix = join(__dirname, '_fixtures') + '/' const base = path => path.replace(prefix, '') const fixture = name => resolve(`./test/_fixtures/${name}`) -const readJSON = async file => { - const data = await readFile(file) - return JSON.parse(data) -} - // overload to force debugging -const getNpmFiles = async dir => { - const pkg = await readJSON(resolve(dir, 'package.json')) - return getNpmFiles_(dir, pkg) +const getNpmFiles = async (dir) => { + const { pkg, nowConfig } = await readMetadata(dir, { quiet: true, strict: false }) + return getNpmFiles_(dir, pkg, nowConfig) } test('`files`', async t => {