-
Notifications
You must be signed in to change notification settings - Fork 4.7k
docgen: Optimize README update script #18840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2528636
Framework: docgen: Optimize README update script
aduth 70bf8ba
Build Tools: Refactor precommit script to read for tokens
aduth eec04be
Build Tools: docgen: Resolve source relative documentation
aduth 6dd4e96
Build Tools: Docgen: Ensure unique package patterns
aduth c51750f
Build Tools: Docgen: Remove space normalization
aduth e0d02ef
Fix path
oandregal 50ce42a
Fix core-data name match
oandregal cbd58ea
Fix JSDoc for exported declaration
oandregal 36823f7
Remove unnecessary process
oandregal 055da58
Rename for better semantics
oandregal 1fac42c
Fix lint-stage syntax
oandregal 6b35cb3
Remove duplicated
oandregal cc94645
Remove unnecessary try/catch
oandregal 23cad80
Extract default path to constant
oandregal 1658348
Add use cases regexp is trying to match
oandregal 62ad0f0
Remove unnecessary functions
oandregal 278ae89
Update docgen docs so they arent picked up by the pre-commit hook
oandregal 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * Node dependencies. | ||
| */ | ||
| const { extname } = require( 'path' ); | ||
| const chalk = require( 'chalk' ); | ||
| const execSync = require( 'child_process' ).execSync; | ||
| const { readFile } = require( 'fs' ).promises; | ||
|
|
||
| const getUnstagedFiles = () => | ||
| execSync( 'git diff --name-only', { encoding: 'utf8' } ) | ||
| .split( '\n' ) | ||
| .filter( Boolean ); | ||
|
|
||
| const fileHasToken = async ( file ) => | ||
| ( await readFile( file, 'utf8' ) ).includes( '<!-- START TOKEN' ); | ||
|
|
||
| const getUnstagedReadmes = () => | ||
| Promise.all( | ||
| getUnstagedFiles().map( | ||
| async ( file ) => | ||
| extname( file ) === '.md' && | ||
| ( await fileHasToken( file ) ) && | ||
| file | ||
| ) | ||
| ).then( ( files ) => files.filter( Boolean ) ); | ||
|
|
||
| ( async () => { | ||
| const unstagedReadmes = await getUnstagedReadmes(); | ||
| if ( unstagedReadmes.length > 0 ) { | ||
| process.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' | ||
| ) | ||
| ); | ||
| } | ||
| } )(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| const { join, relative, resolve, sep, dirname } = require( 'path' ); | ||
| const glob = require( 'fast-glob' ); | ||
| const execa = require( 'execa' ); | ||
| const { Transform } = require( 'stream' ); | ||
| const { readFile } = require( 'fs' ).promises; | ||
|
|
||
| /** | ||
| * README file tokens, defined as a tuple of token identifier, source path. | ||
| * | ||
| * @typedef {[string,string]} WPReadmeFileTokens | ||
| */ | ||
|
|
||
| /** | ||
| * README file data, defined as a tuple of README file path, token details. | ||
| * | ||
| * @typedef {[string,WPReadmeFileTokens]} WPReadmeFileData | ||
| */ | ||
|
|
||
| /** | ||
| * Path to root project directory. | ||
| * | ||
| * @type {string} | ||
| */ | ||
| const ROOT_DIR = resolve( __dirname, '../..' ); | ||
|
|
||
| /** | ||
| * Path to packages directory. | ||
| * | ||
| * @type {string} | ||
| */ | ||
| const PACKAGES_DIR = resolve( ROOT_DIR, 'packages' ); | ||
|
|
||
| /** | ||
| * Path to data documentation directory. | ||
| * | ||
| * @type {string} | ||
| */ | ||
| const DATA_DOCS_DIR = resolve( | ||
| ROOT_DIR, | ||
| 'docs/designers-developers/developers/data' | ||
| ); | ||
|
|
||
| /** | ||
| * Default path to use if the token doesn't include one. | ||
| * | ||
| * @see TOKEN_PATTERN | ||
| */ | ||
| const DEFAULT_PATH = 'src/index.js'; | ||
|
|
||
| /** | ||
| * Pattern matching start token of a README file. | ||
| * | ||
| * @example Delimiter tokens that use the default source file: | ||
| * <!-- START TOKEN(Autogenerated API docs) --> | ||
| * // content within will be filled by docgen | ||
| * <!-- END TOKEN(Autogenerated API docs) --> | ||
| * | ||
| * @example Delimiter tokens that use a specific source file: | ||
| * <!-- START TOKEN(Autogenerated actions|src/actions.js) --> | ||
| * // content within will be filled by docgen | ||
| * <!-- END TOKEN(Autogenerated actions|src/actions.js) --> | ||
| * | ||
| * @type {RegExp} | ||
| * @see DEFAULT_PATH | ||
| */ | ||
| const TOKEN_PATTERN = /<!-- START TOKEN\((.+?(?:\|(.+?))?)\) -->/g; | ||
|
|
||
| /** | ||
| * Given an absolute file path, returns the package name. | ||
| * | ||
| * @param {string} file Absolute path. | ||
| * | ||
| * @return {string} Package name. | ||
| */ | ||
| function getFilePackage( file ) { | ||
| return relative( PACKAGES_DIR, file ).split( sep )[ 0 ]; | ||
| } | ||
|
|
||
| /** | ||
| * Returns an appropriate glob pattern for the packages directory to match | ||
| * relevant documentation files for a given set of files. | ||
| * | ||
| * @param {string[]} files Set of files to match. Pass an empty set to match | ||
| * all packages. | ||
| * | ||
| * @return {string} Packages glob pattern. | ||
| */ | ||
| function getPackagePattern( files ) { | ||
| if ( ! files.length ) { | ||
| return '*'; | ||
| } | ||
|
|
||
| // Since brace expansion doesn't work with a single package, special-case | ||
| // the pattern for the singular match. | ||
| const packages = Array.from( new Set( files.map( getFilePackage ) ) ); | ||
| return packages.length === 1 ? packages[ 0 ] : '{' + packages.join() + '}'; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the conventional store name of a given package. | ||
| * | ||
| * @param {string} packageName Package name. | ||
| * | ||
| * @return {string} Store name. | ||
| */ | ||
| function getPackageStoreName( packageName ) { | ||
| let storeName = 'core'; | ||
| if ( packageName !== 'core-data' ) { | ||
| storeName += '/' + packageName; | ||
| } | ||
|
|
||
| return storeName; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the conventional documentation file name of a given package. | ||
| * | ||
| * @param {string} packageName Package name. | ||
| * | ||
| * @return {string} Documentation file name. | ||
| */ | ||
| function getDataDocumentationFile( packageName ) { | ||
| const storeName = getPackageStoreName( packageName ); | ||
| return `data-${ storeName.replace( '/', '-' ) }.md`; | ||
| } | ||
|
|
||
| /** | ||
| * Returns an appropriate glob pattern for the data documentation directory to | ||
| * match relevant documentation files for a given set of files. | ||
| * | ||
| * @param {string[]} files Set of files to match. Pass an empty set to match | ||
| * all packages. | ||
| * | ||
| * @return {string} Packages glob pattern. | ||
| */ | ||
| function getDataDocumentationPattern( files ) { | ||
| if ( ! files.length ) { | ||
| return '*'; | ||
| } | ||
|
|
||
| // Since brace expansion doesn't work with a single package, special-case | ||
| // the pattern for the singular match. | ||
| const filePackages = Array.from( new Set( files.map( getFilePackage ) ) ); | ||
| const docFiles = filePackages.map( getDataDocumentationFile ); | ||
|
|
||
| return docFiles.length === 1 ? docFiles[ 0 ] : '{' + docFiles.join() + '}'; | ||
| } | ||
|
|
||
| /** | ||
| * Stream transform which filters out README files to include only those | ||
| * containing matched token pattern, yielding a tuple of the file and its | ||
| * matched tokens. | ||
| * | ||
| * @type {Transform} | ||
| */ | ||
| const filterTokenTransform = new Transform( { | ||
| objectMode: true, | ||
|
|
||
| async transform( file, _encoding, callback ) { | ||
| let content; | ||
| try { | ||
| content = await readFile( file, 'utf8' ); | ||
| } catch {} | ||
|
|
||
| if ( content ) { | ||
| const tokens = []; | ||
|
|
||
| for ( const match of content.matchAll( TOKEN_PATTERN ) ) { | ||
| const [ , token, path = DEFAULT_PATH ] = match; | ||
| tokens.push( [ token, path ] ); | ||
| } | ||
|
|
||
| if ( tokens.length ) { | ||
| this.push( [ file, tokens ] ); | ||
| } | ||
| } | ||
|
|
||
| callback(); | ||
| }, | ||
| } ); | ||
|
|
||
| /** | ||
| * Optional process arguments for which to generate documentation. | ||
| * | ||
| * @type {string[]} | ||
| */ | ||
| const files = process.argv.slice( 2 ); | ||
|
|
||
| glob.stream( [ | ||
| `${ PACKAGES_DIR }/${ getPackagePattern( files ) }/README.md`, | ||
| `${ DATA_DOCS_DIR }/${ getDataDocumentationPattern( files ) }`, | ||
| ] ) | ||
| .pipe( filterTokenTransform ) | ||
| .on( 'data', async ( /** @type {WPReadmeFileData} */ data ) => { | ||
| const [ file, tokens ] = data; | ||
| const output = relative( ROOT_DIR, file ); | ||
|
|
||
| // Each file can have more than one placeholder content to update, each | ||
| // represented by tokens. The docgen script updates one token at a time, | ||
| // so the tokens must be replaced in sequence to prevent the processes | ||
| // from overriding each other. | ||
| for ( const [ token, path ] of tokens ) { | ||
| await execa( | ||
| join( __dirname, '..', '..', 'node_modules', '.bin', 'docgen' ), | ||
| [ | ||
| relative( ROOT_DIR, resolve( dirname( file ), path ) ), | ||
| `--output ${ output }`, | ||
| '--to-token', | ||
| `--use-token "${ token }"`, | ||
| '--ignore "/unstable|experimental/i"', | ||
| ], | ||
| { shell: true } | ||
| ); | ||
| } | ||
| } ); | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.