2018-08-31 17:27:21 +00:00
|
|
|
/** @format */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
const fs = require( 'fs' );
|
|
|
|
const path = require( 'path' );
|
2018-09-21 15:19:05 +00:00
|
|
|
const { parse, resolver } = require( 'react-docgen' );
|
2018-08-31 17:27:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
const { getDescription, getProps, getTitle } = require( './lib/formatting' );
|
|
|
|
const {
|
|
|
|
COMPONENTS_FOLDER,
|
2018-11-15 18:16:23 +00:00
|
|
|
PACKAGES_FOLDER,
|
2018-08-31 17:27:21 +00:00
|
|
|
deleteExistingDocs,
|
|
|
|
getExportedFileList,
|
|
|
|
getMdFileName,
|
|
|
|
getRealFilePaths,
|
|
|
|
writeTableOfContents,
|
|
|
|
} = require( './lib/file-system' );
|
|
|
|
|
|
|
|
// Start by wiping the existing docs. **Change this if we end up manually editing docs**
|
|
|
|
deleteExistingDocs();
|
|
|
|
|
|
|
|
// Read components file to get a list of exported files, convert that to a list of absolute paths to public components.
|
2018-11-15 18:16:23 +00:00
|
|
|
const files = [
|
|
|
|
...getRealFilePaths( getExportedFileList( path.resolve( COMPONENTS_FOLDER, 'index.js' ) ) ),
|
|
|
|
...getRealFilePaths( getExportedFileList( path.resolve( PACKAGES_FOLDER, 'index.js' ) ), PACKAGES_FOLDER ),
|
|
|
|
];
|
2018-08-31 17:27:21 +00:00
|
|
|
|
|
|
|
// Build the documentation by reading each file.
|
|
|
|
files.forEach( file => {
|
|
|
|
try {
|
|
|
|
const content = fs.readFileSync( file );
|
|
|
|
buildDocs( file, content );
|
|
|
|
} catch ( readErr ) {
|
|
|
|
console.warn( file, readErr );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
writeTableOfContents( files );
|
|
|
|
|
|
|
|
console.log( `Wrote docs for ${ files.length } files.` );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse each file's content & build up a markdown file.
|
|
|
|
*
|
|
|
|
* @param { string } fileName The absolute path of this file.
|
|
|
|
* @param { string } content Content of this file.
|
2018-09-21 15:19:05 +00:00
|
|
|
* @param { boolean } multiple If there are multiple exports in this file, we need to use a different resolver.
|
2018-08-31 17:27:21 +00:00
|
|
|
*/
|
2018-09-21 15:19:05 +00:00
|
|
|
function buildDocs( fileName, content, multiple = false ) {
|
|
|
|
const mdFileName = getMdFileName( fileName );
|
|
|
|
let markdown;
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
try {
|
2018-09-21 15:19:05 +00:00
|
|
|
if ( multiple ) {
|
|
|
|
const docObject = parse( content, resolver.findAllExportedComponentDefinitions );
|
|
|
|
markdown = docObject.map( doc => generateMarkdown( doc ) ).join( '\n\n' );
|
|
|
|
} else {
|
|
|
|
const docObject = parse( content );
|
|
|
|
markdown = generateMarkdown( docObject );
|
|
|
|
}
|
2018-08-31 17:27:21 +00:00
|
|
|
fs.appendFileSync( mdFileName, markdown );
|
|
|
|
} catch ( parseErr ) {
|
2018-09-21 15:19:05 +00:00
|
|
|
if ( ! multiple ) {
|
|
|
|
// The most likely error is that there are multiple exported components
|
|
|
|
buildDocs( fileName, content, true );
|
|
|
|
return;
|
|
|
|
}
|
2018-08-31 17:27:21 +00:00
|
|
|
console.warn( fileName, parseErr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert documentation object to a markdown string.
|
|
|
|
*
|
|
|
|
* @param { object } docObject The parsed documentation object.
|
|
|
|
* @return { string } Generated markdown.
|
|
|
|
*/
|
|
|
|
function generateMarkdown( docObject ) {
|
|
|
|
let markdownString = getTitle( docObject.displayName ) + '\n';
|
|
|
|
markdownString += getDescription( docObject.description ) + '\n';
|
|
|
|
markdownString += getProps( docObject.props );
|
2018-08-31 20:13:20 +00:00
|
|
|
return markdownString + '\n';
|
2018-08-31 17:27:21 +00:00
|
|
|
}
|