Skip to content

Commit b72eb60

Browse files
authored
Issue 0 file pathing and environments (#179)
1 parent 32853a3 commit b72eb60

17 files changed

Lines changed: 109 additions & 124 deletions

bin/startup.js

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,23 @@ const { spawn } = require('child_process')
44
// node is often not in path for the service process
55
process.env.PATH = `${process.env.PATH}:${process.env.NODE_PATH}`
66

7-
if (process.env.DEBUG === '*') {
8-
console.log('DEBUG: Starting the server')
7+
const isProduction = process.env.NODE_ENV === 'production'
8+
9+
if (!isProduction) {
10+
console.log('DEVELOPMENT: Starting the server')
911
console.log(`process.env: ${JSON.stringify(process.env)}`)
1012
}
1113

12-
const isProduction = process.env.NODE_ENV === 'production'
13-
14-
const runScript = isProduction
14+
isProduction
1515
? spawn('npx', ['codeclimbers', 'start', 'server'], {
1616
shell: true,
17+
stdio: 'inherit',
1718
})
18-
: spawn('node', [
19-
`${process.env.CODE_CLIMBER_BIN_PATH}/run.js`,
20-
'start',
21-
'server',
22-
])
23-
24-
runScript.stdout.on('data', (data) => {
25-
process.stdout.write(data)
26-
})
27-
28-
runScript.stderr.on('data', (data) => {
29-
process.stderr.write(data)
30-
})
31-
32-
runScript.on('close', (code) => {
33-
console.log(`Child process exited with code ${code}`)
34-
})
35-
36-
runScript.on('error', (error) => {
37-
console.error(`Error executing the script: ${error.message}`)
38-
})
19+
: spawn(
20+
'node',
21+
[`${process.env.CODE_CLIMBER_BIN_PATH}/run.js`, 'start', 'server'],
22+
{
23+
shell: true,
24+
stdio: 'inherit',
25+
},
26+
)

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/server/commands/start/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import find from 'find-process'
66
import pc from 'picocolors'
77

88
import http from 'http'
9-
import { bootstrap, SERVER_CONSTANTS, StartupServiceFactory } from '../..'
9+
import { StartupServiceFactory } from '../../src/v1/startup/startupService.factory'
10+
import { bootstrap } from '../../src/main'
11+
import { PROCESS_NAME } from '../../utils/constants'
1012

1113
const MAX_ATTEMPTS = 10
1214
const POLL_INTERVAL = 3000 // 3 seconds
@@ -78,10 +80,7 @@ export default class Start extends Command {
7880
}
7981

8082
async run(): Promise<void> {
81-
const [codeclimbersInstance] = await find(
82-
'name',
83-
SERVER_CONSTANTS.PROCESS_NAME,
84-
)
83+
const [codeclimbersInstance] = await find('name', PROCESS_NAME)
8584

8685
if (codeclimbersInstance) {
8786
return this.error(

packages/server/commands/startup/disable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
process.env.CODECLIMBERS_SERVER_APP_CONTEXT = 'cli'
22

33
import { Command } from '@oclif/core'
4-
import { StartupServiceFactory } from '../..'
4+
import { StartupServiceFactory } from '../../src/v1/startup/startupService.factory'
55

66
export default class Disable extends Command {
77
static description = 'Disable starting codeclimbers on computer startup'

packages/server/commands/startup/enable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
process.env.CODECLIMBERS_SERVER_APP_CONTEXT = 'cli'
22

33
import { Command } from '@oclif/core'
4-
import { StartupServiceFactory } from '../..'
4+
import { StartupServiceFactory } from '../../src/v1/startup/startupService.factory'
55

66
export default class Disable extends Command {
77
static description = 'Disable starting codeclimbers on computer startup'

packages/server/commands/stop/index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { exec as _exec } from 'node:child_process'
55
import util from 'node:util'
66
import os from 'node:os'
77
import find from 'find-process'
8-
import { SERVER_CONSTANTS } from '../..'
8+
import { PROCESS_NAME } from '../../utils/constants'
99
// eslint-disable-next-line import/no-unresolved
1010

1111
const exec = util.promisify(_exec)
@@ -18,12 +18,10 @@ export default class Stop extends Command {
1818
static examples = [`<%= config.bin %> <%= command.id %>`]
1919

2020
async run(): Promise<void> {
21-
const [instance] = await find('name', SERVER_CONSTANTS.PROCESS_NAME)
21+
const [instance] = await find('name', PROCESS_NAME)
2222

2323
if (!instance) {
24-
this.error(
25-
`Could not find a running instance of ${SERVER_CONSTANTS.PROCESS_NAME}`,
26-
)
24+
this.error(`Could not find a running instance of ${PROCESS_NAME}`)
2725
}
2826

2927
const { stdout, stderr } = await exec(
@@ -36,8 +34,6 @@ export default class Stop extends Command {
3634
this.error(stderr)
3735
}
3836

39-
this.log(
40-
`Stopped ${SERVER_CONSTANTS.PROCESS_NAME} - ${stdout || 'Success!'}`,
41-
)
37+
this.log(`Stopped ${PROCESS_NAME} - ${stdout || 'Success!'}`)
4238
}
4339
}

packages/server/index.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/server/src/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { RequestLoggerMiddleware } from './common/infrastructure/http/middleware
66
import { ServeStaticModule } from '@nestjs/serve-static'
77
import { DbModule } from './v1/database/knex'
88
import { SentryModule } from '@sentry/nestjs/setup'
9-
import { APP_PATH } from '../utils/node.util'
9+
import { APP_DIST_PATH } from '../utils/node.util'
1010

1111
@Module({
1212
imports: [
@@ -20,7 +20,7 @@ import { APP_PATH } from '../utils/node.util'
2020
},
2121
]),
2222
ServeStaticModule.forRoot({
23-
rootPath: APP_PATH,
23+
rootPath: APP_DIST_PATH,
2424
}),
2525
],
2626
providers: [

packages/server/src/main.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import './sentry'
44
import { NestFactory } from '@nestjs/core'
55
import { AppModule } from './app.module'
66
import { Logger, ValidationPipe } from '@nestjs/common'
7-
import { isCli } from '../utils/environment.util'
7+
import { isCli, isProd } from '../utils/environment.util'
88
import { PROCESS_NAME } from '../utils/constants'
99
import { updateSettings } from '../utils/wakatime.util'
1010
import { startMigrations } from './v1/database/migrations'
@@ -13,20 +13,18 @@ const updatedIniValues: Record<string, string> = {
1313
api_key: 'eacb3beb-dad8-4fa1-b6ba-f89de8bf8f4a', // placeholder value
1414
api_url: 'http://localhost:14400/api/v1/wakatime',
1515
}
16-
const shouldShowDebugLogs =
17-
process.env.DEBUG === '*' || process.env.NODE_ENV === 'development'
1816

1917
const traceEnvironment = () => {
20-
Logger.log(`Running as: ${process.env.NODE_ENV}`, 'main.ts')
21-
Logger.log(`process.env: ${JSON.stringify(process.env)}`, 'main.ts')
18+
Logger.debug(`Running as: ${process.env.NODE_ENV}`, 'main.ts')
19+
Logger.debug(`process.env: ${JSON.stringify(process.env)}`, 'main.ts')
2220
}
2321

2422
export async function bootstrap() {
2523
const port = process.env.CODECLIMBERS_SERVER_PORT || 14_400
2624
const app = await NestFactory.create(AppModule, {
27-
logger: shouldShowDebugLogs
25+
logger: !isProd()
2826
? ['log', 'debug', 'error', 'verbose', 'warn']
29-
: ['error', 'warn'],
27+
: ['log', 'error', 'warn'],
3028
})
3129
traceEnvironment()
3230

packages/server/src/sentry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as Sentry from '@sentry/nestjs'
22
import { nodeProfilingIntegration } from '@sentry/profiling-node'
3+
import { isProd } from '../utils/environment.util'
34

4-
if (process.env.NODE_ENV === 'production') {
5+
if (isProd()) {
56
Sentry.init({
67
dsn: 'https://e885e4c5eeed09d6229c7d7bfdc8d762@o4507772937043968.ingest.us.sentry.io/4507772946022400',
78
integrations: [nodeProfilingIntegration()],

0 commit comments

Comments
 (0)