Skip to content

Commit

Permalink
Add quicktype, typescript source code and eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbarthelet committed Dec 20, 2020
1 parent 9899a52 commit 92fe40d
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 66 deletions.
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
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"version": "2.15.0",
"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,14 @@
"homepage": "https://github.com/serverless/typescript#readme",
"dependencies": {},
"devDependencies": {
"json-schema-to-typescript": "^9.1.1",
"@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",
"prettier": "^2.2.1",
"quicktype-core": "^6.0.69",
"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
49 changes: 49 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { InputData, JSONSchemaInput, quicktype } from "quicktype-core";
import { writeFileSync } from "fs";

interface Serverless {
configSchemaHandler: {
schema: Record<string, string>;
};
}

class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
private schema: Record<string, string>;

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() {
const schemaInput = new JSONSchemaInput(undefined);
await schemaInput.addSource({
name: "AWS",
schema: JSON.stringify(this.schema),
});
const inputData = new InputData();
inputData.addInput(schemaInput);

const { lines: serverlessTs } = await quicktype({
inputData,
lang: "typescript",
rendererOptions: {
"just-types": "true",
},
});

writeFileSync("index.d.ts", serverlessTs.join("\n"));
}
}

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": "es6"
},
"include": ["src"],
}

0 comments on commit 92fe40d

Please sign in to comment.