Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
moar cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
psealock committed Feb 28, 2021
1 parent c414d9e commit c46435d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
16 changes: 9 additions & 7 deletions bin/hook-reference/data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { readFile } = require( 'fs/promises' );
const exec = require( 'await-exec' );
const { parse } = require( 'comment-parser/lib' );
const { relative, resolve } = require( 'path' );

const getHooks = ( parsedData ) =>
parsedData.filter( ( docBlock ) =>
Expand All @@ -24,18 +25,19 @@ const addSourceFiles = async ( hooks, fileName ) => {
} );
};

const prepareHooks = async ( fileName ) => {
const data = await readFile( fileName, 'utf-8' ).catch( ( err ) =>
const prepareHooks = async ( path ) => {
const data = await readFile( path, 'utf-8' ).catch( ( err ) =>
console.error( 'Failed to read file', err )
);
const fileName = relative( resolve( __dirname, '../../' ), path );

const parsedData = parse( data );
const rawHooks = getHooks( parsedData );
return await addSourceFiles( rawHooks, fileName );
};

const makeDocObjects = async ( fileName ) => {
const hooks = await prepareHooks( fileName );
const makeDocObjects = async ( path ) => {
const hooks = await prepareHooks( path );
return hooks.map( ( { description, tags, sourceFile } ) => {
const example = tags.find( ( tag ) => tag.tag === 'example' );
const hook = tags.find( ( tag ) => tag.tag === 'hook' );
Expand All @@ -48,10 +50,10 @@ const makeDocObjects = async ( fileName ) => {
} );
};

const createData = async ( fileNames ) => {
const createData = async ( paths ) => {
const data = await Promise.all(
fileNames.map( async ( fileName ) => {
return await makeDocObjects( fileName );
paths.map( async ( path ) => {
return await makeDocObjects( path );
} )
);
return data.flat();
Expand Down
2 changes: 1 addition & 1 deletion bin/hook-reference/data.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"description": "List of homepage stats enabled by default",
"sourceFile": "https://github.com/woocommerce/woocommerce-admin/blob/b49e316cda779b925a8de9dbc54c3e293b0a8086//Users/paulsealock/vagrant-local/www/tangaroa/public_html/wp-content/plugins/woocommerce-admin/client/homescreen/stats-overview/defaults.js#L5-L16",
"sourceFile": "https://github.com/woocommerce/woocommerce-admin/blob/c414d9e20cacc1a40bb83ab1a056cfe09b64fc8a/client/homescreen/stats-overview/defaults.js#L5-L16",
"name": "woocommerce_admin_homepage_default_stats",
"example": "addFilter( 'woocommerce_admin_homepage_default_stats', 'plugin-domain', ( defaultStats ) => defaultStats.filter( ( stat ) => stat !== 'jetpack/stats/views' ) );"
}
Expand Down
10 changes: 6 additions & 4 deletions bin/hook-reference/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const { stat, readdir, writeFile } = require( 'fs/promises' );
const { resolve } = require( 'path' );
const createData = require( './data' );

async function getFiles( dir ) {
async function getFilePaths( dir ) {
const subdirs = await readdir( dir );
const files = await Promise.all(
subdirs.map( async ( subdir ) => {
const res = resolve( dir, subdir );
return ( await stat( res ) ).isDirectory() ? getFiles( res ) : res;
return ( await stat( res ) ).isDirectory()
? getFilePaths( res )
: res;
} )
);
return files.reduce( ( a, f ) => a.concat( f ), [] );
Expand All @@ -20,7 +22,7 @@ const writeJSONFile = async ( data ) => {
await writeFile( fileName, stringifiedData );
};

getFiles( 'client' )
.then( ( fileNames ) => createData( fileNames ) )
getFilePaths( 'client' )
.then( ( paths ) => createData( paths ) )
.then( ( data ) => writeJSONFile( data ) )
.catch( ( e ) => console.error( e ) );

0 comments on commit c46435d

Please sign in to comment.