CDK sample project in typescript with eslint prettier and husky dependencies
mkdir cdk-sample && cd my-sample
cdk init app --language=typescript
npm install --save-dev eslint prettier @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-prettier eslint-plugin-prettier
Create a new file named .eslintrc.js in the root of your project, and add the following content:
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:prettier/recommended",
],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
}
Create a new file named .prettierrc in the root of your project and configure Prettier settings:
# Ignore:
node_modules
# ingore build output
cdk.out
ts.out
#Ignore Coverate
coverage
Open your package.json file and add the following scripts:
"lint": "eslint .",
"lint-and-fix": "eslint . --ext .ts --fix",
Now you can run the linting and formatting checks on your CDK project:
npm run lint
npm run lint-and-fix
To ensure code quality, you can add pre-commit hooks to run linting and formatting checks before committing your code. Install the required package:
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,json,ts,tsx}": [
"eslint --fix",
"prettier --write",
"git add"
]
}
Now, every time you try to commit code, ESLint and Prettier will automatically check your files, and if there are any issues, it will be fixed before the commit.