Skip to content

Commit

Permalink
Merge pull request #76 from nfroidure/feat/dev-reload
Browse files Browse the repository at this point in the history
feat(dev): watch mode and API types
  • Loading branch information
nfroidure authored Aug 9, 2020
2 parents 1d7a9b3 + 86025c7 commit 7726b98
Show file tree
Hide file tree
Showing 34 changed files with 1,001 additions and 383 deletions.
639 changes: 335 additions & 304 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"lerna": "lerna",
"lint": "lerna run lint",
"metapak": "metapak",
"postinstall": "lerna bootstrap --hoist && npm run compile && lerna link",
"postinstall": "lerna bootstrap --hoist && npm run compile && lerna link && npm run lerna -- run types",
"postmetapak": "lerna run metapak",
"precz": "npm t && npm run lint && npm run compile && npm run lerna -- run types && npm run metapak -- -s",
"prettier": "prettier --write 'src/**/*.js'",
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-authorization/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"homepage": "https://github.com/nfroidure/whook",
"dependencies": {
"@whook/whook": "^4.0.4",
"common-services": "^7.1.4",
"common-services": "^7.1.5",
"http-auth-utils": "^2.5.0",
"knifecycle": "^9.1.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"ajv": "^6.12.2",
"bytes": "^3.1.0",
"camelcase": "^6.0.0",
"common-services": "^7.1.4",
"common-services": "^7.1.5",
"cpr": "3.0.1",
"knifecycle": "^9.1.1",
"memfs": "3.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"@whook/whook": "^4.0.4",
"ajv": "^6.12.2",
"camelcase": "^6.0.0",
"common-services": "^7.1.4",
"common-services": "^7.1.5",
"fs-extra": "^9.0.0",
"inquirer": "^7.1.0",
"knifecycle": "^9.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"@whook/cli": "^4.0.4",
"@whook/example": "^4.0.4",
"axios": "^0.19.2",
"common-services": "^7.1.4",
"common-services": "^7.1.5",
"debug": "^4.1.1",
"fs-extra": "^9.0.0",
"inquirer": "^7.1.0",
Expand Down

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions packages/whook-create/src/services/createWhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Mr Bean
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^7.1.4",
"common-services": "^7.1.5",
"http-auth-utils": "^2.5.0",
"jwt-service": "^5.0.0",
"knifecycle": "^9.1.1",
Expand All @@ -215,6 +215,8 @@ Mr Bean
"axios": "^0.19.2",
"babel-eslint": "^10.1.0",
"babel-plugin-knifecycle": "^1.2.0",
"chokidar": "^3.4.1",
"dtsgenerator": "^3.1.1",
"eslint": "^7.0.0",
"eslint-plugin-prettier": "^3.1.3",
"jest": "^26.0.1",
Expand Down Expand Up @@ -302,6 +304,7 @@ Mr Bean
},
"private": true,
"scripts": Object {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=false | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"build": "npm run compile && NODE_ENV=\${NODE_ENV:-development} node bin/build",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
"compile:cjs": "babel --env-name=cjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
Expand All @@ -315,6 +318,7 @@ Mr Bean
"start": "NODE_ENV=\${NODE_ENV:-development} node bin/start",
"test": "npm run jest",
"types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist",
"watch": "NODE_ENV=\${NODE_ENV:-development} DEV_MODE=1 DESTROY_SOCKETS=1 babel-node --extensions '.ts,.js' bin/watch",
"whook": "NODE_ENV=\${NODE_ENV:-development} whook",
"whook-dev": "PROJECT_SRC=\\"$PWD/src\\" NODE_ENV=\${NODE_ENV:-development} babel-node --extensions '.ts,.js' -- node_modules/@whook/cli/bin/whook.js",
},
Expand Down Expand Up @@ -449,7 +453,7 @@ Mr Bean
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^7.1.4",
"common-services": "^7.1.5",
"http-auth-utils": "^2.5.0",
"jwt-service": "^5.0.0",
"knifecycle": "^9.1.1",
Expand All @@ -475,6 +479,8 @@ Mr Bean
"axios": "^0.19.2",
"babel-eslint": "^10.1.0",
"babel-plugin-knifecycle": "^1.2.0",
"chokidar": "^3.4.1",
"dtsgenerator": "^3.1.1",
"eslint": "^7.0.0",
"eslint-plugin-prettier": "^3.1.3",
"jest": "^26.0.1",
Expand Down Expand Up @@ -562,6 +568,7 @@ Mr Bean
},
"private": true,
"scripts": Object {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=false | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"build": "npm run compile && NODE_ENV=\${NODE_ENV:-development} node bin/build",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
"compile:cjs": "babel --env-name=cjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
Expand All @@ -575,6 +582,7 @@ Mr Bean
"start": "NODE_ENV=\${NODE_ENV:-development} node bin/start",
"test": "npm run jest",
"types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist",
"watch": "NODE_ENV=\${NODE_ENV:-development} DEV_MODE=1 DESTROY_SOCKETS=1 babel-node --extensions '.ts,.js' bin/watch",
"whook": "NODE_ENV=\${NODE_ENV:-development} whook",
"whook-dev": "PROJECT_SRC=\\"$PWD/src\\" NODE_ENV=\${NODE_ENV:-development} babel-node --extensions '.ts,.js' -- node_modules/@whook/cli/bin/whook.js",
},
Expand Down Expand Up @@ -694,7 +702,7 @@ Mr Bean
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^7.1.4",
"common-services": "^7.1.5",
"http-auth-utils": "^2.5.0",
"jwt-service": "^5.0.0",
"knifecycle": "^9.1.1",
Expand All @@ -720,6 +728,8 @@ Mr Bean
"axios": "^0.19.2",
"babel-eslint": "^10.1.0",
"babel-plugin-knifecycle": "^1.2.0",
"chokidar": "^3.4.1",
"dtsgenerator": "^3.1.1",
"eslint": "^7.0.0",
"eslint-plugin-prettier": "^3.1.3",
"jest": "^26.0.1",
Expand Down Expand Up @@ -807,6 +817,7 @@ Mr Bean
},
"private": true,
"scripts": Object {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=false | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"build": "npm run compile && NODE_ENV=\${NODE_ENV:-development} node bin/build",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
"compile:cjs": "babel --env-name=cjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
Expand All @@ -820,6 +831,7 @@ Mr Bean
"start": "NODE_ENV=\${NODE_ENV:-development} node bin/start",
"test": "npm run jest",
"types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist",
"watch": "NODE_ENV=\${NODE_ENV:-development} DEV_MODE=1 DESTROY_SOCKETS=1 babel-node --extensions '.ts,.js' bin/watch",
"whook": "NODE_ENV=\${NODE_ENV:-development} whook",
"whook-dev": "PROJECT_SRC=\\"$PWD/src\\" NODE_ENV=\${NODE_ENV:-development} babel-node --extensions '.ts,.js' -- node_modules/@whook/cli/bin/whook.js",
},
Expand Down
11 changes: 10 additions & 1 deletion packages/whook-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ NODE_ENV=production npm start

## Dev

Start the server in development mode:
Start the server in development:
```sh
# Simple dev mode
npm run dev

# Watch mode
npm run watch
```

Create a new endpoint / service / provider or command:
Expand All @@ -41,6 +45,11 @@ List available commands:
npx whook ls
```

Generate API types:
```sh
npm run apitypes
```

## Debug
Execute a handler in isolation:
```sh
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-example/bin/dev.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { runServer } from '../src/index';
const { runServer } = require('../src/index');

runServer();
3 changes: 3 additions & 0 deletions packages/whook-example/bin/watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { watchDevServer } from '../src/watch';

watchDevServer();
6 changes: 5 additions & 1 deletion packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
}
},
"scripts": {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"build": "npm run compile && NODE_ENV=${NODE_ENV:-development} node bin/build",
"cli": "env NODE_ENV=${NODE_ENV:-cli}",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
Expand All @@ -57,6 +58,7 @@
"start": "NODE_ENV=${NODE_ENV:-development} node bin/start",
"test": "npm run jest",
"types": "rimraf -f 'dist/**/*.d.ts' && tsc --project . --declaration --emitDeclarationOnly --outDir dist",
"watch": "NODE_ENV=${NODE_ENV:-development} DEV_MODE=1 DESTROY_SOCKETS=1 babel-node --extensions '.ts,.js' bin/watch",
"whook": "NODE_ENV=${NODE_ENV:-development} whook",
"whook-dev": "PROJECT_SRC=\"$PWD/src\" NODE_ENV=${NODE_ENV:-development} babel-node --extensions '.ts,.js' -- node_modules/@whook/cli/bin/whook.js"
},
Expand All @@ -82,7 +84,7 @@
"@whook/http-transaction": "^4.0.4",
"@whook/swagger-ui": "^4.0.4",
"@whook/whook": "^4.0.4",
"common-services": "^7.1.4",
"common-services": "^7.1.5",
"http-auth-utils": "^2.5.0",
"jwt-service": "^5.0.0",
"knifecycle": "^9.1.1",
Expand All @@ -107,6 +109,8 @@
"axios": "^0.19.2",
"babel-eslint": "^10.1.0",
"babel-plugin-knifecycle": "^1.2.0",
"chokidar": "^3.4.1",
"dtsgenerator": "^3.1.1",
"eslint": "^7.0.0",
"eslint-plugin-prettier": "^3.1.3",
"jest": "^26.0.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/whook-example/src/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Object {
",
"stdout": "
# Provided by \\"@whook/example\\": 1 commands
# Provided by \\"@whook/example\\": 3 commands
- generateOpenAPISchema: Write openAPI schema to stdout
- generateOpenAPITypes: Write openAPI types to stdout
- printEnv: A command printing every env values
Expand Down
94 changes: 94 additions & 0 deletions packages/whook-example/src/commands/generateOpenAPISchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import initGenerateOpenAPISchema from './generateOpenAPISchema';
import { PassThrough } from 'stream';
import type { WhookCommandArgs } from '@whook/cli';

describe('generateOpenAPISchema', () => {
const getOpenAPI = jest.fn();
const log = jest.fn();

beforeEach(() => {
getOpenAPI.mockReset();
log.mockReset();
});

it('should work', async () => {
getOpenAPI.mockResolvedValueOnce({
status: 200,
body: {
openapi: '3.0.2',
info: {
version: '0.0.0',
title: 'api',
description: 'The API',
},
},
});

const outstream = new PassThrough();
const outputPromise = new Promise((resolve, reject) => {
let buffer = Buffer.from('');
outstream.on('data', (aBuffer) => {
buffer = Buffer.concat([buffer, aBuffer]);
});
outstream.once('error', () => reject);
outstream.once('end', () => resolve(buffer.toString()));
});
const generateOpenAPISchema = await initGenerateOpenAPISchema({
log,
getOpenAPI,
outstream,
args: (Object.assign(
{
pretty: true,
},
{
_: ['generateOpenAPISchema'],
},
) as unknown) as WhookCommandArgs,
});
const result = await generateOpenAPISchema();

expect({
result,
output: await outputPromise,
getOpenAPICalls: getOpenAPI.mock.calls,
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
}).toMatchInlineSnapshot(
{},
`
Object {
"getOpenAPICalls": Array [
Array [
Object {
"authenticated": true,
"mutedMethods": Array [
"options",
],
"mutedParameters": Array [],
},
],
],
"logCalls": Array [
Array [
"warning",
"📥 - Retrieving schema...",
],
Array [
"warning",
"📇 - Writing Open API schema...",
],
],
"output": "{
\\"openapi\\": \\"3.0.2\\",
\\"info\\": {
\\"version\\": \\"0.0.0\\",
\\"title\\": \\"api\\",
\\"description\\": \\"The API\\"
}
}",
"result": undefined,
}
`,
);
});
});
66 changes: 66 additions & 0 deletions packages/whook-example/src/commands/generateOpenAPISchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { initGetOpenAPI } from '@whook/swagger-ui';
import { readArgs } from '@whook/cli';
import { autoService, extra } from 'knifecycle';
import type { LogService } from 'common-services';
import type { PromiseValue } from 'type-fest';
import type { WhookCommandDefinition, WhookCommandArgs } from '@whook/cli';

export const definition: WhookCommandDefinition = {
description: 'Write openAPI schema to stdout',
example: `whook generateOpenAPISchema`,
arguments: {
type: 'object',
additionalProperties: false,
required: [],
properties: {
pretty: {
description: 'Option to prettify output',
type: 'boolean',
default: true,
},
authenticated: {
description: 'Option to get the private routes too',
type: 'boolean',
default: true,
},
},
},
};

export default extra(definition, autoService(initGenerateOpenAPISchema));

async function initGenerateOpenAPISchema({
getOpenAPI,
outstream = process.stdout,
args,
log,
}: {
getOpenAPI: PromiseValue<ReturnType<typeof initGetOpenAPI>>;
outstream: NodeJS.WritableStream;
args: WhookCommandArgs;
log: LogService;
}): Promise<() => Promise<void>> {
return async function generateOpenAPISchema() {
const { pretty, authenticated } = readArgs(definition.arguments, args) as {
pretty: boolean;
authenticated: boolean;
};

log('warning', '📥 - Retrieving schema...');
const response = await getOpenAPI({
authenticated,
mutedMethods: ['options'],
mutedParameters: [],
});
log('warning', '📇 - Writing Open API schema...');

await new Promise((resolve, reject) => {
outstream.once('finish', resolve);
outstream.once('error', reject);
outstream.write(
JSON.stringify(response.body, null, pretty ? 2 : undefined),
);
outstream.end();
});
};
}
Loading

0 comments on commit 7726b98

Please sign in to comment.