Skip to content

Add typescript source code and eslint #13

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

Merged
merged 1 commit into from
Dec 24, 2020
Merged
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
106 changes: 106 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"rules": {
"curly": [
"error",
"all"
],
"eqeqeq": [
"error",
"smart"
],
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": true,
"optionalDependencies": false,
"peerDependencies": false
}
],
"no-shadow": [
"error",
{
"hoist": "all"
}
],
"prefer-const": "error",
"import/order": [
"error",
{
"groups": [
[
"external",
"builtin"
],
"internal",
[
"parent",
"sibling",
"index"
]
]
}
],
"sort-imports": [
"error",
{
"ignoreCase": true,
"ignoreDeclarationSort": true,
"ignoreMemberSort": false,
"memberSyntaxSortOrder": [
"none",
"all",
"multiple",
"single"
]
}
],
"padding-line-between-statements": [
"error",
{
"blankLine": "always",
"prev": "*",
"next": "return"
}
]
},
"root": true,
"plugins": [
"import"
],
"env": {
"es6": true,
"node": true
},
"overrides": [
{
"files": [
"src/**/*.ts"
],
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier/@typescript-eslint"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json"
},
"rules": {
"@typescript-eslint/prefer-optional-chain": "error",
"no-shadow": "off",
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/prefer-nullish-coalescing": "error",
"@typescript-eslint/strict-boolean-expressions": "error",
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error",
"@typescript-eslint/no-unnecessary-condition": "error",
"@typescript-eslint/no-unnecessary-type-arguments": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/switch-exhaustiveness-check": "error"
}
}
]
}
3 changes: 3 additions & 0 deletions .github/workflows/definitionsBuilder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
- name: Install dependencies
run: npm i

- name: Build plugin
run: npm run build

- id: serverless-version
name: Set Serverless latest version
run: echo "::set-output name=version::$(npm list serverless | grep serverless@ | sed 's/.*serverless@/v/g' | tr -d '[[:space:]]')"
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Tests
on:
push

jobs:
tests:
name: Run serverless/typescript tests
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Install Node.js and npm
uses: actions/setup-node@v1
with:
node-version: 14.x
registry-url: https://registry.npmjs.org

- name: Install dependencies
run: npm i

- name: Run lint tests
run: npm run test:lint

- name: Run type tests
run: npm run test:type
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
node_modules
package-lock.json
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.github
.gitignore
dist
node_modules
package-lock.json
plugin.js
src
serverless.yml
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"version": "2.16.1",
"description": "Serverless typescript definitions",
"main": "index.d.ts",
"scripts": {},
"scripts": {
"build": "tsc",
"lint:fix": "eslint src --fix",
"watch": "tsc -w",
"test:lint": "eslint src",
"test:type": "tsc --noEmit"
},
"repository": {
"type": "git",
"url": "git+https://github.com/serverless/typescript.git"
Expand All @@ -21,7 +27,15 @@
"homepage": "https://github.com/serverless/typescript#readme",
"dependencies": {},
"devDependencies": {
"json-schema-to-typescript": "^9.1.1",
"@types/json-schema": "^7.0.6",
"@typescript-eslint/eslint-plugin": "^4.10.0",
"@typescript-eslint/parser": "^4.10.0",
"eslint": "^7.16.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.0",
"json-schema-to-typescript": "^10.0.2",
"prettier": "^2.2.1",
"serverless": "*",
"typescript": "^4.0.5"
}
Expand Down
62 changes: 0 additions & 62 deletions plugin.js

This file was deleted.

2 changes: 1 addition & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ provider:
name: aws

plugins:
- ./plugin
- ./dist/plugin
71 changes: 71 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { compile } from "json-schema-to-typescript";
import fs from "fs";
import type { JSONSchema4 } from "json-schema";

interface Serverless {
configSchemaHandler: {
schema: JSONSchema4;
};
}

class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
private schema: JSONSchema4;

constructor(serverless: Serverless) {
this.schema = serverless.configSchemaHandler.schema;
}

commands = {
schema: {
usage: "Get JSON schema definition and generate TS definitions",
lifecycleEvents: ["generate"],
},
};

hooks = {
"schema:generate": this.generateSchema.bind(this),
};

async generateSchema() {
/**
* https://github.com/serverless/typescript/issues/4
* JSON Schema v6 `const` keyword converted to `enum`
*/
const normalizedSchema = replaceAllConstForEnum(this.schema);

/**
* ignoreMinAndMaxItems: true -> maxItems: 100 in provider.s3.corsConfiguration definition is generating 100 tuples
*/
const compiledDefinitions = await compile(normalizedSchema, "AWS", {
ignoreMinAndMaxItems: true,
unreachableDefinitions: true,
});
fs.writeFileSync("index.d.ts", compiledDefinitions);
}
}

const replaceAllConstForEnum = (schema: JSONSchema4): JSONSchema4 => {
if ("object" !== typeof schema) {
return schema;
}

return Object.fromEntries(
Object.entries(schema).map(([key, value]) => {
if (key === "const") {
return ["enum", [value]];
}

if (Array.isArray(value)) {
return [key, value.map(replaceAllConstForEnum)];
}

if ("object" === typeof value && value !== null) {
return [key, replaceAllConstForEnum(value)];
}

return [key, value];
})
);
};

module.exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin;
17 changes: 17 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"declaration": false,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist",
"preserveConstEnums": true,
"strict": true,
"skipLibCheck": true,
"target": "ESNext"
},
"include": ["src"],
}