summaryrefslogtreecommitdiff
path: root/src/index.ts
blob: 09d952b283c99fc547e796197c150178493366ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { args, type Args } from '@/args';
import { join } from 'path';
import { watch } from 'fs/promises';
import { generateParser, GRAMMAR_FILE, GENERATED_PARSER } from '@/parser';
import { ConsoleTracingLogger } from '@/utils';

const devMode = async (logger: ConsoleTracingLogger) => {
  logger.info('Watching for changes in parser...');
  const watcher = watch(import.meta.dir, { recursive: true });
  for await (const event of watcher) {
    if (event.filename?.endsWith(GRAMMAR_FILE)) {
      const grammarFile = join(import.meta.dir, event.filename);
      const outputFile = join(
        import.meta.dir,
        event.filename.replace(GRAMMAR_FILE, GENERATED_PARSER),
      );
      logger.info(
        `Generating parser at Location=(${grammarFile}) to Source=(${outputFile})...`,
      );
      generateParser(grammarFile, outputFile);
    }
  }
};

export const main = async (args: Args) => {
  const logger = new ConsoleTracingLogger('main');

  if (args.devMode) {
    logger.info('Starting in dev mode...');
    await devMode(logger.createChild('devMode'));
  }
};

main(args);