From c5626178ed4bbafc9af72b9a762b7a868f30ecfe Mon Sep 17 00:00:00 2001 From: Leo Lamprecht Date: Fri, 28 Oct 2016 13:21:21 +0200 Subject: [PATCH] Move files into content dir if static --- bin/now-deploy.js | 3 ++- lib/index.js | 29 +++++++++++++++++++++++++---- lib/read-metadata.js | 33 ++++++++++++++++++++++++--------- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/bin/now-deploy.js b/bin/now-deploy.js index 6035bb7..fb1c262 100755 --- a/bin/now-deploy.js +++ b/bin/now-deploy.js @@ -389,7 +389,8 @@ async function sync(token) { forceSync, forwardNpm: alwaysForwardNpm || forwardNpm, quiet, - wantsPublic + wantsPublic, + isStatic }) } catch (err) { if (debug) { diff --git a/lib/index.js b/lib/index.js index 4cb308a..4128fa7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ // Native import {homedir} from 'os' -import {resolve as resolvePath} from 'path' +import {resolve as resolvePath, join as joinPaths} from 'path' import EventEmitter from 'events' // Packages @@ -43,15 +43,18 @@ export default class Now extends EventEmitter { forceNew = false, forceSync = false, forwardNpm = false, - deploymentType = 'npm' + deploymentType = 'npm', + isStatic = false }) { this._path = path + this._static = isStatic let files const {pkg, name, description} = await readMetaData(path, { deploymentType, - quiet + quiet, + isStatic }) if (this._debug) { @@ -129,6 +132,10 @@ export default class Now extends EventEmitter { // array has a group of files with the same sha but different path files: Array.prototype.concat.apply([], Array.from(this._files).map(([sha, {data, names}]) => { return names.map(n => { + if (this._static && toRelative(n, this._path) !== 'package.json') { + n = this.pathInsideContent(n) + } + return { sha, size: data.length, @@ -215,6 +222,14 @@ export default class Now extends EventEmitter { return this._url } + pathInsideContent(position) { + const relativePath = toRelative(position, this._path) + const contentDir = joinPaths(this._path, 'content') + const newPath = joinPaths(contentDir, relativePath) + + return newPath + } + upload() { const parts = splitArray(this._missing, MAX_CONCURRENT) @@ -241,7 +256,13 @@ export default class Now extends EventEmitter { 'Content-Length': data.length, 'x-now-deployment-id': this._id, 'x-now-sha': sha, - 'x-now-file': names.map(name => toRelative(encodeURIComponent(name), this._path)).join(','), + 'x-now-file': names.map(name => { + if (this._static) { + name = this.pathInsideContent(name) + } + + return toRelative(encodeURIComponent(name), this._path) + }).join(','), 'x-now-size': data.length }, body: stream diff --git a/lib/read-metadata.js b/lib/read-metadata.js index c9a58eb..856b9cc 100644 --- a/lib/read-metadata.js +++ b/lib/read-metadata.js @@ -3,9 +3,20 @@ import chalk from 'chalk' import {readFile} from 'fs-promise' import {parse as parseDockerfile} from 'docker-file-parser' +const listPackage = { + version: '0.0.0', + scripts: { + start: 'list ./content' + }, + dependencies: { + list: 'latest' + } +} + export default async function (path, { deploymentType = 'npm', - quiet = false + quiet = false, + isStatic = false }) { let pkg = {} @@ -13,13 +24,17 @@ export default async function (path, { let description if (deploymentType === 'npm') { - try { - pkg = await readFile(resolvePath(path, 'package.json')) - pkg = JSON.parse(pkg) - } catch (err) { - const e = Error(`Failed to read JSON in "${path}/package.json"`) - e.userError = true - throw e + if (isStatic) { + pkg = listPackage + } else { + try { + pkg = await readFile(resolvePath(path, 'package.json')) + pkg = JSON.parse(pkg) + } catch (err) { + const e = Error(`Failed to read JSON in "${path}/package.json"`) + e.userError = true + throw e + } } if (!pkg.scripts || (!pkg.scripts.start && !pkg.scripts['now-start'])) { @@ -32,7 +47,7 @@ export default async function (path, { if (pkg.name === null || typeof pkg.name !== 'string') { name = basename(path) - if (!quiet) { + if (!quiet && !isStatic) { console.log(`> No \`name\` in \`package.json\`, using ${chalk.bold(name)}`) } } else {