Browse Source

support `now.files` in now.json

Needs a test case still...
master
Nathan Rajlich 8 years ago
parent
commit
76e4ab5037
  1. 17
      lib/get-files.js
  2. 4
      lib/index.js
  3. 8
      lib/read-metadata.js
  4. 14
      test/index.js

17
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_
}

4
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) {

8
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

14
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 => {

Loading…
Cancel
Save