This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hook Reference: Automate a reference resource (#6454)
* read sample doc * npm cli * work with arrays * Its happening * better naming * cleanup * moar cleanup * new line * better * save * fixup rebase error * package lock update * node 12 usage * add changelog * fancy logs * update package lock * changelog in right place
- Loading branch information
Showing
8 changed files
with
199 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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