-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/autosaves
- Loading branch information
Showing
133 changed files
with
2,434 additions
and
1,103 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
build | ||
build-module | ||
coverage | ||
node_modules | ||
vendor |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Directories/files that may be generated by this project | ||
build | ||
build-module | ||
coverage | ||
/hooks | ||
node_modules | ||
|
This file contains 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
This file contains 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,106 @@ | ||
#!/usr/bin/env node | ||
const parser = require( '../node_modules/pegjs/lib/parser.js' ); | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
const grammarSource = fs.readFileSync( './blocks/api/post.pegjs', 'utf8' ); | ||
const grammar = parser.parse( grammarSource ); | ||
|
||
function escape( text ) { | ||
return text | ||
.replace( /\t/g, '\\t' ) | ||
.replace( /\r/g, '\\r' ) | ||
.replace( /\n/g, '\\n' ) | ||
.replace( /\&/g, '&' ) | ||
.replace( /</g, '<' ); | ||
} | ||
|
||
function isGroup( expression ) { | ||
return [ | ||
'choice', | ||
'action', | ||
'labeled', | ||
'sequence', | ||
].indexOf( expression.type ) >= 0; | ||
} | ||
|
||
function flattenUnary( expression ) { | ||
const shouldWrap = isGroup( expression ); | ||
const inner = flatten( expression ); | ||
return shouldWrap ? '(' + inner + ')' : inner; | ||
} | ||
|
||
function flatten( expression ) { | ||
switch ( expression.type ) { | ||
// Terminal | ||
case 'any': | ||
return '.'; | ||
case 'rule_ref': | ||
return expression.name; | ||
case 'literal': | ||
return '"' + escape( expression.value ) + '"'; | ||
case 'class': | ||
return ( | ||
'[' + ( expression.inverted ? '^' : '' ) + | ||
expression.parts.map( ( part ) => | ||
escape( Array.isArray( part ) ? part.join( '-' ) : part ) | ||
).join( '' ) + | ||
']' + ( expression.ignoreCase ? 'i' : '' ) | ||
); | ||
|
||
// Unary | ||
case 'zero_or_more': | ||
return flattenUnary( expression.expression ) + '*'; | ||
case 'one_or_more': | ||
return flattenUnary( expression.expression ) + '+'; | ||
case 'optional': | ||
return flattenUnary( expression.expression ) + '?'; | ||
case 'simple_not': | ||
return '!' + flattenUnary( expression.expression ); | ||
|
||
// Other groups | ||
case 'sequence': | ||
return expression.elements.map( flatten ).join( ' ' ); | ||
case 'choice': | ||
const sep = expression.isRuleTop ? '\n / ' : ' / '; | ||
return expression.alternatives.map( flatten ).join( sep ); | ||
case 'group': | ||
return '(' + flatten( expression.expression ) + ')'; | ||
case 'text': | ||
// Avoid double parentheses | ||
const inner = flatten( expression.expression ); | ||
const shouldWrap = inner.indexOf( '(' ) !== 0; | ||
return shouldWrap ? '$(' + inner + ')' : '$' + inner; | ||
case 'action': | ||
case 'labeled': | ||
case 'named': | ||
return flatten( expression.expression ); | ||
|
||
// Top-level formatting | ||
case 'grammar': | ||
return `<dl>${ expression.rules.map( flatten ).join( '' ) }</dl>`; | ||
case 'rule': | ||
expression.expression.isRuleTop = true; | ||
const displayName = expression.expression.type === 'named' ? | ||
expression.expression.name : ''; | ||
return `<dt>${ displayName }</dt>` + | ||
`<dd><pre><header>${ expression.name }</header> = ` + | ||
`${ flatten( expression.expression ) }</pre></dd>`; | ||
|
||
default: | ||
throw new Error( JSON.stringify( expression ) ); | ||
} | ||
} | ||
|
||
fs.writeFileSync( | ||
path.join( __dirname, '..', 'docs', 'grammar.md' ), ` | ||
# The Gutenberg block grammar | ||
<style> | ||
dl { display: flex; flex-wrap: wrap; font-size: 110%; } | ||
dt, dd { flex: 40%; margin-bottom: 1em; } | ||
dt { text-align: right; font-style: italic; font-size: 105%; } | ||
dd header { font-weight: bold; } | ||
pre { margin: 0; } | ||
</style> | ||
${ flatten( grammar ) } | ||
` ); |
This file contains 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,124 @@ | ||
/** | ||
* script to build WordPress packages into `build/` directory. | ||
* | ||
* Example: | ||
* node ./scripts/build.js | ||
*/ | ||
|
||
/** | ||
* External Dependenceis | ||
*/ | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
const glob = require( 'glob' ); | ||
const babel = require( 'babel-core' ); | ||
const chalk = require( 'chalk' ); | ||
const mkdirp = require( 'mkdirp' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const getPackages = require( './get-packages' ); | ||
const getBabelConfig = require( './get-babel-config' ); | ||
|
||
/** | ||
* Module Constants | ||
*/ | ||
const PACKAGES_DIR = path.resolve( __dirname, '../../packages' ); | ||
const SRC_DIR = 'src'; | ||
const BUILD_DIR = { | ||
main: 'build', | ||
module: 'build-module', | ||
}; | ||
const DONE = chalk.reset.inverse.bold.green( ' DONE ' ); | ||
|
||
/** | ||
* Get the package name for a specified file | ||
* | ||
* @param {string} file File name | ||
* @return {string} Package name | ||
*/ | ||
function getPackageName( file ) { | ||
return path.relative( PACKAGES_DIR, file ).split( path.sep )[ 0 ]; | ||
} | ||
|
||
/** | ||
* Get Build Path for a specified file | ||
* | ||
* @param {string} file File to build | ||
* @param {string} buildFolder Output folder | ||
* @return {string} Build path | ||
*/ | ||
function getBuildPath( file, buildFolder ) { | ||
const pkgName = getPackageName( file ); | ||
const pkgSrcPath = path.resolve( PACKAGES_DIR, pkgName, SRC_DIR ); | ||
const pkgBuildPath = path.resolve( PACKAGES_DIR, pkgName, buildFolder ); | ||
const relativeToSrcPath = path.relative( pkgSrcPath, file ); | ||
return path.resolve( pkgBuildPath, relativeToSrcPath ); | ||
} | ||
|
||
/** | ||
* Build a file for the required environments (node and ES5) | ||
* | ||
* @param {string} file File path to build | ||
* @param {boolean} silent Show logs | ||
*/ | ||
function buildFile( file, silent ) { | ||
buildFileFor( file, silent, 'main' ); | ||
buildFileFor( file, silent, 'module' ); | ||
} | ||
|
||
/** | ||
* Build a file for a specific environment | ||
* | ||
* @param {string} file File path to build | ||
* @param {boolean} silent Show logs | ||
* @param {string} environment Dist environment (node or es5) | ||
*/ | ||
function buildFileFor( file, silent, environment ) { | ||
const buildDir = BUILD_DIR[ environment ]; | ||
const destPath = getBuildPath( file, buildDir ); | ||
const babelOptions = getBabelConfig( environment ); | ||
|
||
mkdirp.sync( path.dirname( destPath ) ); | ||
const transformed = babel.transformFileSync( file, babelOptions ).code; | ||
fs.writeFileSync( destPath, transformed ); | ||
if ( ! silent ) { | ||
process.stdout.write( | ||
chalk.green( ' \u2022 ' ) + | ||
path.relative( PACKAGES_DIR, file ) + | ||
chalk.green( ' \u21D2 ' ) + | ||
path.relative( PACKAGES_DIR, destPath ) + | ||
'\n' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Build the provided package path | ||
* | ||
* @param {string} packagePath absolute package path | ||
*/ | ||
function buildPackage( packagePath ) { | ||
const srcDir = path.resolve( packagePath, SRC_DIR ); | ||
const files = glob.sync( `${ srcDir }/**/*.js`, { | ||
ignore: `${ srcDir }/**/test/**/*.js`, | ||
nodir: true, | ||
} ); | ||
|
||
process.stdout.write( `${ path.basename( packagePath ) }\n` ); | ||
|
||
files.forEach( ( file ) => buildFile( file, true ) ); | ||
process.stdout.write( `${ DONE }\n` ); | ||
} | ||
|
||
const files = process.argv.slice( 2 ); | ||
|
||
if ( files.length ) { | ||
files.forEach( buildFile ); | ||
} else { | ||
process.stdout.write( chalk.inverse( '>> Building packages \n' ) ); | ||
getPackages() | ||
.forEach( buildPackage ); | ||
process.stdout.write( '\n' ); | ||
} |
This file contains 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,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { isArray, map } = require( 'lodash' ); | ||
const babelPluginTransformReactJSX = require( 'babel-plugin-transform-react-jsx' ); | ||
const babelPresetEnv = require( 'babel-preset-env' ); | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
const babelDefaultConfig = require( '@wordpress/babel-preset-default' ); | ||
|
||
const plugins = map( babelDefaultConfig.plugins, ( plugin ) => { | ||
if ( isArray( plugin ) && plugin[ 0 ] === babelPluginTransformReactJSX ) { | ||
// TODO: It should become the default value when all modules are moved to packages. | ||
return [ babelPluginTransformReactJSX, { pragma: 'createElement' } ]; | ||
} | ||
|
||
return plugin; | ||
} ); | ||
|
||
const babelConfigs = { | ||
main: { | ||
...babelDefaultConfig, | ||
babelrc: false, | ||
plugins, | ||
presets: map( babelDefaultConfig.presets, ( preset ) => { | ||
if ( isArray( preset ) && preset[ 0 ] === babelPresetEnv ) { | ||
return [ babelPresetEnv, Object.assign( | ||
{}, | ||
preset[ 1 ], | ||
{ modules: 'commonjs' } | ||
) ]; | ||
} | ||
return preset; | ||
} ), | ||
}, | ||
module: { | ||
...babelDefaultConfig, | ||
babelrc: false, | ||
plugins, | ||
}, | ||
}; | ||
|
||
function getBabelConfig( environment ) { | ||
return babelConfigs[ environment ]; | ||
} | ||
|
||
module.exports = getBabelConfig; |
This file contains 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,24 @@ | ||
/** | ||
* External Dependenceis | ||
*/ | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
|
||
/** | ||
* Module Constants | ||
*/ | ||
const PACKAGES_DIR = path.resolve( __dirname, '../../packages' ); | ||
|
||
/** | ||
* Returns the absolute path of all WordPress packages | ||
* | ||
* @return {Array} Package paths | ||
*/ | ||
function getPackages() { | ||
return fs | ||
.readdirSync( PACKAGES_DIR ) | ||
.map( ( file ) => path.resolve( PACKAGES_DIR, file ) ) | ||
.filter( ( f ) => fs.lstatSync( path.resolve( f ) ).isDirectory() ); | ||
} | ||
|
||
module.exports = getPackages; |
Oops, something went wrong.