Browse Source

Upgraded instance of `serve`

master
Leo Lamprecht 8 years ago
parent
commit
a49a033408
  1. 50
      lib/read-metadata.js

50
lib/read-metadata.js

@ -1,14 +1,14 @@
const { basename, resolve: resolvePath } = require("path"); const { basename, resolve: resolvePath } = require('path');
const chalk = require("chalk"); const chalk = require('chalk');
const { readFile, exists } = require("fs-promise"); const { readFile, exists } = require('fs-promise');
const { parse: parseDockerfile } = require("docker-file-parser"); const { parse: parseDockerfile } = require('docker-file-parser');
const listPackage = { const listPackage = {
scripts: { scripts: {
start: `NODE_ENV='production' serve ./content` start: `NODE_ENV='production' serve ./content`
}, },
dependencies: { dependencies: {
serve: "4.0.1" serve: '5.0.4'
} }
}; };
@ -17,7 +17,7 @@ module.exports = readMetaData;
async function readMetaData( async function readMetaData(
path, path,
{ {
deploymentType = "npm", deploymentType = 'npm',
deploymentName, deploymentName,
quiet = false, quiet = false,
strict = true, strict = true,
@ -32,11 +32,11 @@ async function readMetaData(
let description; let description;
try { try {
nowConfig = JSON.parse(await readFile(resolvePath(path, "now.json"))); nowConfig = JSON.parse(await readFile(resolvePath(path, 'now.json')));
hasNowJson = true; hasNowJson = true;
} catch (err) { } catch (err) {
// if the file doesn't exist then that's fine; any other error bubbles up // if the file doesn't exist then that's fine; any other error bubbles up
if (err.code !== "ENOENT") { if (err.code !== 'ENOENT') {
const e = Error(`Failed to read JSON in "${path}/now.json"`); const e = Error(`Failed to read JSON in "${path}/now.json"`);
e.userError = true; e.userError = true;
throw e; throw e;
@ -50,26 +50,26 @@ async function readMetaData(
deploymentType = nowConfig.type; deploymentType = nowConfig.type;
} else if ( } else if (
nowConfig.type === undefined && nowConfig.type === undefined &&
!await exists(resolvePath(path, "package.json")) !await exists(resolvePath(path, 'package.json'))
) { ) {
deploymentType = "static"; deploymentType = 'static';
} }
if (nowConfig.name) { if (nowConfig.name) {
deploymentName = nowConfig.name; deploymentName = nowConfig.name;
} }
} }
if (deploymentType === "static") { if (deploymentType === 'static') {
isStatic = true; isStatic = true;
deploymentType = "npm"; deploymentType = 'npm';
} }
if (deploymentType === "npm") { if (deploymentType === 'npm') {
if (isStatic) { if (isStatic) {
pkg = listPackage; pkg = listPackage;
} else { } else {
try { try {
pkg = JSON.parse(await readFile(resolvePath(path, "package.json"))); pkg = JSON.parse(await readFile(resolvePath(path, 'package.json')));
} catch (err) { } catch (err) {
const e = Error(`Failed to read JSON in "${path}/package.json"`); const e = Error(`Failed to read JSON in "${path}/package.json"`);
e.userError = true; e.userError = true;
@ -79,18 +79,18 @@ async function readMetaData(
if ( if (
strict && strict &&
(!pkg.scripts || (!pkg.scripts.start && !pkg.scripts["now-start"])) (!pkg.scripts || (!pkg.scripts.start && !pkg.scripts['now-start']))
) { ) {
const e = Error( const e = Error(
"Missing `start` (or `now-start`) script in `package.json`. " + 'Missing `start` (or `now-start`) script in `package.json`. ' +
"See: https://docs.npmjs.com/cli/start." 'See: https://docs.npmjs.com/cli/start.'
); );
e.userError = true; e.userError = true;
throw e; throw e;
} }
if (!deploymentName) { if (!deploymentName) {
if (typeof pkg.name === "string") { if (typeof pkg.name === 'string') {
name = pkg.name; name = pkg.name;
} else { } else {
name = basename(path); name = basename(path);
@ -104,12 +104,12 @@ async function readMetaData(
} }
description = pkg.description; description = pkg.description;
} else if (deploymentType === "docker") { } else if (deploymentType === 'docker') {
let docker; let docker;
try { try {
const dockerfile = await readFile( const dockerfile = await readFile(
resolvePath(path, "Dockerfile"), resolvePath(path, 'Dockerfile'),
"utf8" 'utf8'
); );
docker = parseDockerfile(dockerfile, { includeComments: true }); docker = parseDockerfile(dockerfile, { includeComments: true });
} catch (err) { } catch (err) {
@ -119,13 +119,13 @@ async function readMetaData(
} }
if (strict && docker.length <= 0) { if (strict && docker.length <= 0) {
const e = Error("No commands found in `Dockerfile`"); const e = Error('No commands found in `Dockerfile`');
e.userError = true; e.userError = true;
throw e; throw e;
} }
const labels = {}; const labels = {};
docker.filter(cmd => cmd.name === "LABEL").forEach(({ args }) => { docker.filter(cmd => cmd.name === 'LABEL').forEach(({ args }) => {
for (const key in args) { for (const key in args) {
if (!{}.hasOwnProperty.call(args, key)) { if (!{}.hasOwnProperty.call(args, key)) {
continue; continue;
@ -179,8 +179,8 @@ async function readMetaData(
// other // other
if (hasNowJson) { if (hasNowJson) {
const e = new Error( const e = new Error(
"You have a `now` configuration field" + 'You have a `now` configuration field' +
"inside `package.json`, but configuration is also present" + 'inside `package.json`, but configuration is also present' +
"in `now.json`! Please ensure there's a single source of configuration by removing one" "in `now.json`! Please ensure there's a single source of configuration by removing one"
); );
e.userError = true; e.userError = true;

Loading…
Cancel
Save