Skip to content

Commit

Permalink
Add pre-commit hook for handbook data docs
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Dec 9, 2019
1 parent ba13296 commit 14f3a3b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 26 deletions.
38 changes: 38 additions & 0 deletions docs/tool/are-data-files-unstaged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env node

/**
* Node dependencies.
*/
const chalk = require( 'chalk' );
const execSync = require( 'child_process' ).execSync;

/**
* Local dependencies.
*/
const getPackages = require( './packages' );

const getUnstagedFiles = () => execSync( 'git diff --name-only', { encoding: 'utf8' } ).split( '\n' ).filter( ( element ) => '' !== element );

const readmeFiles = getPackages().map( ( [ packageName ] ) => `docs/designers-developers/developers/data/data-${ packageName.replace( '/', '-' ) }.md` );
const unstagedFiles = getUnstagedFiles();

const unstagedReadmes = [];
unstagedFiles.forEach( ( element ) => {
if ( readmeFiles.includes( element ) ) {
unstagedReadmes.push( element );
}
} );

let exitCode = 0;
if ( unstagedReadmes.length > 0 ) {
exitCode = 1;
process.stdout.write( chalk.red(
'\n',
'Some API docs may be out of date:',
unstagedReadmes.toString(),
'Either stage them or continue with --no-verify.',
'\n'
) );
}

process.exit( exitCode );
26 changes: 26 additions & 0 deletions docs/tool/packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const packages = [
[ 'core', {
'Autogenerated actions': 'packages/core-data/src/actions.js',
'Autogenerated selectors': 'packages/core-data/src/selectors.js',
} ],
'core/annotations',
'core/blocks',
'core/block-editor',
'core/editor',
'core/edit-post',
'core/notices',
'core/nux',
'core/viewport',
];

module.exports = function() {
return packages.map( ( entry ) => {
if ( ! Array.isArray( entry ) ) {
entry = [ entry, {
'Autogenerated actions': `packages/${ entry.replace( 'core/', '' ) }/src/store/actions.js`,
'Autogenerated selectors': `packages/${ entry.replace( 'core/', '' ) }/src/store/selectors.js`,
} ];
}
return entry;
} );
};
35 changes: 10 additions & 25 deletions docs/tool/update-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,23 @@
const { join } = require( 'path' );
const spawnSync = require( 'child_process' ).spawnSync;

const modules = [
[ 'core', {
'Autogenerated actions': 'packages/core-data/src/actions.js',
'Autogenerated selectors': 'packages/core-data/src/selectors.js',
} ],
'core/annotations',
'core/blocks',
'core/block-editor',
'core/editor',
'core/edit-post',
'core/notices',
'core/nux',
'core/viewport',
];
/**
* Local dependencies.
*/
const getPackages = require( './packages' );

modules.forEach( ( entry ) => {
if ( ! Array.isArray( entry ) ) {
entry = [ entry, {
'Autogenerated actions': `packages/${ entry.replace( 'core/', '' ) }/src/store/actions.js`,
'Autogenerated selectors': `packages/${ entry.replace( 'core/', '' ) }/src/store/selectors.js`,
} ];
}
const [ namespace, targets ] = entry;
getPackages().forEach( ( entry ) => {
const [ packageName, targetFiles ] = entry;

Object.entries( targets ).forEach( ( [ token, target ] ) => {
Object.entries( targetFiles ).forEach( ( [ token, target ] ) => {
// Note that this needs to be a sync process for each output file that is updated:
// until docgen provides a way to update many tokens at once, we need to make sure
// the output file is updated before starting the second pass for the next token.
const { status, stderr } = spawnSync(
join( __dirname, '..', '..', 'node_modules', '.bin', 'docgen' ).replace( / /g, '\\ ' ),
[
target,
`--output docs/designers-developers/developers/data/data-${ namespace.replace( '/', '-' ) }.md`,
`--output docs/designers-developers/developers/data/data-${ packageName.replace( '/', '-' ) }.md`,
'--to-token',
`--use-token "${ token }"`,
'--ignore "/unstable|experimental/i"',
Expand All @@ -45,7 +29,8 @@ modules.forEach( ( entry ) => {
);

if ( status !== 0 ) {
throw stderr.toString();
process.stderr.write( `${ packageName } ${ stderr.toString() }\n` );
process.exit( 1 );
}
} );
} );
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@
"wp-scripts lint-js"
],
"{docs/{toc.json,tool/*.js},packages/{*/README.md,*/src/{actions,selectors}.js,components/src/*/**/README.md}}": [
"node ./docs/tool/index.js"
"node ./docs/tool/index.js",
"node ./docs/tool/are-data-files-unstaged.js"
],
"packages/**/*.js": [
"node ./bin/api-docs/update-readmes.js",
Expand Down

0 comments on commit 14f3a3b

Please sign in to comment.