Skip to content

Commit 92fe40d

Browse files
Add quicktype, typescript source code and eslint
1 parent 9899a52 commit 92fe40d

10 files changed

+220
-66
lines changed

.eslintrc.json

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"extends": [
3+
"eslint:recommended",
4+
"plugin:prettier/recommended"
5+
],
6+
"rules": {
7+
"curly": [
8+
"error",
9+
"all"
10+
],
11+
"eqeqeq": [
12+
"error",
13+
"smart"
14+
],
15+
"import/no-extraneous-dependencies": [
16+
"error",
17+
{
18+
"devDependencies": true,
19+
"optionalDependencies": false,
20+
"peerDependencies": false
21+
}
22+
],
23+
"no-shadow": [
24+
"error",
25+
{
26+
"hoist": "all"
27+
}
28+
],
29+
"prefer-const": "error",
30+
"import/order": [
31+
"error",
32+
{
33+
"groups": [
34+
[
35+
"external",
36+
"builtin"
37+
],
38+
"internal",
39+
[
40+
"parent",
41+
"sibling",
42+
"index"
43+
]
44+
]
45+
}
46+
],
47+
"sort-imports": [
48+
"error",
49+
{
50+
"ignoreCase": true,
51+
"ignoreDeclarationSort": true,
52+
"ignoreMemberSort": false,
53+
"memberSyntaxSortOrder": [
54+
"none",
55+
"all",
56+
"multiple",
57+
"single"
58+
]
59+
}
60+
],
61+
"padding-line-between-statements": [
62+
"error",
63+
{
64+
"blankLine": "always",
65+
"prev": "*",
66+
"next": "return"
67+
}
68+
]
69+
},
70+
"root": true,
71+
"plugins": [
72+
"import"
73+
],
74+
"env": {
75+
"es6": true,
76+
"node": true
77+
},
78+
"overrides": [
79+
{
80+
"files": [
81+
"src/**/*.ts"
82+
],
83+
"extends": [
84+
"plugin:@typescript-eslint/recommended",
85+
"plugin:@typescript-eslint/recommended-requiring-type-checking",
86+
"prettier/@typescript-eslint"
87+
],
88+
"parser": "@typescript-eslint/parser",
89+
"parserOptions": {
90+
"project": "tsconfig.json"
91+
},
92+
"rules": {
93+
"@typescript-eslint/prefer-optional-chain": "error",
94+
"no-shadow": "off",
95+
"@typescript-eslint/no-shadow": "error",
96+
"@typescript-eslint/prefer-nullish-coalescing": "error",
97+
"@typescript-eslint/strict-boolean-expressions": "error",
98+
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error",
99+
"@typescript-eslint/no-unnecessary-condition": "error",
100+
"@typescript-eslint/no-unnecessary-type-arguments": "error",
101+
"@typescript-eslint/prefer-string-starts-ends-with": "error",
102+
"@typescript-eslint/switch-exhaustiveness-check": "error"
103+
}
104+
}
105+
]
106+
}

.github/workflows/definitionsBuilder.yml

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ jobs:
2020
- name: Install dependencies
2121
run: npm i
2222

23+
- name: Build plugin
24+
run: npm run build
25+
2326
- id: serverless-version
2427
name: Set Serverless latest version
2528
run: echo "::set-output name=version::$(npm list serverless | grep serverless@ | sed 's/.*serverless@/v/g' | tr -d '[[:space:]]')"

.github/workflows/test.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Tests
2+
on:
3+
push
4+
5+
jobs:
6+
tests:
7+
name: Run serverless/typescript tests
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout repository
11+
uses: actions/checkout@v2
12+
13+
- name: Install Node.js and npm
14+
uses: actions/setup-node@v1
15+
with:
16+
node-version: 14.x
17+
registry-url: https://registry.npmjs.org
18+
19+
- name: Install dependencies
20+
run: npm i
21+
22+
- name: Run lint tests
23+
run: npm run test:lint
24+
25+
- name: Run type tests
26+
run: npm run test:type

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
dist
12
node_modules
23
package-lock.json

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.github
22
.gitignore
3+
dist
34
node_modules
45
package-lock.json
5-
plugin.js
6+
src
67
serverless.yml

package.json

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
"version": "2.15.0",
44
"description": "Serverless typescript definitions",
55
"main": "index.d.ts",
6-
"scripts": {},
6+
"scripts": {
7+
"build": "tsc",
8+
"lint:fix": "eslint src --fix",
9+
"watch": "tsc -w",
10+
"test:lint": "eslint src",
11+
"test:type": "tsc --noEmit"
12+
},
713
"repository": {
814
"type": "git",
915
"url": "git+https://github.com/serverless/typescript.git"
@@ -21,7 +27,14 @@
2127
"homepage": "https://github.com/serverless/typescript#readme",
2228
"dependencies": {},
2329
"devDependencies": {
24-
"json-schema-to-typescript": "^9.1.1",
30+
"@typescript-eslint/eslint-plugin": "^4.10.0",
31+
"@typescript-eslint/parser": "^4.10.0",
32+
"eslint": "^7.16.0",
33+
"eslint-config-prettier": "^7.1.0",
34+
"eslint-plugin-import": "^2.22.1",
35+
"eslint-plugin-prettier": "^3.3.0",
36+
"prettier": "^2.2.1",
37+
"quicktype-core": "^6.0.69",
2538
"serverless": "*",
2639
"typescript": "^4.0.5"
2740
}

plugin.js

-62
This file was deleted.

serverless.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ provider:
44
name: aws
55

66
plugins:
7-
- ./plugin
7+
- ./dist/plugin

src/plugin.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { InputData, JSONSchemaInput, quicktype } from "quicktype-core";
2+
import { writeFileSync } from "fs";
3+
4+
interface Serverless {
5+
configSchemaHandler: {
6+
schema: Record<string, string>;
7+
};
8+
}
9+
10+
class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
11+
private schema: Record<string, string>;
12+
13+
constructor(serverless: Serverless) {
14+
this.schema = serverless.configSchemaHandler.schema;
15+
}
16+
17+
commands = {
18+
schema: {
19+
usage: "Get JSON schema definition and generate TS definitions",
20+
lifecycleEvents: ["generate"],
21+
},
22+
};
23+
24+
hooks = {
25+
"schema:generate": this.generateSchema.bind(this),
26+
};
27+
28+
async generateSchema() {
29+
const schemaInput = new JSONSchemaInput(undefined);
30+
await schemaInput.addSource({
31+
name: "AWS",
32+
schema: JSON.stringify(this.schema),
33+
});
34+
const inputData = new InputData();
35+
inputData.addInput(schemaInput);
36+
37+
const { lines: serverlessTs } = await quicktype({
38+
inputData,
39+
lang: "typescript",
40+
rendererOptions: {
41+
"just-types": "true",
42+
},
43+
});
44+
45+
writeFileSync("index.d.ts", serverlessTs.join("\n"));
46+
}
47+
}
48+
49+
module.exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin;

tsconfig.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": false,
4+
"esModuleInterop": true,
5+
"module": "commonjs",
6+
"moduleResolution": "node",
7+
"noFallthroughCasesInSwitch": true,
8+
"noUnusedLocals": true,
9+
"noUnusedParameters": true,
10+
"outDir": "dist",
11+
"preserveConstEnums": true,
12+
"strict": true,
13+
"skipLibCheck": true,
14+
"target": "es6"
15+
},
16+
"include": ["src"],
17+
}

0 commit comments

Comments
 (0)