Skip to content

Commit

Permalink
Support shorthand scoped templates (#8298)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 authored and mrmckeb committed Jan 12, 2020
1 parent c03bb36 commit fa85f03
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docusaurus/docs/custom-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Custom Templates enable you to select a template to create your project from, wh

You'll notice that Custom Templates are always named in the format `cra-template-[template-name]`, however you only need to provide the `[template-name]` to the creation command.

Scoped templates are also supported, under the name `@[scope-name]/cra-template` or `@[scope-name]/cra-template-[template-name]`, which can be installed via `@[scope]` and `@[scope]/[template-name]` respectively.

### npm

```sh
Expand Down
23 changes: 19 additions & 4 deletions packages/create-react-app/createReactApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,25 @@ function getTemplateInstallPackage(template, originalDirectory) {
const scope = packageMatch[1] || '';
const templateName = packageMatch[2];

const name = templateName.startsWith(templateToInstall)
? templateName
: `${templateToInstall}-${templateName}`;
templateToInstall = `${scope}${name}`;
if (
templateName === templateToInstall ||
templateName.startsWith(`${templateToInstall}-`)
) {
// Covers:
// - cra-template
// - @SCOPE/cra-template
// - cra-template-NAME
// - @SCOPE/cra-template-NAME
templateToInstall = `${scope}${templateName}`;
} else if (templateName.startsWith('@')) {
// Covers using @SCOPE only
templateToInstall = `${templateName}/${templateToInstall}`;
} else {
// Covers templates without the `cra-template` prefix:
// - NAME
// - @SCOPE/NAME
templateToInstall = `${scope}${templateToInstall}-${templateName}`;
}
}
}

Expand Down

0 comments on commit fa85f03

Please sign in to comment.