From c46435daf50b86b48d806cdadc9139dc05e3b64a Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Mon, 1 Mar 2021 11:08:53 +1300 Subject: [PATCH] moar cleanup --- bin/hook-reference/data.js | 16 +++++++++------- bin/hook-reference/data.json | 2 +- bin/hook-reference/index.js | 10 ++++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/bin/hook-reference/data.js b/bin/hook-reference/data.js index 2db756fa819..8ef31ebbaea 100644 --- a/bin/hook-reference/data.js +++ b/bin/hook-reference/data.js @@ -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 ) => @@ -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' ); @@ -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(); diff --git a/bin/hook-reference/data.json b/bin/hook-reference/data.json index 4c9a9848a50..65d4513d1c3 100644 --- a/bin/hook-reference/data.json +++ b/bin/hook-reference/data.json @@ -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' ) );" } diff --git a/bin/hook-reference/index.js b/bin/hook-reference/index.js index 2869a59b7c0..38dc5a35269 100644 --- a/bin/hook-reference/index.js +++ b/bin/hook-reference/index.js @@ -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 ), [] ); @@ -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 ) );