async function extractDocsFromRN()

in sync-api-docs/extractDocsFromRN.js [21:69]


async function extractDocsFromRN(rnRoot) {
  // TODO: make implementation async

  const allComponentFiles = glob.sync(
    path.join(rnRoot, '/Libraries/{Components,Image,}/**/*.js'),
    {
      nodir: true,
      absolute: true,
    }
  );

  const docs = [];

  for (const file of allComponentFiles) {
    const contents = fs.readFileSync(file, {encoding: 'utf-8'});
    if (!contents.includes(GENERATE_ANNOTATION)) {
      continue;
    }

    console.log(file);

    const result = reactDocs.parse(
      contents,
      reactDocs.resolver.findAllComponentDefinitions,
      reactDocs.defaultHandlers.filter(
        handler => handler !== reactDocs.handlers.propTypeCompositionHandler
      ),
      {filename: file}
    );

    const filteredResult = result.filter(item => {
      if (item.description) return item;
    });

    docs.push({
      file,
      component: cleanComponentResult(...filteredResult),
    });
  }

  // Make sure output is JSON-safe
  const docsSerialized = JSON.parse(JSON.stringify(docs));
  await fs.writeFile(
    path.join(__dirname, 'extracted.json'),
    JSON.stringify(docsSerialized, null, 2),
    'utf8'
  );
  return docsSerialized;
}