From fc403127cfa73f546309dda0012299b628c2fd86 Mon Sep 17 00:00:00 2001 From: Marcus Kazmierczak Date: Thu, 21 Oct 2021 09:17:18 -0700 Subject: [PATCH] Add block.json schema to generated file This adds the schema defintinon to the top of the generated block.json file: "$schema": "https://json.schemastore.org/block.json", This makes it easier to work with the `block.json` file in devleopment, with supported editors it offers tooltips, autocomplete, and schema validation. --- packages/create-block/CHANGELOG.md | 3 +++ packages/create-block/README.md | 1 + packages/create-block/lib/init-block-json.js | 2 ++ packages/create-block/lib/scaffold.js | 2 ++ packages/create-block/lib/templates.js | 1 + 5 files changed, 9 insertions(+) diff --git a/packages/create-block/CHANGELOG.md b/packages/create-block/CHANGELOG.md index a8d54848ad791b..cea0e6800d8bdf 100644 --- a/packages/create-block/CHANGELOG.md +++ b/packages/create-block/CHANGELOG.md @@ -3,9 +3,12 @@ ## Unreleased ### New Features + - Add passing local directories to --template. ([#35645](https://github.com/WordPress/gutenberg/pull/35645)) - Add `slugPascalCase` to the list of variables that can be used in templates ([#35462](https://github.com/WordPress/gutenberg/pull/35462)) +- Add $schema definition to generated `block.json` file. + ## 2.5.0 (2021-07-21) ### Enhancements diff --git a/packages/create-block/README.md b/packages/create-block/README.md index 2ae2ad98590d13..60978c4e792ec6 100644 --- a/packages/create-block/README.md +++ b/packages/create-block/README.md @@ -177,6 +177,7 @@ module.exports = { The following configurable variables are used with the template files. Template authors can change default values to use when users don't provide their data: +- `$schema` (default: `https://json.schemastore.org/block.json`) - `apiVersion` (default: `2`) - see https://make.wordpress.org/core/2020/11/18/block-api-version-2/. - `slug` (no default) - `namespace` (default: `'create-block'`) diff --git a/packages/create-block/lib/init-block-json.js b/packages/create-block/lib/init-block-json.js index 29e65a0b67c7d5..1520c6d0c59104 100644 --- a/packages/create-block/lib/init-block-json.js +++ b/packages/create-block/lib/init-block-json.js @@ -11,6 +11,7 @@ const { writeFile } = require( 'fs' ).promises; const { info } = require( './log' ); module.exports = async ( { + $schema, apiVersion, slug, namespace, @@ -34,6 +35,7 @@ module.exports = async ( { JSON.stringify( omitBy( { + $schema, apiVersion, name: namespace + '/' + slug, version, diff --git a/packages/create-block/lib/scaffold.js b/packages/create-block/lib/scaffold.js index 52551395767331..f5c89bad0768f2 100644 --- a/packages/create-block/lib/scaffold.js +++ b/packages/create-block/lib/scaffold.js @@ -19,6 +19,7 @@ const { code, info, success } = require( './log' ); module.exports = async ( blockTemplate, { + $schema, apiVersion, namespace, slug, @@ -48,6 +49,7 @@ module.exports = async ( const { outputTemplates, outputAssets } = blockTemplate; const view = { + $schema, apiVersion, namespace, namespaceSnakeCase: snakeCase( namespace ), diff --git a/packages/create-block/lib/templates.js b/packages/create-block/lib/templates.js index f2bc47f69a7cf9..cd74938167b3f1 100644 --- a/packages/create-block/lib/templates.js +++ b/packages/create-block/lib/templates.js @@ -185,6 +185,7 @@ const getBlockTemplate = async ( templateName ) => { const getDefaultValues = ( blockTemplate ) => { return { + $schema: 'https://json.schemastore.org/block.json', apiVersion: 2, namespace: 'create-block', category: 'widgets',