aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/mkdocs.ts
blob: 95d27fe6d17f9c1eb35f74ac53e310fd67b1c8aa (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { SpawnSyncReturns } from 'child_process';
import { Command } from 'commander';
import fs from 'fs-extra';
import { logger } from '../lib/logger';
import { generateDocs } from './docs';
import { exec } from './utils/exec';

process.on('unhandledRejection', (err) => {
  // Will print "unhandledRejection err is not defined"
  logger.error({ err }, 'unhandledRejection');
  process.exit(-1);
});

const program = new Command('pnpm mkdocs').description('Run mkdocs');

program
  .command('build', { isDefault: true })
  .description('Build mkdocs')
  .option('--no-build', 'do not build docs from source')
  .option('--no-strict', 'do not build in strict mode')
  .action(async (opts) => {
    await prepareDocs(opts);
    logger.info('* running mkdocs build');
    const args = ['run', 'mkdocs', 'build'];
    if (opts.strict) {
      args.push('--strict');
    }
    const res = exec('pdm', args, {
      cwd: 'tools/mkdocs',
      stdio: 'inherit',
    });
    checkResult(res);
  });

program
  .command('serve')
  .description('serve mkdocs')
  .option('--no-build', 'do not build docs from source')
  .option('--no-strict', 'do not build in strict mode')
  .action(async (opts) => {
    await prepareDocs(opts);
    logger.info('serving docs');
    logger.info('* running mkdocs serve');
    const args = ['run', 'mkdocs', 'serve'];
    if (opts.strict) {
      args.push('--strict');
    }
    const res = exec('pdm', args, {
      cwd: 'tools/mkdocs',
      stdio: 'inherit',
    });
    checkResult(res);
  });

async function prepareDocs(opts: any): Promise<void> {
  logger.info('Building docs');
  if (opts.build) {
    logger.info('* generate docs');
    await generateDocs('tools/mkdocs', false);
  } else {
    logger.info('* using prebuild docs from build step');
    await fs.copy('tmp/docs', 'tools/mkdocs/docs');
  }
}

function checkResult(res: SpawnSyncReturns<string>): void {
  if (res.signal) {
    logger.error(`Signal received: ${res.signal}`);
    process.exit(-1);
  } else if (res.status && res.status !== 0) {
    logger.error(`Error occured:\n${res.stderr || res.stdout}`);
    process.exit(res.status);
  } else {
    logger.debug(`Build completed:\n${res.stdout || res.stderr}`);
  }
}

void program.parseAsync();