Skip to content

Instantly share code, notes, and snippets.

@mrclay
Last active April 11, 2024 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrclay/f1a949d1be18a44281a1556fe2765971 to your computer and use it in GitHub Desktop.
Save mrclay/f1a949d1be18a44281a1556fe2765971 to your computer and use it in GitHub Desktop.
Script to generate a codegen-friendly CoreBlocksFragment.ts file from Faust's blocks.
/**
* Generates file ./utils/queries/WpBlocksFragment.ts containing
* a fragment with all the WP core block fragments Faust provides.
*
* USAGE: ts-node block-fragments.ts
*/
import { CoreBlocks } from '@faustwp/blocks';
import { glob } from 'glob';
import { mkdirSync, writeFileSync } from 'node:fs';
import { pathToFileURL } from 'node:url';
async function go() {
const filePath = pathToFileURL(__filename).toString();
const outDir = new URL('./utils/queries', filePath).pathname;
const outPath = `${outDir}/WpBlocksFragment.ts`;
const fragments: string[] = [];
// Core fragment sources
for (const v of Object.values(CoreBlocks)) {
if (v.fragments.entry.loc) {
fragments.push(v.fragments.entry.loc.source.body);
}
}
const fragmentFiles = await glob('wp-blocks/**/fragments.ts', {
absolute: true,
withFileTypes: false,
});
// Some fragments will depend on others. We will gather them here and output
// them directly before the WpBlocksFragment definition.
const prefixFragments: string[] = [];
// Custom fragment sources
const customFragmentModules = await Promise.all(
fragmentFiles.map(file => import(file)),
);
for (const module of customFragmentModules) {
fragments.push(module.default.entry.loc.source.body);
// If set, fragments.dependencies should be an array of fragment definition
// objects (each with .key and .entry).
if (module.default.dependencies) {
for (const dep of module.default.dependencies) {
prefixFragments.push(dep.entry.loc.source.body);
}
}
}
const lines: string[] = [];
lines.push('// Do not alter this file. Instead, use `npm run generate:all');
lines.push('/* eslint-disable */');
lines.push('');
lines.push("import { gql } from '@apollo/client';");
lines.push('');
lines.push(`const WpBlocksFragment = gql\`
${prefixFragments.join('\n')}
fragment WpBlocksFragment on EditorBlock {
__typename
clientId
name
renderedHtml
parentClientId
`);
for (const fragment of fragments) {
lines.push(' ' + fragment.trim().replace(/fragment \w+/, '...'));
}
lines.push(' }');
lines.push('`;');
lines.push('');
lines.push('export default WpBlocksFragment;');
lines.push('');
mkdirSync(outDir, { recursive: true });
writeFileSync(outPath, lines.join('\n'), { encoding: 'utf8' });
}
go().catch(err => console.error(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment