Skip to content

Commit

Permalink
Merge pull request #1195 from serlo/fix-start-command
Browse files Browse the repository at this point in the history
chore: Use esbuild for start command
  • Loading branch information
hugotiburtino authored Dec 12, 2023
2 parents 3bff307 + 7eb2923 commit ca9a8e0
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REDIS_URL=redis://127.0.0.1:6379
ROCKET_CHAT_API_USER_ID=an-user-id
ROCKET_CHAT_API_AUTH_TOKEN=an-auth-token
ROCKET_CHAT_URL=https://community.serlo.org/
SERLO_ORG_DATABASE_LAYER_HOST=localhost:8080
SERLO_ORG_DATABASE_LAYER_HOST=127.0.0.1:8080
SERLO_ORG_SECRET=serlo.org-secret
CACHE_TYPE=empty

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"_prettier": "prettier .",
"auth": "scripts/authenticate.sh",
"_build": "node --loader ts-node/esm --experimental-specifier-resolution=node scripts/build",
"_start": "node --loader ts-node/esm --experimental-specifier-resolution=node scripts/start",
"build:server": "yarn _build packages/server/src/server.ts server.cjs",
"build:swr-queue-worker": "yarn _build packages/server/src/swr-queue-worker.ts swr-queue-worker.cjs",
"build:packages": "yarn lerna run build",
Expand Down Expand Up @@ -52,11 +53,11 @@
"redis:cli": "docker compose exec redis redis-cli",
"redis:empty": "docker compose exec redis redis-cli FLUSHDB",
"redis:list": "docker compose exec redis redis-cli KEYS '*'",
"start": "npm-run-all start:redis start:server",
"start": "run-s start:redis start:server",
"start:enmeshed": "docker-compose -f enmeshed/docker-compose.yml up -d",
"start:redis": "docker compose up --detach",
"start:server": "lerna run --stream --scope @serlo/api.serlo.org start",
"start:swr-queue-worker": "lerna run --stream --scope @serlo/api.serlo.org start:swr-queue-worker",
"start:server": "yarn _start packages/server/src/server.ts server.cjs",
"start:swr-queue-worker": "yarn _start packages/server/src/swr-queue-worker.ts swr-queue-worker.cjs",
"stop": "docker compose stop",
"stop:enmeshed": "docker-compose -f enmeshed/docker-compose.yml down",
"stop:redis": "docker-compose stop",
Expand Down
13 changes: 0 additions & 13 deletions packages/server/.babelrc

This file was deleted.

8 changes: 1 addition & 7 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
"build:server": "yarn _build --format cjs --target node --entry src/server.ts",
"build:swr-queue-worker": "yarn _build --format cjs --target node --entry src/swr-queue-worker.ts",
"codegen": "graphql-codegen --config codegen.yml",
"deploy:image": "ts-node --experimental-specifier-resolution=node scripts/deploy",
"prestart": "lerna run build",
"start": "tsdx watch --transpileOnly --format cjs --target node --entry src/server.ts --tsconfig tsconfig.prod.json --onSuccess \"node dist\"",
"start:swr-queue-worker": "tsdx watch --transpileOnly --format cjs --target node --entry src/swr-queue-worker.ts --tsconfig tsconfig.prod.json --onSuccess \"node dist\""
"deploy:image": "ts-node --experimental-specifier-resolution=node scripts/deploy"
},
"dependencies": {
"bee-queue": "^1.7.1"
Expand All @@ -41,10 +38,7 @@
"@types/redlock": "^4.0.7",
"@types/semver": "^7.5.6",
"@types/uuid": "^9.0.7",
"@zerollup/ts-transform-paths": "^1.7.18",
"apollo-datasource-rest": "^3.7.0",
"babel-plugin-import-graphql": "^2.8.1",
"babel-plugin-module-resolver": "^5.0.0",
"basic-auth": "^2.0.1",
"bull-arena": "^4.1.0",
"default-import": "^1.1.5",
Expand Down
8 changes: 3 additions & 5 deletions packages/server/src/internals/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dotenv from 'dotenv'
import createApp from 'express'
import path from 'path'

import { applyGraphQLMiddleware } from './graphql-middleware'
import { applySwrQueueDashboardMiddleware } from './swr-queue-dashboard-middleware'
Expand All @@ -15,9 +14,8 @@ import { applyKratosMiddleware } from '~/internals/server/kratos-middleware'
export { getGraphQLOptions } from './graphql-middleware'

export async function start() {
dotenv.config({
path: path.join(__dirname, '..', '..', '..', '.env'),
})
dotenv.config()

initializeSentry({ context: 'server' })
const timer = createTimer()
const cache =
Expand Down Expand Up @@ -53,7 +51,7 @@ async function initializeServer({
})
const enmeshedPath = applyEnmeshedMiddleware({ app, cache })

app.get(healthPath, (req, res) => {
app.get(healthPath, (_req, res) => {
res.status(200).send('Okay!')
})

Expand Down
5 changes: 1 addition & 4 deletions packages/server/src/internals/swr-queue-worker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import dotenv from 'dotenv'
import createApp from 'express'
import path from 'path'

import { createCache } from './cache'
import { initializeSentry } from './sentry'
import { createSwrQueueWorker } from './swr-queue'
import { createTimer } from './timer'

export async function start() {
dotenv.config({
path: path.join(__dirname, '..', '..', '..', '.env'),
})
dotenv.config()
initializeSentry({ context: 'swr-queue-worker' })
const timer = createTimer()
const cache = createCache({ timer })
Expand Down
21 changes: 15 additions & 6 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import * as path from 'path'
import { fileURLToPath } from 'url'

const graphqlLoaderPlugin = defaultImport(graphqlLoaderPlugin_)
const __dirname = path.dirname(fileURLToPath(import.meta.url))

const root = path.join(__dirname, '..')
const dist = path.join(root, 'dist')

await main()

async function main() {
const { source, outfile } = loadSourceAndOutput()

await esbuild.build(getEsbuildOptions(source, outfile))
}

export function loadSourceAndOutput() {
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const root = path.join(__dirname, '..')
const dist = path.join(root, 'dist')
const [sourceArg, targetArg] = process.argv.slice(2)

const source = path.resolve(sourceArg)
Expand All @@ -24,7 +29,11 @@ async function main() {

const outfile = path.join(dist, targetArg)

await esbuild.build({
return { source, outfile }
}

export function getEsbuildOptions(source: string, outfile: string) {
return {
entryPoints: [source],
treeShaking: true,
minifySyntax: false,
Expand All @@ -41,5 +50,5 @@ async function main() {
external: ['bee-queue'],
outfile,
plugins: [graphqlLoaderPlugin()],
})
} as esbuild.BuildOptions
}
44 changes: 44 additions & 0 deletions scripts/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { spawn, ChildProcess } from 'child_process'
import * as esbuild from 'esbuild'

import { getEsbuildOptions, loadSourceAndOutput } from './build'

await main()

async function main() {
const { source, outfile } = loadSourceAndOutput()

let serverProcess: ChildProcess | null = null

const startServerPlugin: esbuild.Plugin = {
name: 'startServer',
setup(build) {
build.onStart(() => {
// eslint-disable-next-line no-console
console.info('\nINFO: (Re)start of process...')
})

build.onEnd(({ errors }) => {
if (errors.length > 0) {
// eslint-disable-next-line no-console
console.info('INFO: Process will not be (re)started due to error.')

return
}

if (serverProcess) serverProcess.kill()

serverProcess = spawn('node', [outfile], { stdio: 'inherit' })
})
},
}

const { plugins = [], ...options } = getEsbuildOptions(source, outfile)

const ctx = await esbuild.context({
...options,
plugins: [...plugins, startServerPlugin],
})

await ctx.watch({})
}
86 changes: 5 additions & 81 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4105,10 +4105,7 @@ __metadata:
"@types/redlock": ^4.0.7
"@types/semver": ^7.5.6
"@types/uuid": ^9.0.7
"@zerollup/ts-transform-paths": ^1.7.18
apollo-datasource-rest: ^3.7.0
babel-plugin-import-graphql: ^2.8.1
babel-plugin-module-resolver: ^5.0.0
basic-auth: ^2.0.1
bee-queue: ^1.7.1
bull-arena: ^4.1.0
Expand Down Expand Up @@ -5204,28 +5201,6 @@ __metadata:
languageName: node
linkType: hard

"@zerollup/ts-helpers@npm:^1.7.18":
version: 1.7.18
resolution: "@zerollup/ts-helpers@npm:1.7.18"
dependencies:
resolve: ^1.12.0
peerDependencies:
typescript: ">=3.7.2"
checksum: 6ad5cc350139e28866ecff6138b38771a385b8f6c66af07bfff5f7a6d54a9071c06cdd43e0ae0e71157cce2815e6207c0ce15a28ef862d87fe2ad8381b0465dd
languageName: node
linkType: hard

"@zerollup/ts-transform-paths@npm:^1.7.18":
version: 1.7.18
resolution: "@zerollup/ts-transform-paths@npm:1.7.18"
dependencies:
"@zerollup/ts-helpers": ^1.7.18
peerDependencies:
typescript: ">=3.7.2"
checksum: 2a3b258222c954e6cbcb85fdbb8fe0a8781128015951d352a42fe4b59379b7051397592c9be0dd96d15fc8c6f2916b2805cd85adff93aaa3b9346beaf144a0dd
languageName: node
linkType: hard

"@zkochan/js-yaml@npm:0.0.6":
version: 0.0.6
resolution: "@zkochan/js-yaml@npm:0.0.6"
Expand Down Expand Up @@ -6038,18 +6013,6 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-import-graphql@npm:^2.8.1":
version: 2.8.1
resolution: "babel-plugin-import-graphql@npm:2.8.1"
dependencies:
graphql-tag: ^2.9.2
peerDependencies:
graphql: ">=0.9.6"
graphql-tag: ^2.1.0
checksum: 8e156d514c785f49df8d4934276104a48b4e6c09da70641eca1b439ae9eeff13b0a98cdfb869752762bc8719084ccdf8cc1960a3b084ca66a740ad09010ccbfa
languageName: node
linkType: hard

"babel-plugin-istanbul@npm:^6.0.0, babel-plugin-istanbul@npm:^6.1.1":
version: 6.1.1
resolution: "babel-plugin-istanbul@npm:6.1.1"
Expand Down Expand Up @@ -6097,19 +6060,6 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-module-resolver@npm:^5.0.0":
version: 5.0.0
resolution: "babel-plugin-module-resolver@npm:5.0.0"
dependencies:
find-babel-config: ^2.0.0
glob: ^8.0.3
pkg-up: ^3.1.0
reselect: ^4.1.7
resolve: ^1.22.1
checksum: d6880e49fc8e7bac509a2c183b4303ee054a47a80032a59a6f7844bb468ebe5e333b5dc5378443afdab5839e2da2b31a6c8d9a985a0047cd076b82bb9161cc78
languageName: node
linkType: hard

"babel-plugin-polyfill-corejs2@npm:^0.3.0":
version: 0.3.0
resolution: "babel-plugin-polyfill-corejs2@npm:0.3.0"
Expand Down Expand Up @@ -9709,16 +9659,6 @@ __metadata:
languageName: node
linkType: hard

"find-babel-config@npm:^2.0.0":
version: 2.0.0
resolution: "find-babel-config@npm:2.0.0"
dependencies:
json5: ^2.1.1
path-exists: ^4.0.0
checksum: d110308b02fe6a6411a0cfb7fd50af6740fbf5093eada3d6ddacf99b07fc8eea4aa3475356484710a0032433029a21ce733bb3ef88fda1d6e35c29a3e4983014
languageName: node
linkType: hard

"find-cache-dir@npm:^3.3.1":
version: 3.3.2
resolution: "find-cache-dir@npm:3.3.2"
Expand Down Expand Up @@ -10379,7 +10319,7 @@ __metadata:
languageName: node
linkType: hard

"glob@npm:^8.0.0, glob@npm:^8.0.1, glob@npm:^8.0.3":
"glob@npm:^8.0.0, glob@npm:^8.0.1":
version: 8.1.0
resolution: "glob@npm:8.1.0"
dependencies:
Expand Down Expand Up @@ -10598,7 +10538,7 @@ __metadata:
languageName: node
linkType: hard

"graphql-tag@npm:^2.11.0, graphql-tag@npm:^2.12.6, graphql-tag@npm:^2.9.1, graphql-tag@npm:^2.9.2":
"graphql-tag@npm:^2.11.0, graphql-tag@npm:^2.12.6, graphql-tag@npm:^2.9.1":
version: 2.12.6
resolution: "graphql-tag@npm:2.12.6"
dependencies:
Expand Down Expand Up @@ -13233,7 +13173,7 @@ __metadata:
languageName: node
linkType: hard

"json5@npm:2.x, json5@npm:^2.1.1, json5@npm:^2.2.2, json5@npm:^2.2.3":
"json5@npm:2.x, json5@npm:^2.2.2, json5@npm:^2.2.3":
version: 2.2.3
resolution: "json5@npm:2.2.3"
bin:
Expand Down Expand Up @@ -16670,15 +16610,6 @@ __metadata:
languageName: node
linkType: hard

"pkg-up@npm:^3.1.0":
version: 3.1.0
resolution: "pkg-up@npm:3.1.0"
dependencies:
find-up: ^3.0.0
checksum: 5bac346b7c7c903613c057ae3ab722f320716199d753f4a7d053d38f2b5955460f3e6ab73b4762c62fd3e947f58e04f1343e92089e7bb6091c90877406fcd8c8
languageName: node
linkType: hard

"pkginfo@npm:^0.4.1":
version: 0.4.1
resolution: "pkginfo@npm:0.4.1"
Expand Down Expand Up @@ -17690,13 +17621,6 @@ __metadata:
languageName: node
linkType: hard

"reselect@npm:^4.1.7":
version: 4.1.8
resolution: "reselect@npm:4.1.8"
checksum: a4ac87cedab198769a29be92bc221c32da76cfdad6911eda67b4d3e7136dca86208c3b210e31632eae31ebd2cded18596f0dd230d3ccc9e978df22f233b5583e
languageName: node
linkType: hard

"resolve-cwd@npm:^3.0.0":
version: 3.0.0
resolution: "resolve-cwd@npm:3.0.0"
Expand Down Expand Up @@ -17760,7 +17684,7 @@ __metadata:
languageName: node
linkType: hard

"resolve@npm:^1.1.6, resolve@npm:^1.10.0, resolve@npm:^1.11.0, resolve@npm:^1.12.0, resolve@npm:^1.14.2, resolve@npm:^1.17.0, resolve@npm:^1.20.0, resolve@npm:^1.22.1, resolve@npm:^1.22.3, resolve@npm:^1.22.4":
"resolve@npm:^1.1.6, resolve@npm:^1.10.0, resolve@npm:^1.11.0, resolve@npm:^1.12.0, resolve@npm:^1.14.2, resolve@npm:^1.17.0, resolve@npm:^1.20.0, resolve@npm:^1.22.3, resolve@npm:^1.22.4":
version: 1.22.8
resolution: "resolve@npm:1.22.8"
dependencies:
Expand Down Expand Up @@ -17802,7 +17726,7 @@ __metadata:
languageName: node
linkType: hard

"resolve@patch:resolve@^1.1.6#~builtin<compat/resolve>, resolve@patch:resolve@^1.10.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.11.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.12.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.14.2#~builtin<compat/resolve>, resolve@patch:resolve@^1.17.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.20.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.22.1#~builtin<compat/resolve>, resolve@patch:resolve@^1.22.3#~builtin<compat/resolve>, resolve@patch:resolve@^1.22.4#~builtin<compat/resolve>":
"resolve@patch:resolve@^1.1.6#~builtin<compat/resolve>, resolve@patch:resolve@^1.10.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.11.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.12.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.14.2#~builtin<compat/resolve>, resolve@patch:resolve@^1.17.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.20.0#~builtin<compat/resolve>, resolve@patch:resolve@^1.22.3#~builtin<compat/resolve>, resolve@patch:resolve@^1.22.4#~builtin<compat/resolve>":
version: 1.22.8
resolution: "resolve@patch:resolve@npm%3A1.22.8#~builtin<compat/resolve>::version=1.22.8&hash=c3c19d"
dependencies:
Expand Down

0 comments on commit ca9a8e0

Please sign in to comment.