This repository was archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
Hook Reference: Automate a reference resource #6454
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
78d796a
read sample doc
psealock e24c32c
npm cli
psealock 5a926a4
work with arrays
psealock c411e5b
Its happening
psealock 5b179c6
better naming
psealock 08ae8ee
cleanup
psealock 802760d
moar cleanup
psealock 4a1274f
new line
psealock 7c278be
better
psealock 59c091d
save
psealock 66442a5
fixup rebase error
psealock 8af57c6
package lock update
psealock 9c036fd
node 12 usage
psealock 00faca7
add changelog
psealock 1b2c88a
fancy logs
psealock 4e530bb
update package lock
psealock de6027e
changelog in right place
psealock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { readFile } = require( 'fs' ).promises; | ||
const exec = require( 'await-exec' ); | ||
const { parse } = require( 'comment-parser/lib' ); | ||
const { relative, resolve } = require( 'path' ); | ||
const chalk = require( 'chalk' ); | ||
|
||
const getHooks = ( parsedData ) => | ||
parsedData.filter( ( docBlock ) => | ||
docBlock.tags.some( ( tag ) => tag.tag === 'hook' ) | ||
); | ||
|
||
const getSourceFile = ( file, commit, { source } ) => { | ||
const first = source[ 0 ].number + 1; | ||
const last = source[ source.length - 1 ].number + 1; | ||
|
||
return `https://github.com/woocommerce/woocommerce-admin/blob/${ commit }/${ file }#L${ first }-L${ last }`; | ||
}; | ||
|
||
const logProgress = ( fileName, { tags } ) => { | ||
const hook = tags.find( ( tag ) => tag.tag === 'hook' ); | ||
console.log( | ||
chalk.cyan( `${ hook.name } ` ) + | ||
chalk.yellow( 'generated in ' ) + | ||
chalk.yellow.underline( fileName ) | ||
); | ||
}; | ||
|
||
const addSourceFiles = async ( hooks, fileName ) => { | ||
const { stdout } = await exec( 'git log --pretty="format:%H" -1' ); | ||
const commit = stdout.trim(); | ||
|
||
return hooks.map( ( hook ) => { | ||
logProgress( fileName, hook ); | ||
hook.sourceFile = getSourceFile( fileName, commit, hook ); | ||
return hook; | ||
} ); | ||
}; | ||
|
||
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 ( 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' ); | ||
return { | ||
description, | ||
sourceFile, | ||
name: hook ? hook.name : '', | ||
example: example ? example.description : '', | ||
}; | ||
} ); | ||
}; | ||
|
||
const createData = async ( paths ) => { | ||
const data = await Promise.all( | ||
paths.map( async ( path ) => { | ||
return await makeDocObjects( path ); | ||
} ) | ||
); | ||
return data.flat(); | ||
}; | ||
|
||
module.exports = createData; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
"description": "List of homepage stats enabled by default", | ||
"sourceFile": "https://github.com/woocommerce/woocommerce-admin/blob/ff1a3cb2f3857cfa759fb3c93cedfe630dbd5510/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' ) );" | ||
} | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const fs = require( 'fs' ); | ||
const { stat, readdir, writeFile } = require( 'fs' ).promises; | ||
const { resolve } = require( 'path' ); | ||
const createData = require( './data' ); | ||
const chalk = require( 'chalk' ); | ||
|
||
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() | ||
? getFilePaths( res ) | ||
: res; | ||
} ) | ||
); | ||
return files.reduce( ( a, f ) => a.concat( f ), [] ); | ||
} | ||
|
||
const writeJSONFile = async ( data ) => { | ||
const fileName = 'bin/hook-reference/data.json'; | ||
const stringifiedData = JSON.stringify( data, null, 4 ); | ||
await writeFile( fileName, stringifiedData + '\n' ); | ||
|
||
console.log( '\n' ); | ||
console.log( | ||
chalk.greenBright( | ||
'A new Hook Reference data source has been created. See `/bin/hook-reference/data.json` and be sure to commit changes.' | ||
) | ||
); | ||
}; | ||
|
||
console.log( chalk.green( 'Preparing Hook Reference data file' ) ); | ||
console.log( '\n' ); | ||
|
||
getFilePaths( 'client' ) | ||
.then( ( paths ) => createData( paths ) ) | ||
.then( ( data ) => writeJSONFile( data ) ) | ||
.catch( ( e ) => console.error( e ) ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Promise.all
,map
,reduce
, and recursion all in a ~10 line async function. 👏🏻There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤣 There was significant help from stack overflow