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

Commit b17aab1

Browse files
authored
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
1 parent e7a62cb commit b17aab1

File tree

8 files changed

+199
-49
lines changed

8 files changed

+199
-49
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
'@wordpress/valid-sprintf': 'warn',
1010
'jsdoc/check-tag-names': [
1111
'error',
12-
{ definedTags: [ 'jest-environment' ] },
12+
{ definedTags: [ 'jest-environment', 'hook' ] },
1313
],
1414
'import/no-extraneous-dependencies': 'warn',
1515
'import/no-unresolved': 'warn',

bin/hook-reference/data.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const { readFile } = require( 'fs' ).promises;
2+
const exec = require( 'await-exec' );
3+
const { parse } = require( 'comment-parser/lib' );
4+
const { relative, resolve } = require( 'path' );
5+
const chalk = require( 'chalk' );
6+
7+
const getHooks = ( parsedData ) =>
8+
parsedData.filter( ( docBlock ) =>
9+
docBlock.tags.some( ( tag ) => tag.tag === 'hook' )
10+
);
11+
12+
const getSourceFile = ( file, commit, { source } ) => {
13+
const first = source[ 0 ].number + 1;
14+
const last = source[ source.length - 1 ].number + 1;
15+
16+
return `https://github.com/woocommerce/woocommerce-admin/blob/${ commit }/${ file }#L${ first }-L${ last }`;
17+
};
18+
19+
const logProgress = ( fileName, { tags } ) => {
20+
const hook = tags.find( ( tag ) => tag.tag === 'hook' );
21+
console.log(
22+
chalk.cyan( `${ hook.name } ` ) +
23+
chalk.yellow( 'generated in ' ) +
24+
chalk.yellow.underline( fileName )
25+
);
26+
};
27+
28+
const addSourceFiles = async ( hooks, fileName ) => {
29+
const { stdout } = await exec( 'git log --pretty="format:%H" -1' );
30+
const commit = stdout.trim();
31+
32+
return hooks.map( ( hook ) => {
33+
logProgress( fileName, hook );
34+
hook.sourceFile = getSourceFile( fileName, commit, hook );
35+
return hook;
36+
} );
37+
};
38+
39+
const prepareHooks = async ( path ) => {
40+
const data = await readFile( path, 'utf-8' ).catch( ( err ) =>
41+
console.error( 'Failed to read file', err )
42+
);
43+
const fileName = relative( resolve( __dirname, '../../' ), path );
44+
45+
const parsedData = parse( data );
46+
const rawHooks = getHooks( parsedData );
47+
return await addSourceFiles( rawHooks, fileName );
48+
};
49+
50+
const makeDocObjects = async ( path ) => {
51+
const hooks = await prepareHooks( path );
52+
return hooks.map( ( { description, tags, sourceFile } ) => {
53+
const example = tags.find( ( tag ) => tag.tag === 'example' );
54+
const hook = tags.find( ( tag ) => tag.tag === 'hook' );
55+
return {
56+
description,
57+
sourceFile,
58+
name: hook ? hook.name : '',
59+
example: example ? example.description : '',
60+
};
61+
} );
62+
};
63+
64+
const createData = async ( paths ) => {
65+
const data = await Promise.all(
66+
paths.map( async ( path ) => {
67+
return await makeDocObjects( path );
68+
} )
69+
);
70+
return data.flat();
71+
};
72+
73+
module.exports = createData;

bin/hook-reference/data.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"description": "List of homepage stats enabled by default",
4+
"sourceFile": "https://github.com/woocommerce/woocommerce-admin/blob/ff1a3cb2f3857cfa759fb3c93cedfe630dbd5510/client/homescreen/stats-overview/defaults.js#L5-L16",
5+
"name": "woocommerce_admin_homepage_default_stats",
6+
"example": "addFilter( 'woocommerce_admin_homepage_default_stats', 'plugin-domain', ( defaultStats ) => defaultStats.filter( ( stat ) => stat !== 'jetpack/stats/views' ) );"
7+
}
8+
]

bin/hook-reference/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fs = require( 'fs' );
2+
const { stat, readdir, writeFile } = require( 'fs' ).promises;
3+
const { resolve } = require( 'path' );
4+
const createData = require( './data' );
5+
const chalk = require( 'chalk' );
6+
7+
async function getFilePaths( dir ) {
8+
const subdirs = await readdir( dir );
9+
const files = await Promise.all(
10+
subdirs.map( async ( subdir ) => {
11+
const res = resolve( dir, subdir );
12+
return ( await stat( res ) ).isDirectory()
13+
? getFilePaths( res )
14+
: res;
15+
} )
16+
);
17+
return files.reduce( ( a, f ) => a.concat( f ), [] );
18+
}
19+
20+
const writeJSONFile = async ( data ) => {
21+
const fileName = 'bin/hook-reference/data.json';
22+
const stringifiedData = JSON.stringify( data, null, 4 );
23+
await writeFile( fileName, stringifiedData + '\n' );
24+
25+
console.log( '\n' );
26+
console.log(
27+
chalk.greenBright(
28+
'A new Hook Reference data source has been created. See `/bin/hook-reference/data.json` and be sure to commit changes.'
29+
)
30+
);
31+
};
32+
33+
console.log( chalk.green( 'Preparing Hook Reference data file' ) );
34+
console.log( '\n' );
35+
36+
getFilePaths( 'client' )
37+
.then( ( paths ) => createData( paths ) )
38+
.then( ( data ) => writeJSONFile( data ) )
39+
.catch( ( e ) => console.error( e ) );

client/homescreen/stats-overview/defaults.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22
* External dependencies
33
*/
44
import { applyFilters } from '@wordpress/hooks';
5-
5+
/**
6+
* List of homepage stats enabled by default
7+
*
8+
* @hook woocommerce_admin_homepage_default_stats
9+
* @example
10+
* addFilter(
11+
* 'woocommerce_admin_homepage_default_stats',
12+
* 'plugin-domain',
13+
* ( defaultStats ) =>
14+
* defaultStats.filter( ( stat ) => stat !== 'jetpack/stats/views' )
15+
*);
16+
*/
617
export const DEFAULT_STATS = applyFilters(
718
'woocommerce_admin_homepage_default_stats',
819
[

package-lock.json

Lines changed: 61 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
"build-storybook": "build-storybook -c ./storybook",
7878
"changelog": "node ./bin/changelog --changelogSrcType='ZENHUB_RELEASE'",
7979
"bump-version": "npm run -s install-if-deps-outdated && php ./bin/update-version.php",
80-
"wp-env-mysql-port": "node ./docker/wc-admin-wp-env/mysql-port.js"
80+
"wp-env-mysql-port": "node ./docker/wc-admin-wp-env/mysql-port.js",
81+
"create-hook-reference": "node ./bin/hook-reference/index.js"
8182
},
8283
"changelog": {
8384
"labelPrefix": "[Type]",
@@ -176,11 +177,13 @@
176177
"@wordpress/scripts": "12.6.1",
177178
"ast-types": "0.14.2",
178179
"autoprefixer": "10.2.4",
180+
"await-exec": "^0.1.2",
179181
"babel-jest": "26.6.3",
180182
"babel-loader": "8.2.2",
181183
"babel-plugin-transform-class-properties": "6.24.1",
182184
"babel-plugin-transform-es2015-template-literals": "6.22.0",
183185
"chalk": "4.1.0",
186+
"comment-parser": "^1.1.2",
184187
"concurrently": "5.3.0",
185188
"config": "3.3.6",
186189
"copy-webpack-plugin": "5.1.1",

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
122122
- Add: CES survey for importing products #6419
123123
- Add: CES survey for adding product categories, tags, and attributes #6418
124124
- Add: Include tracking for mail poet installs in the selective bundle install #6603
125+
- Dev: Add script automation for gathering hooks and filters. #6454
125126

126127
== 2.1.3 3/14/2021 ==
127128

0 commit comments

Comments
 (0)