2021-03-17 00:27:26 +00:00
|
|
|
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' );
|
|
|
|
|
2021-07-08 23:01:43 +00:00
|
|
|
const dataTypes = [ 'action', 'filter', 'slotFill' ];
|
|
|
|
|
2021-03-17 00:27:26 +00:00
|
|
|
const getHooks = ( parsedData ) =>
|
|
|
|
parsedData.filter( ( docBlock ) =>
|
2021-07-08 23:01:43 +00:00
|
|
|
docBlock.tags.some( ( tag ) => dataTypes.includes( tag.tag ) )
|
2021-03-17 00:27:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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 } ) => {
|
2021-07-08 23:01:43 +00:00
|
|
|
const hook = tags.find( ( tag ) => dataTypes.includes( tag.tag ) );
|
2021-03-17 00:27:26 +00:00
|
|
|
console.log(
|
2021-07-08 23:01:43 +00:00
|
|
|
chalk.green( `@${ hook.tag } ` ) +
|
|
|
|
chalk.cyan( `${ hook.name } ` ) +
|
2021-03-17 00:27:26 +00:00
|
|
|
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 } ) => {
|
2021-07-08 23:01:43 +00:00
|
|
|
const tag = tags.find( ( tag ) => dataTypes.includes( tag.tag ) );
|
|
|
|
|
2022-02-11 14:38:38 +00:00
|
|
|
paramTags = tags.reduce(
|
|
|
|
( result, { tag, name, type, description } ) => {
|
|
|
|
if ( tag === 'param' ) {
|
|
|
|
result.push( {
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
description,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const docObject = {
|
2021-03-17 00:27:26 +00:00
|
|
|
description,
|
|
|
|
sourceFile,
|
2021-07-08 23:01:43 +00:00
|
|
|
name: tag ? tag.name : '',
|
|
|
|
type: tag.tag,
|
2022-02-11 14:38:38 +00:00
|
|
|
params: paramTags,
|
2021-03-17 00:27:26 +00:00
|
|
|
};
|
2022-02-11 14:38:38 +00:00
|
|
|
|
|
|
|
if ( tag.tag === 'slotFill' ) {
|
|
|
|
const scopeTab = tags.find( ( tag ) => tag.tag === 'scope' );
|
|
|
|
if ( scopeTab ) {
|
|
|
|
docObject.scope = scopeTab.name;
|
|
|
|
} else {
|
|
|
|
console.warn(
|
|
|
|
`Failed to find "scope" tag for slotFill "${ tag.name }" doc.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return docObject;
|
2021-03-17 00:27:26 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const createData = async ( paths ) => {
|
|
|
|
const data = await Promise.all(
|
|
|
|
paths.map( async ( path ) => {
|
|
|
|
return await makeDocObjects( path );
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
return data.flat();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = createData;
|