Merge pull request #4116 from mslourens/worker_typescript

Add typescript to worker project
This commit is contained in:
Martin McKeaveney 2022-02-01 10:05:11 +00:00 committed by GitHub
commit 969c36e472
21 changed files with 1969 additions and 2093 deletions

View File

@ -36,10 +36,10 @@
"dev:server": "lerna run --parallel dev:builder --concurrency 1 --scope @budibase/worker --scope @budibase/server", "dev:server": "lerna run --parallel dev:builder --concurrency 1 --scope @budibase/worker --scope @budibase/server",
"test": "lerna run test", "test": "lerna run test",
"lint:eslint": "eslint packages", "lint:eslint": "eslint packages",
"lint:prettier": "prettier --check \"packages/**/*.{js,svelte}\"", "lint:prettier": "prettier --check \"packages/**/*.{js,ts,svelte}\"",
"lint": "yarn run lint:eslint && yarn run lint:prettier", "lint": "yarn run lint:eslint && yarn run lint:prettier",
"lint:fix:eslint": "eslint --fix packages", "lint:fix:eslint": "eslint --fix packages",
"lint:fix:prettier": "prettier --write \"packages/**/*.{js,svelte}\"", "lint:fix:prettier": "prettier --write \"packages/**/*.{js,ts,svelte}\"",
"lint:fix:ts": "lerna run lint:fix", "lint:fix:ts": "lerna run lint:fix",
"lint:fix": "yarn run lint:fix:ts && yarn run lint:fix:prettier && yarn run lint:fix:eslint", "lint:fix": "yarn run lint:fix:ts && yarn run lint:fix:prettier && yarn run lint:fix:eslint",
"test:e2e": "lerna run cy:test", "test:e2e": "lerna run cy:test",

View File

@ -40,7 +40,7 @@ async function run() {
// it will cause environment module to be loaded prematurely // it will cause environment module to be loaded prematurely
const server = require("../../server/dist/app") const server = require("../../server/dist/app")
process.env.PORT = WORKER_PORT process.env.PORT = WORKER_PORT
const worker = require("../../worker/src/index") const worker = require("../../worker/dist/index")
// reload main port for rest of system // reload main port for rest of system
process.env.PORT = MAIN_PORT process.env.PORT = MAIN_PORT
server.on("close", () => console.log("Server Closed")) server.on("close", () => console.log("Server Closed"))

View File

@ -9,7 +9,7 @@ module ArangoMock {
this.close = jest.fn() this.close = jest.fn()
} }
// @ts-ignore // @ts-ignore
arangodb.aql = (strings, ...args) => { arangodb.aql = (strings, ...args) => {
let str = strings.join("{}") let str = strings.join("{}")

View File

@ -1,7 +1,7 @@
module AwsMock { module AwsMock {
const aws: any = {} const aws: any = {}
const response = (body: any) => () => ({promise: () => body}) const response = (body: any) => () => ({ promise: () => body })
function DocumentClient() { function DocumentClient() {
// @ts-ignore // @ts-ignore
@ -39,9 +39,9 @@ module AwsMock {
) )
} }
aws.DynamoDB = {DocumentClient} aws.DynamoDB = { DocumentClient }
aws.S3 = S3 aws.S3 = S3
aws.config = {update: jest.fn()} aws.config = { update: jest.fn() }
module.exports = aws module.exports = aws
} }

View File

@ -5,15 +5,14 @@ module MongoMock {
this.connect = jest.fn() this.connect = jest.fn()
this.close = jest.fn() this.close = jest.fn()
this.insertOne = jest.fn() this.insertOne = jest.fn()
this.insertMany = jest.fn(() => ({toArray: () => []})) this.insertMany = jest.fn(() => ({ toArray: () => [] }))
this.find = jest.fn(() => ({toArray: () => []})) this.find = jest.fn(() => ({ toArray: () => [] }))
this.findOne = jest.fn() this.findOne = jest.fn()
this.count = jest.fn() this.count = jest.fn()
this.deleteOne = jest.fn() this.deleteOne = jest.fn()
this.deleteMany = jest.fn(() => ({toArray: () => []})) this.deleteMany = jest.fn(() => ({ toArray: () => [] }))
this.updateOne = jest.fn() this.updateOne = jest.fn()
this.updateMany = jest.fn(() => ({toArray: () => []})) this.updateMany = jest.fn(() => ({ toArray: () => [] }))
this.collection = jest.fn(() => ({ this.collection = jest.fn(() => ({
insertOne: this.insertOne, insertOne: this.insertOne,

View File

@ -10,15 +10,15 @@ module MsSqlMock {
], ],
})) }))
// mssql.connect = jest.fn(() => ({ recordset: [] })) // mssql.connect = jest.fn(() => ({ recordset: [] }))
mssql.ConnectionPool = jest.fn(() => ({ mssql.ConnectionPool = jest.fn(() => ({
connect: jest.fn(() => ({ connect: jest.fn(() => ({
request: jest.fn(() => ({ request: jest.fn(() => ({
query: jest.fn(sql => ({ recordset: [ sql ] })), query: jest.fn(sql => ({ recordset: [sql] })),
})), })),
})), })),
})) }))
module.exports = mssql module.exports = mssql
} }

View File

@ -62,7 +62,8 @@ module FetchMock {
return json({ return json({
url, url,
opts, opts,
value: "<!doctype html><html itemscope=\"\" itemtype=\"http://schema.org/WebPage\" lang=\"en-GB\"></html>", value:
'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-GB"></html>',
}) })
} else if (url.includes("failonce.com")) { } else if (url.includes("failonce.com")) {
failCount++ failCount++

View File

@ -10,16 +10,14 @@ module PgMock {
], ],
})) }))
// constructor // constructor
function Client() { function Client() {}
}
Client.prototype.query = query Client.prototype.query = query
Client.prototype.connect = jest.fn() Client.prototype.connect = jest.fn()
Client.prototype.release = jest.fn() Client.prototype.release = jest.fn()
function Pool() { function Pool() {}
}
const on = jest.fn() const on = jest.fn()
Pool.prototype.query = query Pool.prototype.query = query

View File

@ -30,4 +30,4 @@ process.env.ALLOW_DEV_AUTOMATIONS = "1"
const server = require("../src/app") const server = require("../src/app")
process.env.PORT = WORKER_PORT process.env.PORT = WORKER_PORT
const worker = require("../../worker/src/index") const worker = require("../../worker/src/index")
process.env.PORT = MAIN_PORT process.env.PORT = MAIN_PORT

File diff suppressed because it is too large Load Diff

View File

@ -3,10 +3,11 @@
"emit": true, "emit": true,
"key": true "key": true
}, },
"parser": "@typescript-eslint/parser",
"env": { "env": {
"node": true "node": true
}, },
"extends": ["eslint:recommended"], "extends": ["eslint:recommended"],
"rules": { "rules": {
} }
} }

View File

@ -1,3 +1,4 @@
node_modules/ node_modules/
.env .env
watchtower-hook.json watchtower-hook.json
dist/

View File

@ -1,3 +1,6 @@
{ {
"watch": ["src", "../backend-core"] "watch": ["src", "../backend-core"],
} "ext": "js,ts,json",
"ignore": ["src/**/*.spec.ts", "src/**/*.spec.js"],
"exec": "ts-node src/index.ts"
}

View File

@ -3,7 +3,7 @@
"email": "hi@budibase.com", "email": "hi@budibase.com",
"version": "1.0.49-alpha.4", "version": "1.0.49-alpha.4",
"description": "Budibase background service", "description": "Budibase background service",
"main": "src/index.js", "main": "src/index.ts",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/Budibase/budibase.git" "url": "https://github.com/Budibase/budibase.git"
@ -12,10 +12,15 @@
"budibase" "budibase"
], ],
"scripts": { "scripts": {
"run:docker": "node src/index.js", "build": "rimraf dist/ && tsc",
"postbuild": "copyfiles -u 1 src/**/*.hbs dist/",
"run:docker": "node dist/index.js",
"build:docker": "docker build . -t worker-service --label version=$BUDIBASE_RELEASE_VERSION", "build:docker": "docker build . -t worker-service --label version=$BUDIBASE_RELEASE_VERSION",
"dev:stack:init": "node ./scripts/dev/manage.js init", "dev:stack:init": "node ./scripts/dev/manage.js init",
"dev:builder": "npm run dev:stack:init && nodemon", "dev:builder": "npm run dev:stack:init && nodemon",
"format": "prettier --config ../../.prettierrc.json 'src/**/*.ts' --write",
"lint": "eslint --fix src/",
"lint:fix": "yarn run format && yarn run lint",
"test": "jest --runInBand", "test": "jest --runInBand",
"env:multi:enable": "node scripts/multiTenancy.js enable", "env:multi:enable": "node scripts/multiTenancy.js enable",
"env:multi:disable": "node scripts/multiTenancy.js disable", "env:multi:disable": "node scripts/multiTenancy.js disable",
@ -58,13 +63,26 @@
"server-destroy": "^1.0.1" "server-destroy": "^1.0.1"
}, },
"devDependencies": { "devDependencies": {
"jest": "^26.6.3", "@types/jest": "^26.0.23",
"@types/koa": "^2.13.3",
"@types/koa-router": "^7.4.2",
"@types/node": "^15.12.4",
"@typescript-eslint/parser": "4.28.0",
"copyfiles": "^2.4.1",
"eslint": "^6.8.0",
"jest": "^27.0.5",
"nodemon": "^2.0.7", "nodemon": "^2.0.7",
"pouchdb-adapter-memory": "^7.2.2", "pouchdb-adapter-memory": "^7.2.2",
"prettier": "2.3.1",
"rimraf": "^3.0.2",
"supertest": "^6.1.3", "supertest": "^6.1.3",
"ts-jest": "^27.0.3",
"ts-node": "^10.0.0",
"typescript": "4.3.5",
"update-dotenv": "^1.1.1" "update-dotenv": "^1.1.1"
}, },
"jest": { "jest": {
"preset": "ts-jest",
"testEnvironment": "node", "testEnvironment": "node",
"setupFiles": [ "setupFiles": [
"./scripts/jestSetup.js" "./scripts/jestSetup.js"

View File

@ -1,10 +1,9 @@
jest.mock("nodemailer")
const setup = require("./utilities") const setup = require("./utilities")
const sendMailMock = setup.emailMock()
const TENANT_ID = "default" const TENANT_ID = "default"
jest.mock("nodemailer")
const sendMailMock = setup.emailMock()
describe("/api/global/auth", () => { describe("/api/global/auth", () => {
let request = setup.getRequest() let request = setup.getRequest()
let config = setup.getConfig() let config = setup.getConfig()

View File

@ -1,11 +1,7 @@
const setup = require("./utilities")
// mock the email system // mock the email system
jest.mock("nodemailer") jest.mock("nodemailer")
const nodemailer = require("nodemailer") const setup = require("./utilities")
nodemailer.createTransport.mockReturnValue({ setup.emailMock()
verify: jest.fn(),
})
describe("/api/global/configs/checklist", () => { describe("/api/global/configs/checklist", () => {
let request = setup.getRequest() let request = setup.getRequest()

View File

@ -1,16 +1,10 @@
jest.mock("nodemailer")
const setup = require("./utilities") const setup = require("./utilities")
const sendMailMock = setup.emailMock()
const { EmailTemplatePurpose } = require("../../../constants") const { EmailTemplatePurpose } = require("../../../constants")
const { TENANT_ID } = require("./utilities/structures") const { TENANT_ID } = require("./utilities/structures")
// mock the email system
const sendMailMock = jest.fn()
jest.mock("nodemailer")
const nodemailer = require("nodemailer")
nodemailer.createTransport.mockReturnValue({
sendMail: sendMailMock,
verify: jest.fn(),
})
describe("/api/global/email", () => { describe("/api/global/email", () => {
let request = setup.getRequest() let request = setup.getRequest()
let config = setup.getConfig() let config = setup.getConfig()

View File

@ -1,7 +1,5 @@
const setup = require("./utilities")
const { TENANT_ID } = require("./utilities/structures")
jest.mock("nodemailer") jest.mock("nodemailer")
const setup = require("./utilities")
const sendMailMock = setup.emailMock() const sendMailMock = setup.emailMock()
describe("/api/global/users", () => { describe("/api/global/users", () => {

View File

@ -1,4 +1,8 @@
// need to load environment first // need to load environment first
import { Scope } from "@sentry/node"
import { Event } from "@sentry/types/dist/event"
import Application from "koa"
const env = require("./environment") const env = require("./environment")
const CouchDB = require("./db") const CouchDB = require("./db")
require("@budibase/backend-core").init(CouchDB) require("@budibase/backend-core").init(CouchDB)
@ -13,7 +17,7 @@ const api = require("./api")
const redis = require("./utilities/redis") const redis = require("./utilities/redis")
const Sentry = require("@sentry/node") const Sentry = require("@sentry/node")
const app = new Koa() const app: Application = new Koa()
app.keys = ["secret", "key"] app.keys = ["secret", "key"]
@ -42,8 +46,8 @@ if (env.isProd()) {
Sentry.init() Sentry.init()
app.on("error", (err, ctx) => { app.on("error", (err, ctx) => {
Sentry.withScope(function (scope) { Sentry.withScope(function (scope: Scope) {
scope.addEventProcessor(function (event) { scope.addEventProcessor(function (event: Event) {
return Sentry.Handlers.parseRequest(event, ctx.request) return Sentry.Handlers.parseRequest(event, ctx.request)
}) })
Sentry.captureException(err) Sentry.captureException(err)

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es2019"],
"allowJs": true,
"outDir": "dist",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"incremental": true
},
"include": [
"./src/**/*"
],
"exclude": [
"node_modules",
"**/*.json",
"**/*.spec.ts",
"**/*.spec.js"
]
}

File diff suppressed because it is too large Load Diff