From cc8e0b8e4aaf69e43c40adaf2c78da2c770aaa03 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 28 Apr 2017 18:31:32 -0700 Subject: [PATCH] Use implicit "start" script when `server.js` exists Moves the verification step to the `create()` function, instead of in `readMetaData()`. Closes https://github.com/zeit/now-cli/issues/181. --- lib/index.js | 24 ++++++++++++++++++++++++ lib/read-metadata.js | 13 ------------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9e0ef15..c7333a3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -23,6 +23,9 @@ const Agent = require('./agent'); const readMetaData = require('./read-metadata'); const toHost = require('./to-host'); +// Helpers +const { error } = require('./error'); + // How many concurrent HTTP/2 stream uploads const MAX_CONCURRENT = 10; @@ -77,6 +80,18 @@ module.exports = class Now extends EventEmitter { const opts = { debug: this._debug, hasNowJson }; if (deploymentType === 'npm') { files = await getNpmFiles(path, pkg, nowConfig, opts); + + // A `start` or `now-start` npm script, or a `server.js` file + // in the root directory of the deployment are required + if (!hasNpmStart(pkg) && !hasFile(path, files, 'server.js')) { + error( + 'Missing `start` (or `now-start`) script in `package.json`. ' + + 'See: https://docs.npmjs.com/cli/start.' + ); + + // eslint-disable-next-line unicorn/no-process-exit + process.exit(1); + } } else { files = await getDockerFiles(path, nowConfig, opts); } @@ -989,3 +1004,12 @@ async function responseError(res) { return err; } + +function hasNpmStart(pkg) { + return pkg.scripts && (pkg.scripts.start || pkg.scripts['now-start']); +} + +function hasFile(base, files, name) { + const relative = files.map(file => toRelative(file, base)); + return relative.indexOf(name) !== -1; +} diff --git a/lib/read-metadata.js b/lib/read-metadata.js index 7bff301..ea552d2 100644 --- a/lib/read-metadata.js +++ b/lib/read-metadata.js @@ -84,19 +84,6 @@ async function readMetaData( } } - if ( - strict && - (!pkg.scripts || (!pkg.scripts.start && !pkg.scripts['now-start'])) - ) { - error( - 'Missing `start` (or `now-start`) script in `package.json`. ' + - 'See: https://docs.npmjs.com/cli/start.' - ); - - // eslint-disable-next-line unicorn/no-process-exit - process.exit(1); - } - if (!deploymentName) { if (typeof pkg.name === 'string' && pkg.name !== '') { name = pkg.name;