Skip to content

Commit

Permalink
Create Block: Allow using locally installed packages with templates (#…
Browse files Browse the repository at this point in the history
…28105)

* Create Block: Allow using locally installed packages with templates

* Add CHANGELOG entry
  • Loading branch information
gziolo authored Jan 12, 2021
1 parent 59923c1 commit d466acc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### New Features

- Add support for handling static assets with the `assetsPath` field in the external template configuration ([#28038](https://github.com/WordPress/gutenberg/pull/28038)).
- Allow using locally installed packages with templates ([#28105](https://github.com/WordPress/gutenberg/pull/28105)).

### Internal

Expand Down
72 changes: 46 additions & 26 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const predefinedBlockTemplates = {
editorStyle: 'file:./editor.css',
style: 'file:./style.css',
},
templatesPath: join( __dirname, 'templates', 'es5' ),
},
esnext: {
defaultValues: {
Expand All @@ -43,6 +44,7 @@ const predefinedBlockTemplates = {
'@wordpress/i18n',
],
},
templatesPath: join( __dirname, 'templates', 'esnext' ),
},
};

Expand Down Expand Up @@ -94,16 +96,41 @@ const externalTemplateExists = async ( templateName ) => {
return true;
};

const configToTemplate = async ( {
assetsPath,
defaultValues = {},
templatesPath,
} ) => {
if ( ! isObject( defaultValues ) || ! templatesPath ) {
throw new CLIError( 'Template found but invalid definition provided.' );
}

return {
defaultValues,
outputAssets: assetsPath ? await getOutputAssets( assetsPath ) : {},
outputTemplates: await getOutputTemplates( templatesPath ),
};
};

const getBlockTemplate = async ( templateName ) => {
if ( predefinedBlockTemplates[ templateName ] ) {
return {
...predefinedBlockTemplates[ templateName ],
outputTemplates: await getOutputTemplates(
join( __dirname, 'templates', templateName )
),
outputAssets: {},
};
return await configToTemplate(
predefinedBlockTemplates[ templateName ]
);
}

try {
return await configToTemplate( require( templateName ) );
} catch ( error ) {
if ( error instanceof CLIError ) {
throw error;
} else if ( error.code !== 'MODULE_NOT_FOUND' ) {
throw new CLIError(
`Invalid block template loaded. Error: ${ error.message }`
);
}
}

if ( ! ( await externalTemplateExists( templateName ) ) ) {
throw new CLIError(
`Invalid block template type name: "${ templateName }". Allowed values: ` +
Expand All @@ -126,26 +153,19 @@ const getBlockTemplate = async ( templateName ) => {
cwd: tempCwd,
} );

const {
defaultValues = {},
templatesPath,
assetsPath,
} = require( require.resolve( templateName, {
paths: [ tempCwd ],
} ) );
if ( ! isObject( defaultValues ) || ! templatesPath ) {
throw new Error();
}

return {
defaultValues,
outputTemplates: await getOutputTemplates( templatesPath ),
outputAssets: assetsPath ? await getOutputAssets( assetsPath ) : {},
};
} catch ( error ) {
throw new CLIError(
`Invalid template definition provided in "${ templateName }" package.`
return await configToTemplate(
require( require.resolve( templateName, {
paths: [ tempCwd ],
} ) )
);
} catch ( error ) {
if ( error instanceof CLIError ) {
throw error;
} else {
throw new CLIError(
`Invalid block template downloaded. Error: ${ error.message }`
);
}
} finally {
if ( tempCwd ) {
rimraf( tempCwd );
Expand Down

0 comments on commit d466acc

Please sign in to comment.