Skip to content

Commit 3d43d14

Browse files
Add typescript source code and eslint (#13)
1 parent b40c6ea commit 3d43d14

File tree

10 files changed

+243
-66
lines changed

10 files changed

+243
-66
lines changed

.eslintrc.json

Lines changed: 106 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 26 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
dist
12
node_modules
23
package-lock.json

.npmignore

Lines changed: 2 additions & 1 deletion
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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
"version": "2.16.1",
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,15 @@
2127
"homepage": "https://github.com/serverless/typescript#readme",
2228
"dependencies": {},
2329
"devDependencies": {
24-
"json-schema-to-typescript": "^9.1.1",
30+
"@types/json-schema": "^7.0.6",
31+
"@typescript-eslint/eslint-plugin": "^4.10.0",
32+
"@typescript-eslint/parser": "^4.10.0",
33+
"eslint": "^7.16.0",
34+
"eslint-config-prettier": "^7.1.0",
35+
"eslint-plugin-import": "^2.22.1",
36+
"eslint-plugin-prettier": "^3.3.0",
37+
"json-schema-to-typescript": "^10.0.2",
38+
"prettier": "^2.2.1",
2539
"serverless": "*",
2640
"typescript": "^4.0.5"
2741
}

plugin.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

serverless.yml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { compile } from "json-schema-to-typescript";
2+
import fs from "fs";
3+
import type { JSONSchema4 } from "json-schema";
4+
5+
interface Serverless {
6+
configSchemaHandler: {
7+
schema: JSONSchema4;
8+
};
9+
}
10+
11+
class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
12+
private schema: JSONSchema4;
13+
14+
constructor(serverless: Serverless) {
15+
this.schema = serverless.configSchemaHandler.schema;
16+
}
17+
18+
commands = {
19+
schema: {
20+
usage: "Get JSON schema definition and generate TS definitions",
21+
lifecycleEvents: ["generate"],
22+
},
23+
};
24+
25+
hooks = {
26+
"schema:generate": this.generateSchema.bind(this),
27+
};
28+
29+
async generateSchema() {
30+
/**
31+
* https://github.com/serverless/typescript/issues/4
32+
* JSON Schema v6 `const` keyword converted to `enum`
33+
*/
34+
const normalizedSchema = replaceAllConstForEnum(this.schema);
35+
36+
/**
37+
* ignoreMinAndMaxItems: true -> maxItems: 100 in provider.s3.corsConfiguration definition is generating 100 tuples
38+
*/
39+
const compiledDefinitions = await compile(normalizedSchema, "AWS", {
40+
ignoreMinAndMaxItems: true,
41+
unreachableDefinitions: true,
42+
});
43+
fs.writeFileSync("index.d.ts", compiledDefinitions);
44+
}
45+
}
46+
47+
const replaceAllConstForEnum = (schema: JSONSchema4): JSONSchema4 => {
48+
if ("object" !== typeof schema) {
49+
return schema;
50+
}
51+
52+
return Object.fromEntries(
53+
Object.entries(schema).map(([key, value]) => {
54+
if (key === "const") {
55+
return ["enum", [value]];
56+
}
57+
58+
if (Array.isArray(value)) {
59+
return [key, value.map(replaceAllConstForEnum)];
60+
}
61+
62+
if ("object" === typeof value && value !== null) {
63+
return [key, replaceAllConstForEnum(value)];
64+
}
65+
66+
return [key, value];
67+
})
68+
);
69+
};
70+
71+
module.exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin;

tsconfig.json

Lines changed: 17 additions & 0 deletions
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": "ESNext"
15+
},
16+
"include": ["src"],
17+
}

0 commit comments

Comments
 (0)