diff --git a/CHANGELOG.md b/CHANGELOG.md index 73e5115ff..89bb6dc96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), # Unreleased +## Fixed + +- Correctly catch some exceptions and propagate them to the CLI [#1107](https://github.com/stoplightio/prism/pull/1107) + # 3.3.3 (2020-04-02) ## Fixed @@ -15,14 +19,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - All the dependencies used by the various Prism packages have been explicitily declared avoiding some resolutions problems in case you are using Prism programmatically [#1072](https://github.com/stoplightio/prism/pull/1072) - Prism's current options aren't overriden internally anymore because of the `Prefer` header set [#1074](https://github.com/stoplightio/prism/pull/1074) - # 3.3.2 (2020-03-16) ## Fixed - Prism will not correctly consider that HTTP Security Schemes are case insensitive [#1044](https://github.com/stoplightio/prism/pull/1044) - # 3.3.1 (2020-03-13) ## Fixed diff --git a/docs/getting-started/01-installation.md b/docs/getting-started/01-installation.md index aa3971bd0..131427cf9 100644 --- a/docs/getting-started/01-installation.md +++ b/docs/getting-started/01-installation.md @@ -1,6 +1,6 @@ # Installation -For many, the easiest way to install Prism is as a node module. +For many, the easiest way to install Prism is as a node module. ```bash npm install -g @stoplight/prism-cli @@ -17,6 +17,7 @@ curl -L https://raw.githack.com/stoplightio/prism/master/install | sh ``` + > The binaries do _not_ auto-update, so you will need to run it again to install new versions. ## Docker @@ -37,6 +38,6 @@ If you want to start the proxy server, you can run a command like this: ```bash docker run --init --rm -it -d --name myprism -p 4010:4010 -v $(pwd):/tmp -P stoplight/prism:3 proxy -h 0.0.0.0 "/tmp/file.yml" http://host.docker.internal:8080 --errors -``` +``` Now everything is installed, let's look at some of the [concepts](./02-concepts.md). diff --git a/packages/cli/package.json b/packages/cli/package.json index aca258a5a..52dc88b83 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -15,7 +15,7 @@ "chokidar": "^3.2.1", "fp-ts": "^2.5.3", "lodash": "^4.17.15", - "pino": "^6.2.0", + "pino": "^6.2.1", "signale": "^1.4.0", "split2": "^3.1.1", "tslib": "^1.10.0", @@ -39,7 +39,7 @@ "url": "https://github.com/stoplightio/prism.git" }, "scripts": { - "cli": "node -r ts-node/register/transpile-only -r tsconfig-paths/register src/index.ts src/index.ts", + "cli": "node -r ts-node/register/transpile-only -r tsconfig-paths/register src/index.ts", "cli:debug": "node -r ts-node/register/transpile-only -r tsconfig-paths/register --inspect-brk src/index.ts" }, "types": "dist/index.d.ts" diff --git a/packages/cli/src/util/createServer.ts b/packages/cli/src/util/createServer.ts index e09f49ebc..3c1f65dc1 100644 --- a/packages/cli/src/util/createServer.ts +++ b/packages/cli/src/util/createServer.ts @@ -18,8 +18,10 @@ signale.config({ displayTimestamp: true }); const cliSpecificLoggerOptions: LoggerOptions = { customLevels: { start: 11 }, - useLevelLabels: true, level: 'start', + formatters: { + level: level => ({ level }), + }, }; const createMultiProcessPrism: CreatePrism = async options => { @@ -37,27 +39,26 @@ const createMultiProcessPrism: CreatePrism = async options => { return; } else { const logInstance = createLogger('CLI', cliSpecificLoggerOptions); - try { - return createPrismServerWithLogger(options, logInstance); - } catch (e) { + + return createPrismServerWithLogger(options, logInstance).catch(e => { logInstance.fatal(e.message); cluster.worker.kill(); - } + throw e; + }); } }; -const createSingleProcessPrism: CreatePrism = async options => { +const createSingleProcessPrism: CreatePrism = options => { signale.await({ prefix: chalk.bgWhiteBright.black('[CLI]'), message: 'Starting Prism…' }); const logStream = new PassThrough(); const logInstance = createLogger('CLI', cliSpecificLoggerOptions, logStream); pipeOutputToSignale(logStream); - try { - return createPrismServerWithLogger(options, logInstance); - } catch (e) { + return createPrismServerWithLogger(options, logInstance).catch(e => { logInstance.fatal(e.message); - } + throw e; + }); }; async function createPrismServerWithLogger(options: CreateBaseServerOptions, logInstance: Logger) { diff --git a/packages/cli/src/util/runner.ts b/packages/cli/src/util/runner.ts index fdf83ae94..150421b8a 100644 --- a/packages/cli/src/util/runner.ts +++ b/packages/cli/src/util/runner.ts @@ -7,9 +7,9 @@ import { CreateMockServerOptions } from './createServer'; export type CreatePrism = (options: CreateMockServerOptions) => Promise; export function runPrismAndSetupWatcher(createPrism: CreatePrism, options: CreateMockServerOptions) { - return createPrism(options).then(possiblyServer => { - if (possiblyServer) { - let server: IPrismHttpServer = possiblyServer; + return createPrism(options).then(possibleServer => { + if (possibleServer) { + let server: IPrismHttpServer = possibleServer; const watcher = chokidar.watch(options.document, { // See https://github.com/paulmillr/chokidar#persistence diff --git a/packages/core/package.json b/packages/core/package.json index b4e99d3c0..44f45be9b 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -18,7 +18,7 @@ "dependencies": { "fp-ts": "^2.5.3", "lodash": "^4.17.15", - "pino": "^6.2.0", + "pino": "^6.2.1", "tslib": "^1.10.0" }, "publishConfig": { diff --git a/packages/http-server/src/server.ts b/packages/http-server/src/server.ts index fbdae0424..00bcc89ea 100644 --- a/packages/http-server/src/server.ts +++ b/packages/http-server/src/server.ts @@ -220,7 +220,13 @@ export const createServer = (operations: IHttpOperation[], opts: IPrismHttpServe }, listen: (port: number, ...args: any[]) => - new Promise(resolve => server.listen(port, ...args, () => resolve(addressInfoToString(server.address())))), + new Promise((resolve, reject) => { + server.once('error', e => reject(e.message)); + server.listen(port, ...args, (err: unknown) => { + if (err) return reject(err); + return resolve(addressInfoToString(server.address())); + }); + }), }; }; diff --git a/packages/http/package.json b/packages/http/package.json index c166e0d84..bfd5e5850 100644 --- a/packages/http/package.json +++ b/packages/http/package.json @@ -34,7 +34,7 @@ "lodash": "^4.17.15", "node-fetch": "^2.6.0", "openapi-sampler": "^1.0.0-beta.15", - "pino": "^6.2.0", + "pino": "^6.2.1", "tslib": "^1.10.0", "type-is": "^1.6.18", "uri-template-lite": "^19.4.0" diff --git a/packages/http/src/getHttpOperations.ts b/packages/http/src/getHttpOperations.ts index bcb067cd7..ec3c6ef2f 100644 --- a/packages/http/src/getHttpOperations.ts +++ b/packages/http/src/getHttpOperations.ts @@ -27,7 +27,9 @@ const httpAndFileResolver = new Resolver({ export async function getHttpOperationsFromResource(file: string): Promise { const isRemote = /^https?:\/\//i.test(file); - const fileContent = isRemote ? await fetch(file).then(d => d.text()) : fs.readFileSync(file, { encoding: 'utf8' }); + const fileContent = await (isRemote + ? fetch(file).then(d => d.text()) + : fs.promises.readFile(file, { encoding: 'utf8' })); return getHttpOperations(fileContent, isRemote ? file : resolve(file)); } diff --git a/yarn.lock b/yarn.lock index 94b575522..21c3f2d23 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7131,7 +7131,7 @@ pino-std-serializers@^2.4.2: resolved "https://registry.yarnpkg.com/pino-std-serializers/-/pino-std-serializers-2.4.2.tgz#cb5e3e58c358b26f88969d7e619ae54bdfcc1ae1" integrity sha512-WaL504dO8eGs+vrK+j4BuQQq6GLKeCCcHaMB2ItygzVURcL1CycwNEUHTD/lHFHs/NL5qAz2UKrjYWXKSf4aMQ== -pino@^6.2.0: +pino@^6.2.1: version "6.2.1" resolved "https://registry.yarnpkg.com/pino/-/pino-6.2.1.tgz#d2b86306b3998e8f6bb33bdf23910d418ed696cf" integrity sha512-5F5A+G25Ex2rMOBEe3XYGyLSF4dikQZsFvPojwsqnDBX+rfg7+kw9s5i7pHuVAJImekjwb+MR9jQyHWPLENlvQ==