Add Husky

This commit is contained in:
Vahid Al
2023-12-01 02:17:03 +03:30
parent 069d5a55a7
commit 381fb2b48b
8 changed files with 1693 additions and 31 deletions

23
.editorconfig Normal file
View File

@@ -0,0 +1,23 @@
# EditorConfig is awesome: https://github.com/editorconfig/editorconfig
# Top-most EditorConfig file
root = true
[*]
# Set default charset to utf-8
charset = utf-8
# Set default indentation to spaces
indent_style = space
# Linux-style newlines with a newline ending every file
end_of_line = lf
insert_final_newline = true
# Remove whitespace characters preceding newline characters
trim_trailing_whitespace = true
# Two space indentation for JavaScript files
[*.{js,json}]
indent_size = 2
# Disable trimming trailing whitespaces so that double space newlines work
[*.md]
trim_trailing_whitespace = false

2
.eslintignore Normal file
View File

@@ -0,0 +1,2 @@
*.test.js
_extensions/

24
.eslintrc.js Normal file
View File

@@ -0,0 +1,24 @@
module.exports = {
env: {
node: true,
commonjs: true,
es2021: true,
jest: true,
},
extends: ["eslint:recommended", "prettier"],
overrides: [
{
env: {
node: true,
},
files: [".eslintrc.{js,cjs}"],
parserOptions: {
sourceType: "script",
},
},
],
parserOptions: {
ecmaVersion: "latest",
},
rules: {},
};

5
.husky/pre-commit Executable file
View File

@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm test
npx lint-staged

1598
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -11,7 +11,10 @@
"dev": "npm run swagger-autogen && cross-env NO_CLI=true nodemon src/server.js",
"cli": "nodemon src/server.js --database foobar.db",
"swagger-autogen": "cross-env NO_CLI=true node src/swagger/index.js",
"test": "cross-env NODE_ENV=test NO_CLI=true DB=test.db CORE_PORT=8001 jest --testTimeout=10000"
"test": "cross-env CI=true NODE_ENV=test NO_CLI=true DB=test.db CORE_PORT=8001 jest --testTimeout=10000",
"prepare": "husky install",
"lint": "eslint . --fix --max-warnings=0",
"format": "prettier . --write"
},
"repository": {
"type": "git",
@@ -40,8 +43,12 @@
},
"devDependencies": {
"cross-env": "^7.0.3",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
"husky": "^8.0.3",
"jest": "^29.4.3",
"nodemon": "^2.0.20",
"prettier": "3.1.0",
"supertest": "^6.3.3",
"swagger-autogen": "^2.23.1"
},
@@ -52,5 +59,9 @@
],
"globalSetup": "./src/tests/globalSetup.js",
"globalTeardown": "./src/tests/globalTeardown.js"
},
"lint-staged": {
"*.js": "eslint --fix",
"*.{js,css,md,html,json}": "prettier --write"
}
}

View File

@@ -1,42 +1,40 @@
#! /usr/bin/env node
const express = require('express');
const bodyParser = require('body-parser');
const winston = require('winston');
const expressWinston = require('express-winston');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const swaggerUi = require('swagger-ui-express');
const express = require("express");
const bodyParser = require("body-parser");
const winston = require("winston");
const expressWinston = require("express-winston");
const cors = require("cors");
const rateLimit = require("express-rate-limit");
const swaggerUi = require("swagger-ui-express");
const config = require('./config/index');
const db = require('./db/index');
const rootRoutes = require('./routes/index');
const tablesRoutes = require('./routes/tables');
const rowsRoutes = require('./routes/rows');
const swaggerFile = require('./swagger/swagger.json');
const { setupExtensions } = require('./extensions');
const config = require("./config/index");
const db = require("./db/index");
const rootRoutes = require("./routes/index");
const tablesRoutes = require("./routes/tables");
const rowsRoutes = require("./routes/rows");
const swaggerFile = require("./swagger/swagger.json");
const { setupExtensions } = require("./extensions");
const app = express();
app.get('/health', (req, res) => {
res.send('OK');
app.get("/health", (req, res) => {
res.send("OK");
});
app.use(bodyParser.json());
// Activate wal mode
db.pragma('journal_mode = WAL');
db.exec("PRAGMA journal_mode = WAL");
// Enable CORS
let corsOrigin = config.cors.origin;
if (corsOrigin.includes('*')) {
corsOrigin = '*';
if (corsOrigin.includes("*")) {
corsOrigin = "*";
}
const corsOptions = {
origin: corsOrigin,
};
const corsOptions = { origin: corsOrigin };
app.use(cors(corsOptions));
@@ -47,16 +45,17 @@ if (config.verbose !== null) {
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
winston.format.json(),
),
meta: false,
msg: 'HTTP {{req.method}} {{req.url}}',
msg: "HTTP {{req.method}} {{req.url}}",
expressFormat: true,
colorize: false,
})
}),
);
}
if (config.rateLimit.enabled) {
const limiter = rateLimit({
windowMs: config.rateLimit.windowMs,
@@ -69,10 +68,10 @@ if (config.rateLimit.enabled) {
app.use(limiter);
}
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerFile));
app.use('/api', rootRoutes);
app.use('/api/tables', tablesRoutes);
app.use('/api/tables', rowsRoutes);
app.use("/api/docs", swaggerUi.serve, swaggerUi.setup(swaggerFile));
app.use("/api", rootRoutes);
app.use("/api/tables", tablesRoutes);
app.use("/api/tables", rowsRoutes);
setupExtensions(app, db);

View File