async function buildDocs()

in tools/build.ts [254:321]


async function buildDocs() {
  try {
    // INVESTIGATE json to stdout rather than FS?
    await Promise.all(
      MODULES.map(module =>
        spawnPromise('npx', ['typedoc',
          `${module === 'core' ?
            join(process.cwd(), 'src') :
            join(process.cwd(), 'src', module)}`,
          '--json',
          join(process.cwd(), 'dist', 'typedocs', `${module}.json`)
        ])));
    const entries = await Promise.all(MODULES.map(async (module) => {

      const buffer = await readFile(join(process.cwd(), 'dist', 'typedocs', `${module}.json`));
      const typedoc = JSON.parse(buffer.toString());
      if (!typedoc.children) {
        console.error('typedoc fail', module);
      }
      // TODO infer the entryPoint from the package.json
      const entryPoint = typedoc.children.find((c: any) => c.name === '"public_api"');
      const allChildren = [].concat(...typedoc.children.map(child =>
        // TODO chop out the working directory and filename
        child.children ?
          child.children.map(c => {
            return { ...c, path: dirname(child.originalName.split(process.cwd())[1]) };
          }) :
          []
      ));
      return (entryPoint.children || [])
        .filter(c => c.name[0] !== 'ɵ' && c.name[0] !== '_' /* private */)
        .map(child => ({ ...allChildren.find(c => child.target === c.id) }))
        .reduce((acc, child) => ({ ...acc, [encodeURIComponent(child.name)]: child }), {});
    }));
    const root = await rootPackage;
    const pipes = ['MonoTypeOperatorFunction', 'OperatorFunction', 'AuthPipe', 'UnaryFunction'];
    const tocType = child => {
      const decorators: string[] = child.decorators && child.decorators.map(d => d.name) || [];
      if (decorators.includes('NgModule')) {
        return 'NgModule';
      } else if (child.kindString === 'Type alias') {
        return 'Type alias';
      } else if (child.kindString === 'Variable' && child.defaultValue && child.defaultValue.startsWith('new InjectionToken')) {
        return 'InjectionToken';
      } else if (child.type) {
        return pipes.includes(child.type.name) ? 'Pipe' : child.type.name;
      } else if (child.signatures && child.signatures[0] && child.signatures[0].type && pipes.includes(child.signatures[0].type.name)) {
        return 'Pipe';
      } else {
        return child.kindString;
      }
    };
    const tableOfContents = entries.reduce((acc, entry, index) =>
        ({
          ...acc, [MODULES[index]]: {
            name: ENTRY_NAMES[index],
            exports: Object.keys(entry).reduce((acc, key) => ({ ...acc, [key]: tocType(entry[key]) }), {})
          }
        }),
      {}
    );
    const afdoc = entries.reduce((acc, entry, index) => ({ ...acc, [MODULES[index]]: entry }), { table_of_contents: tableOfContents });
    return writeFile(join(process.cwd(), `api-${root.version}.json`), JSON.stringify(afdoc, null, 2));
  } catch (e) {
    console.warn(e);
    return Promise.resolve();
  }
}