import { Command , flags } from '@oclif/command' ; import { spawn } from 'cross-spawn' ; import { join , resolve } from 'path' ; import { existsSync } from 'fs' ; import { getNodemon } from '../utils/getNodemon' ; import { appPath } from '../utils/paths' ; import { cleanJsFile } from '../utils/cleanJsFile' ; import { genServer } from '../generators/server' ; import { genConfig } from '../generators/config' ; import { genControllers } from '../generators/controllers' ; import { genResolvers } from '../generators/resolvers' ; import { genSchedules } from '../generators/schedules' ; import { genEvents } from '../generators/events' ; import { genConfigs } from '../generators/configs' ; export default class Dev extends Command { static description = 'Runs the app in development mode' ; static examples = [ '$ tie dev' ] ; static flags = { main : flags . string ( { char : 'm' , description : 'Server main file' , } ) , } ; static args = [ { name : 'file' } ] ; private cwd = process . cwd ( ) ; private entry : string = '' ; private tsconfigPath = this . getTsConfigPath ( ) ; private tsconfig = require ( this . tsconfigPath ) ; private tsconfigPathsString = this . tsconfig . compilerOptions . baseUrl ? '-r tsconfig-paths/register' : '' ; private getTsConfigPath ( ) { const serverTsConfigPath = join ( this . cwd , 'tsconfig.server.json' ) ; if ( existsSync ( serverTsConfigPath ) ) return serverTsConfigPath ; return join ( this . cwd , 'tsconfig.json' ) ; } private useTsNode ( ) { const exec = `ts-node ${ this . tsconfigPathsString } --project ${ this . tsconfigPath } ${ this . entry }` ; const startArgs = [ '--watch' , './**/*.ts' , '--ext' , 'ts' , '--ignore' , './**/*.test.ts' , '--exec' , exec , ] ; const nodemon = getNodemon ( ) ; const child = spawn ( nodemon , startArgs , { stdio : 'inherit' } ) ; child . on ( 'close' , ( code ) => { if ( code !== 0 ) { console . log ( 'Error~' ) ; } } ) ; } async run ( ) { const { flags } = this . parse ( Dev ) ; const cwd = process . cwd ( ) ; const { main = false } = flags ; this . entry = main ? resolve ( cwd , main ) : appPath ; cleanJsFile ( cwd ) ; await Promise . all ( [ genControllers ( ) , genResolvers ( ) , genSchedules ( ) , genEvents ( ) , genServer ( ) , genConfigs ( ) , ] ) ; await genConfig ( ) ; this . useTsNode ( ) ; } }