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

Create Block: Allow using locally installed packages with templates #28105

Merged
merged 2 commits into from
Jan 12, 2021
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
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