Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support shorthand scoped templates #8298

Merged
merged 3 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
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
21 changes: 17 additions & 4 deletions packages/create-react-app/createReactApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,23 @@ 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}-`)
) {
// cra-template
// @SCOPE/cra-template
// cra-template-NAME
// @SCOPE/cra-template-NAME
Copy link
Contributor

Choose a reason for hiding this comment

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

As above... :)

templateToInstall = `${scope}${templateName}`;
} else if (templateName.startsWith('@')) {
// @SCOPE
templateToInstall = `${templateName}/${templateToInstall}`;
} else {
// NAME
// @SCOPE/NAME
templateToInstall = `${scope}${templateToInstall}-${templateName}`;
}
}
}

Expand Down