import colors from 'colors' import { spawn } from 'node:child_process' import { buildTypesExport } from '../Build/helpers' import { getConfigFile, log, tickLog } from '../helpers' import { ConfigFile, IndexState } from '../types' import Build from '../Build' import Watch from '../Watch' import CreateDocumentation from '../CreateDocumentation' import { copyTypesFileToFrontendPaths } from './copyTypesFileToFrontendPaths' export const Run = async (state: IndexState) => { const startTime = new Date() const { port }: ConfigFile = await getConfigFile() const errorLength = 0 let started = false let isRestarting = false let restartTime = 0 await Build(state) log('Server built', 'green', state) await CreateDocumentation(state, 'Internal') await CreateDocumentation(state, 'Public') log(`\nStarting dev server on port: ${port}`, 'green', state) await copyTypesFileToFrontendPaths() const nodemon = spawn( 'nodemon', [ '--watch', './dist/API', './dist/server/index.js', 'localhost', `${port}`, ], { shell: true }, ) const elapsedSeconds = (new Date().getTime() - startTime.getTime()) / 1000 tickLog(`Ready in ${elapsedSeconds.toFixed(1)}s`, 'green', 'white', state, true) nodemon.stdout.on('data', (data) => { // console.info(`${data}`) if (!started) { const containsWappiMorse = data.includes('.-- .- .--. .--. ..') if (containsWappiMorse) { started = true } log(`${data}`, null, state, true) } else if (started) { const containsSkipMorse = data.includes('... -.- .. .--.') if (!containsSkipMorse) { log(`${data}`, null, state, true) if (!isRestarting) { const containsRestartingSubstring = `${data}`.includes('[nodemon] restarting') if (containsRestartingSubstring) { isRestarting = true restartTime = new Date().getTime() console.info(colors.magenta(' ↻ '), colors.white('Compiling changes')) } } else if (isRestarting) { const containsStartingSubstring = `${data}`.includes('nodemon] starting') if (containsStartingSubstring) { isRestarting = false tickLog(`Compiled changes in ${(new Date().getTime() - restartTime)}ms`, 'green', 'white', state, true) if (process.env.FRONTEND_PROJECT_PATHS) { buildTypesExport(state).then(() => { copyTypesFileToFrontendPaths() tickLog(`Types compiled and synced to: ${process.env.FRONTEND_PROJECT_PATHS}`, 'green', 'white', state, true) }) } } } } } }) nodemon.stderr.on('data', (data) => { console.error(`${data}`) }) nodemon.on('close', (code) => { if (errorLength === 0) { console.info(colors.green('\n Time for a break? see you soon. ;) \n\n')) } if (code !== 0) { console.info(`Something went wrong.. Exit code: ${code}`) } }) Watch(state) } export default Run