Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] esbuild #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "5.8.5",
"author": "Stainless <[email protected]>",
"bin": {
"prism": "./dist/index.js"
"prism": "./dist/bundled.js"
},
"bugs": "https://github.com/stainless-api/prism/issues",
"dependencies": {
Expand Down Expand Up @@ -43,6 +43,7 @@
"url": "https://github.com/stainless-api/prism.git"
},
"scripts": {
"build": "esbuild src/index.ts --bundle --platform=node --outfile=dist/bundled.js --inject:./src/import-meta-url.js --define:import.meta.url=import_meta_url --main-fields=module,main",
"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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/sharedOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dictionary } from '@stoplight/types';
import { Options } from 'yargs';
import * as pino from 'pino';
import pino from 'pino';

const sharedOptions: Dictionary<Options> = {
port: {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as $RefParser from '@stoplight/json-schema-ref-parser';
import $RefParser from '@stoplight/json-schema-ref-parser';
import { decycle } from '@stoplight/json';
import { get, camelCase, forOwn } from 'lodash';
import { JSONSchemaFaker } from 'json-schema-faker';
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/import-meta-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export var import_meta_url = require('url').pathToFileURL(__filename);

5 changes: 2 additions & 3 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env node

import * as yargs from 'yargs';
import yargs from 'yargs/yargs';
import mockCommand from './commands/mock';
import proxyCommand from './commands/proxy';

yargs
yargs(process.argv.slice(2))
.scriptName('prism')
.version()
.help(true)
.strict()
.wrap(yargs.terminalWidth())
.command(mockCommand)
.command(proxyCommand)
.demandCommand(1, '').argv;
8 changes: 5 additions & 3 deletions packages/cli/src/util/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import * as chalk from 'chalk';
import * as cluster from 'cluster';
import * as E from 'fp-ts/Either';
import { pipe } from 'fp-ts/function';
import * as pino from 'pino';
import * as signale from 'signale';
import * as split from 'split2';
import pino from 'pino';
import { Signale } from 'signale';
import split from 'split2';
import { PassThrough, Readable } from 'stream';
import { LOG_COLOR_MAP } from '../const/options';
import { CreatePrism } from './runner';
Expand All @@ -22,6 +22,8 @@ type PrismLogDescriptor = pino.LogDescriptor & {
input: IHttpRequest;
};

const signale = new Signale()

signale.config({ displayTimestamp: true });

const cliSpecificLoggerOptions: pino.LoggerOptions = {
Expand Down
67 changes: 26 additions & 41 deletions packages/cli/src/util/runner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { IPrismHttpServer } from '@stoplight/prism-http-server/src/types';
import * as chokidar from 'chokidar';
import * as os from 'os';
import { CreateMockServerOptions } from './createServer';
import { getHttpOperationsFromSpec } from '@stoplight/prism-http';

Expand All @@ -14,48 +12,35 @@ export function runPrismAndSetupWatcher(
if (possibleServer) {
let server: IPrismHttpServer = possibleServer;

const watcher = chokidar.watch(options.document, {
// See https://github.com/paulmillr/chokidar#persistence
persistent: os.platform() === 'darwin',
disableGlobbing: true,
awaitWriteFinish: { stabilityThreshold: 500, pollInterval: 100 },
});

watcher.on('change', () => {
server.logger.info('Restarting Prism...');

getHttpOperationsFromSpec(options.document)
.then(operations => {
if (operations.length === 0) {
server.logger.info(
'No operations found in the current file, continuing with the previously loaded spec.'
);
} else {
return server
.close()
.then(() => {
server.logger.info('Loading the updated operations...');

return createPrism(options);
})
.then(newServer => {
if (newServer) {
server = newServer;
}
});
}
})
.catch(() => {
server.logger.warn('Something went terribly wrong, trying to start Prism with the original document.');
server.logger.info('Restarting Prism...');

return getHttpOperationsFromSpec(options.document)
.then(operations => {
if (operations.length === 0) {
server.logger.info('No operations found in the current file, continuing with the previously loaded spec.');
} else {
return server
.close()
.then(() => createPrism(options))
.catch(() => process.exit(1));
});
});

return new Promise(resolve => watcher.once('ready', () => resolve(server)));
.then(() => {
server.logger.info('Loading the updated operations...');

return createPrism(options);
})
.then(newServer => {
if (newServer) {
server = newServer;
}
});
}
})
.catch(() => {
server.logger.warn('Something went terribly wrong, trying to start Prism with the original document.');

return server
.close()
.then(() => createPrism(options))
.catch(() => process.exit(1));
});
}
});
}
2 changes: 1 addition & 1 deletion packages/core/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as pino from 'pino';
import pino from 'pino';
import { defaultsDeep } from 'lodash';

export function createLogger(
Expand Down
2 changes: 1 addition & 1 deletion packages/http-server/src/getHttpConfigFromRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { pipe } from 'fp-ts/function';
import * as E from 'fp-ts/Either';
import * as D from 'io-ts/lib/Decoder';
//@ts-ignore
import * as parsePreferHeader from 'parse-prefer-header';
import parsePreferHeader from 'parse-prefer-header';

const BooleanFromString = D.parse<string, boolean>(s =>
s === 'true' ? D.success(true) : s === 'false' ? D.success(false) : D.failure(s, 'a boolean')
Expand Down
2 changes: 1 addition & 1 deletion packages/http-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { IPrismHttpServer, IPrismHttpServerOpts } from './types';
import { IPrismDiagnostic } from '@stoplight/prism-core';
import { MicriHandler } from 'micri';
import micri, { Router, json, send, text } from 'micri';
import * as typeIs from 'type-is';
import typeIs from 'type-is';
import { getHttpConfigFromRequest } from './getHttpConfigFromRequest';
import { serialize } from './serialize';
import { merge } from 'lodash/fp';
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/__tests__/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as pino from 'pino';
import pino from 'pino';

/**
* Creates special instance of pino logger that prevent collecting or logging anything
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { fold } from 'fp-ts/TaskEither';
import * as Task from 'fp-ts/Task';
import * as O from 'fp-ts/Option';
import { pipe } from 'fp-ts/function';
import * as pino from 'pino';
import pino from 'pino';

const logger = pino();
logger.success = logger.info;
Expand Down
4 changes: 2 additions & 2 deletions packages/http/src/forwarder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { hopByHopHeaders } from './resources';
import { createUnauthorisedResponse, createUnprocessableEntityResponse } from '../mocker';
import { ProblemJsonError } from '../types';
import { PROXY_UNSUPPORTED_REQUEST_BODY, UPSTREAM_NOT_IMPLEMENTED } from './errors';
import * as createHttpProxyAgent from 'http-proxy-agent';
import * as createHttpsProxyAgent from 'https-proxy-agent';
import createHttpProxyAgent from 'http-proxy-agent';
import createHttpsProxyAgent from 'https-proxy-agent';
import { toURLSearchParams } from '../utils/url';
import { logRequest, logResponse } from '../utils/logger';
import * as chalk from 'chalk';
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/mocker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
INodeExample,
} from '@stoplight/types';

import * as caseless from 'caseless';
import caseless from 'caseless';
import * as chalk from 'chalk';
import * as E from 'fp-ts/Either';
import * as Record from 'fp-ts/Record';
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/mocker/negotiator/InternalHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IHttpContent, IHttpOperationResponse, IMediaTypeContent } from '@stoplight/types';
// @ts-ignore
import * as accepts from 'accepts';
import accepts from 'accepts';
import * as contentType from 'content-type';
import * as O from 'fp-ts/Option';
import * as A from 'fp-ts/Array';
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/utils/operations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { transformOas3Operations } from '@stoplight/http-spec/oas3/operation';
import { transformOas2Operations } from '@stoplight/http-spec/oas2/operation';
import { transformPostmanCollectionOperations } from '@stoplight/http-spec/postman/operation';
import * as $RefParser from '@stoplight/json-schema-ref-parser';
import $RefParser from '@stoplight/json-schema-ref-parser';
import { HTTPResolverOptions } from '@stoplight/json-schema-ref-parser';
import { bundleTarget, decycle } from '@stoplight/json';
import { IHttpOperation } from '@stoplight/types';
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
IHttpOperationResponse,
IMediaTypeContent,
} from '@stoplight/types';
import * as caseless from 'caseless';
import caseless from 'caseless';
import * as contentType from 'content-type';
import * as A from 'fp-ts/Array';
import * as O from 'fp-ts/Option';
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/validator/validators/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { validateAgainstSchema } from './utils';
import { NonEmptyArray } from 'fp-ts/NonEmptyArray';
import { ValidationContext, validateFn } from './types';
// @ts-ignore no typings
import * as mergeAllOf from '@stoplight/json-schema-merge-allof';
import mergeAllOf from '@stoplight/json-schema-merge-allof';

import { stripReadOnlyProperties, stripWriteOnlyProperties } from '../../utils/filterRequiredProperties';
import { JSONSchema7 } from 'json-schema';
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/validator/validators/headers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpParamStyles, IHttpPathParam } from '@stoplight/types';
import { IHttpNameValue } from '../../types';
import * as MIMEType from 'whatwg-mimetype';
import MIMEType from 'whatwg-mimetype';
import { validateParams } from './params';
import { header } from '../deserializers';
import { ValidationContext } from './types';
Expand Down
Loading
Loading