|
| 1 | +/* |
| 2 | +* @adonisjs/assembler |
| 3 | +* |
| 4 | +* (c) Harminder Virk <[email protected]> |
| 5 | +* |
| 6 | +* For the full copyright and license information, please view the LICENSE |
| 7 | +* file that was distributed with this source code. |
| 8 | +*/ |
| 9 | + |
| 10 | +import { BaseCommand, flags } from '@adonisjs/ace' |
| 11 | + |
| 12 | +import { Watcher } from '../src/Watcher' |
| 13 | +import { Compiler } from '../src/Compiler' |
| 14 | +import { BuildWatcher } from '../src/BuildWatcher' |
| 15 | +import { ADONIS_ACE_CWD, ADONIS_IS_TYPESCRIPT, ADONIS_BUILD_DIR } from '../config/env' |
| 16 | + |
| 17 | +/** |
| 18 | + * Compile typescript project to Javascript and start |
| 19 | + * the HTTP server |
| 20 | + */ |
| 21 | +export default class Serve extends BaseCommand { |
| 22 | + public static commandName = 'serve' |
| 23 | + public static description = 'Compile typescript code to Javascript and start the HTTP server' |
| 24 | + |
| 25 | + /** |
| 26 | + * Allows watching for file changes |
| 27 | + */ |
| 28 | + @flags.boolean({ description: 'Watch for file changes and re-build the project', alias: 'w' }) |
| 29 | + public watch: boolean |
| 30 | + |
| 31 | + /** |
| 32 | + * Allows watching for file changes |
| 33 | + */ |
| 34 | + @flags.boolean({ |
| 35 | + description: 'Turn off Typescript compiler by passing --no-compile', |
| 36 | + default: true, |
| 37 | + }) |
| 38 | + public compile: boolean |
| 39 | + |
| 40 | + /** |
| 41 | + * Arguments to pass to the `node` binary |
| 42 | + */ |
| 43 | + @flags.array({ description: 'CLI options to pass to the node command line' }) |
| 44 | + public nodeArgs: string[] = [] |
| 45 | + |
| 46 | + public async handle () { |
| 47 | + /** |
| 48 | + * Dis-allow when CWD is missing. It will always be set by `node ace` |
| 49 | + * commands |
| 50 | + */ |
| 51 | + if (!ADONIS_ACE_CWD) { |
| 52 | + this.logger.error( |
| 53 | + 'Cannot build non-typescript project. Make sure to run "node ace serve" from the project root', |
| 54 | + ) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Dis-allow when running the command inside the compiled source and still |
| 60 | + * asking to re-compile the code |
| 61 | + */ |
| 62 | + if (!ADONIS_IS_TYPESCRIPT && this.compile !== false) { |
| 63 | + this.logger.error( |
| 64 | + 'Cannot build non-typescript project. Make sure to run "node ace serve" from the project root', |
| 65 | + ) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + if (this.compile === false) { |
| 71 | + await new BuildWatcher(ADONIS_ACE_CWD, this.nodeArgs, this.logger).watch(ADONIS_BUILD_DIR || './') |
| 72 | + } else if (this.watch) { |
| 73 | + await new Watcher(ADONIS_ACE_CWD, true, this.nodeArgs, this.logger).watch() |
| 74 | + } else { |
| 75 | + await new Compiler(ADONIS_ACE_CWD, true, this.nodeArgs, this.logger).compile() |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + this.logger.fatal(error) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments