Skip to content
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

Testing: Add verification for Create Block to Continues Integration #23195

Merged
merged 7 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/create-block.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Create Block

on:
push:
paths:
- 'packages/**'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still unsure of what would be important here, maybe package*.json to cover the case when dependencies change. Although it would probably mean that one of the packages changed as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially thought about only adding @WordPress dependencies for create-block but it has none. 🙈

In this case probably all of

"@wordpress/babel-preset-default": "file:../babel-preset-default",
"@wordpress/dependency-extraction-webpack-plugin": "file:../dependency-extraction-webpack-plugin",
"@wordpress/eslint-plugin": "file:../eslint-plugin",
"@wordpress/jest-preset-default": "file:../jest-preset-default",
"@wordpress/npm-package-json-lint-config": "file:../npm-package-json-lint-config",
"@wordpress/postcss-plugins-preset": "file:../postcss-plugins-preset",
"@wordpress/prettier-config": "file:../prettier-config",
, packages/scripts/package.json and packages/create-block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also @wordpress/browserlist-config or @wordpress/element that is used by Babel. I said, it might be tricky to identify all dependencies used :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we add new configs, like @wordpress/styleling-config as proposed by @ntwb in #22777, we would have to update this list. I would personally prefer to extend the list of excluded files rather than allow list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the current list and see how it goes.

- '!packages/**/test/**'
- '!packages/**/*.md'

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: 12.x
- name: npm install, build, format and lint
run: |
npm ci
npm run test:create-block
env:
CI: true
38 changes: 38 additions & 0 deletions bin/test-create-block.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# This script validates whether `npm init @wordpress/block` works properly
# with the latest changes applied to the `master` branch. It purposefully
# avoids installing `@wordpress/scripts` package from npm when scaffolding
# a test block and uses the local package by executing everything from the
# root of the project.

# Exit if any command fails.
set -e

DIRECTORY="$PWD"

status () {
echo -e "\n\033[1;34m$1\033[0m\n"
}

cleanup() {
rm -rf "$DIRECTORY/esnext-test"
}

trap cleanup EXIT

status "Scaffolding block..."
npx wp-create-block esnext-test --no-wp-scripts
cd esnext-test

status "Formatting JavaScript files..."
../node_modules/.bin/wp-scripts format-js

status "Building block..."
../node_modules/.bin/wp-scripts build

status "Lintig CSS files..."
../node_modules/.bin/wp-scripts lint-style

status "Linting JavaScript files..."
../node_modules/.bin/wp-scripts lint-js
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
"publish:patch": "npm run clean:package-types && npm run build:packages && lerna publish --dist-tag patch",
"publish:prod": "npm run clean:package-types && npm run build:packages && lerna publish",
"test": "npm run lint && npm run test-unit",
"test:create-block": "./bin/test-create-block.sh",
"test-e2e": "wp-scripts test-e2e --config packages/e2e-tests/jest.config.js",
"test-e2e:debug": "wp-scripts --inspect-brk test-e2e --config packages/e2e-tests/jest.config.js --puppeteer-devtools",
"test-e2e:watch": "npm run test-e2e -- --watch",
Expand Down
4 changes: 4 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Features

- Add new CLI options: `--no-wp-scripts` and `--wp-scripts` to let users override the settings that template defines for supports for `@wordpress/scripts` package integration ([#23195](https://github.com/WordPress/gutenberg/pull/23195)).

## 0.14.2 (2020-06-16)

### Bug Fix
Expand Down
2 changes: 2 additions & 0 deletions packages/create-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Options:
--title <value> display title for the block
--short-description <value> short description for the block
--category <name> category name for the block
--wp-scripts enable integration with `@wordpress/scripts` package
--no-wp-scripts disable integration with `@wordpress/scripts` package
-h, --help output usage information
```

Expand Down
26 changes: 20 additions & 6 deletions packages/create-block/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ program
// The name "description" is used internally so it couldn't be used.
.option( '--short-description <value>', 'short description for the block' )
.option( '--category <name>', 'category name for the block' )
.option(
'--wp-scripts',
'enable integration with `@wordpress/scripts` package'
)
.option(
'--no-wp-scripts',
'disable integration with `@wordpress/scripts` package'
)
.action(
async (
slug,
Expand All @@ -51,18 +59,24 @@ program
shortDescription: description,
template: templateName,
title,
wpScripts,
}
) => {
await checkSystemRequirements( engines );
try {
const blockTemplate = await getBlockTemplate( templateName );
const defaultValues = getDefaultValues( blockTemplate );
const optionsValues = pickBy( {
category,
description,
namespace,
title,
} );
const optionsValues = pickBy(
{
category,
description,
namespace,
title,
wpScripts,
},
( value ) => value !== undefined
);

if ( slug ) {
const answers = {
...defaultValues,
Expand Down
47 changes: 47 additions & 0 deletions packages/create-block/lib/init-package-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* External dependencies
*/
const { isEmpty, omitBy } = require( 'lodash' );
const { join } = require( 'path' );
const writePkg = require( 'write-pkg' );

/**
* Internal dependencies
*/
const { info } = require( './log' );

module.exports = async ( {
author,
description,
license,
slug,
version,
wpScripts,
} ) => {
const cwd = join( process.cwd(), slug );

info( '' );
info( 'Creating a "package.json" file.' );
await writePkg(
cwd,
omitBy(
{
name: slug,
version,
description,
author,
license,
main: wpScripts && 'build/index.js',
scripts: wpScripts && {
build: 'wp-scripts build',
'format:js': 'wp-scripts format-js',
'lint:css': 'wp-scripts lint-style',
'lint:js': 'wp-scripts lint-js',
start: 'wp-scripts start',
'packages-update': 'wp-scripts packages-update',
},
},
isEmpty
)
);
};
29 changes: 1 addition & 28 deletions packages/create-block/lib/init-wp-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,16 @@
* External dependencies
*/
const { command } = require( 'execa' );
const { isEmpty, omitBy } = require( 'lodash' );
const { join } = require( 'path' );
const writePkg = require( 'write-pkg' );

/**
* Internal dependencies
*/
const { info } = require( './log' );

module.exports = async ( { author, description, license, slug, version } ) => {
module.exports = async ( { slug } ) => {
const cwd = join( process.cwd(), slug );

info( '' );
info( 'Creating a "package.json" file.' );
await writePkg(
cwd,
omitBy(
{
name: slug,
version,
description,
author,
license,
main: 'build/index.js',
scripts: {
build: 'wp-scripts build',
'format:js': 'wp-scripts format-js',
'lint:css': 'wp-scripts lint-style',
'lint:js': 'wp-scripts lint-js',
start: 'wp-scripts start',
'packages-update': 'wp-scripts packages-update',
},
},
isEmpty
)
);

info( '' );
info( 'Installing packages. It might take a couple of minutes.' );
await command( 'npm install @wordpress/scripts --save-dev', {
Expand Down
9 changes: 6 additions & 3 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const { dirname } = require( 'path' );
/**
* Internal dependencies
*/
const initPackageJSON = require( './init-package-json' );
const initWPScripts = require( './init-wp-scripts' );
const { code, info, success } = require( './log' );
const { hasWPScriptsEnabled } = require( './templates' );

module.exports = async (
blockTemplate,
Expand All @@ -27,6 +27,7 @@ module.exports = async (
license,
licenseURI,
version,
wpScripts,
}
) => {
slug = slug.toLowerCase();
Expand Down Expand Up @@ -66,15 +67,17 @@ module.exports = async (
} )
);

if ( hasWPScriptsEnabled( blockTemplate ) ) {
await initPackageJSON( view );

if ( wpScripts ) {
await initWPScripts( view );
}

info( '' );
success(
`Done: block "${ title }" bootstrapped in the "${ slug }" folder.`
);
if ( hasWPScriptsEnabled( blockTemplate ) ) {
if ( wpScripts ) {
info( '' );
info( 'Inside that directory, you can run several commands:' );
info( '' );
Expand Down
8 changes: 2 additions & 6 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const predefinedBlockTemplates = {
title: 'ES5 Example',
description:
'Example block written with ES5 standard and no JSX – no build step required.',
wpScripts: false,
},
wpScripts: false,
},
esnext: {
defaultValues: {
Expand Down Expand Up @@ -78,6 +78,7 @@ const getDefaultValues = ( blockTemplate ) => {
license: 'GPL-2.0-or-later',
licenseURI: 'https://www.gnu.org/licenses/gpl-2.0.html',
version: '0.1.0',
wpScripts: true,
...blockTemplate.defaultValues,
};
};
Expand All @@ -92,13 +93,8 @@ const getPrompts = ( blockTemplate ) => {
} );
};

const hasWPScriptsEnabled = ( blockTemplate ) => {
return blockTemplate.wpScripts !== false;
};

module.exports = {
getBlockTemplate,
getDefaultValues,
getPrompts,
hasWPScriptsEnabled,
};