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

Add --template option to create-strapi-app #8112

Merged
merged 17 commits into from Oct 2, 2020

Conversation

remidej
Copy link
Contributor

@remidej remidej commented Sep 30, 2020

Description of what I did

I added a --template option to the create-strapi-app CLI.

Right now, every project created with create-strapi-app is an empty Strapi project. This will remain the default, but we also want to help users bootstrap projects that are preconfigured for specific use cases. After discussions with @alexandrebodin and @Convly , we designed a specification for this that relies on a new template system.

It looks like this:

# Using yarn
yarn create strapi-app my-strapi-app --template <template-github-url>

# Using npm
npx create-strapi-app my-strapi-app --template <template-github-url>

A template's only concern should be to adapt Strapi to a use case. It should not deal with environment-specific configs, like databases, upload or email providers. This is to make sure they are maintainable, and to avoid conflicts with other CLI options like --quickstart.

These templates are meant to be an important piece of our upcoming starters system. To clarify:

  • A template is a pre-made Strapi configuration
  • A starter is a pre-made frontend application that consumes a Strapi API

How it works

A template is a GitHub repository. This way, it's fully open. Any community member can create a template, not just the Strapi team.

Inspired by create-react-app, here is the structure a template should follow:

.
├── README.md
├── .gitignore
├── template.json
├── template
│   ├── README.md
│   ├── .env.example
│   ├── api/*
│   ├── components/*
│   ├── config
│   │   └── functions/*
│   ├── data/*
│   ├── plugins/*
│   ├── public/*
│   └── scripts/*
  • README.md and .gitignore are meta files: They only affect the template itself, not the projects made with it.
  • template.json is used to extend the default package.json of a Strapi project. We can use it to add scripts and dependencies.
  • template/ is the folder where you can extend the file contents of a Strapi project. It can contain any of the following:
    • README.md: the readme of app made with this template
    • .env.example: to specify required environment variables
    • api/: for collections and single types
    • components/ for components
    • config/ can only include the functions directory (things like bootstrap.js or 404.js). That's because other config files are environment-specific.
    • data/ to store the data imported by a seed script
    • plugins/ for custom Strapi plugins
    • public/ to serve files
    • scripts/ for custom scripts

If a template doesn't follow this structure, the installation will crash.

Try it out

You can try it by calling the node script locally:

# Template that adds an Article collection
node ./path-to-this-strapi-fork/packages/create-strapi-app/index.js my-strapi-app --template https://github.com/remi2j/strapi-template-dummy-article

# Template that will crash because it doesn't follow the structure
node ./path-to-this-strapi-fork/packages/create-strapi-app/index.js my-strapi-app --template https://github.com/remi2j/strapi-template-dummy-illegal

Things to figure out

  • There are already some things called "templates" in the CLI for database configs. We should find a new name for these to avoid confusion.
  • Should seed data be imported automatically? Or should it be left as an additional command?

Signed-off-by: Rémi de Juvigny <remi@hey.com>
Signed-off-by: Rémi de Juvigny <remi@hey.com>
Signed-off-by: Rémi de Juvigny <remi@hey.com>
Signed-off-by: Rémi de Juvigny <remi@hey.com>
Signed-off-by: Rémi de Juvigny <remi@hey.com>
Signed-off-by: Rémi de Juvigny <remi@hey.com>
Signed-off-by: Rémi de Juvigny <remi@hey.com>
Signed-off-by: Rémi de Juvigny <remi@hey.com>
Signed-off-by: Rémi de Juvigny <remi@hey.com>
@alexandrebodin alexandrebodin added source: core:strapi Source is core/strapi package issue: enhancement Issue suggesting an enhancement to an existing feature labels Sep 30, 2020
@alexandrebodin alexandrebodin added this to the 3.2.0 milestone Sep 30, 2020
@remidej
Copy link
Contributor Author

remidej commented Sep 30, 2020

See more discussion about this PR at #7626

@strapi strapi deleted a comment from codecov bot Oct 2, 2020
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
@codecov
Copy link

codecov bot commented Oct 2, 2020

Codecov Report

Merging #8112 into master will increase coverage by 0.24%.
The diff coverage is 49.27%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #8112      +/-   ##
==========================================
+ Coverage   32.71%   32.96%   +0.24%     
==========================================
  Files        1194     1197       +3     
  Lines       12969    13020      +51     
  Branches     1280     1286       +6     
==========================================
+ Hits         4243     4292      +49     
+ Misses       7886     7885       -1     
- Partials      840      843       +3     
Flag Coverage Δ
#front 25.06% <28.57%> (+0.25%) ⬆️
#unit 53.88% <51.61%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...trapi-admin/admin/src/containers/HomePage/index.js 62.50% <ø> (ø)
...n/src/containers/NotificationProvider/selectors.js 0.00% <0.00%> (ø)
...-manager/admin/src/components/Wysiwyg/constants.js 0.00% <ø> (ø)
...nt-manager/admin/src/components/Wysiwyg/helpers.js 0.00% <ø> (ø)
...lder/admin/src/containers/FormModal/utils/forms.js 22.03% <ø> (+22.03%) ⬆️
...pi-plugin-content-type-builder/utils/attributes.js 30.00% <ø> (ø)
...cumentation/admin/src/containers/HomePage/index.js 0.00% <ø> (ø)
...ugin-upload/admin/src/components/EditForm/index.js 0.00% <0.00%> (ø)
.../containers/InputModalStepper/InputModalStepper.js 0.00% <ø> (ø)
.../src/containers/InputModalStepperProvider/index.js 0.00% <0.00%> (ø)
... and 30 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 012902d...66e8579. Read the comment docs.

Signed-off-by: Rémi de Juvigny <remi@hey.com>
Copy link
Member

@alexandrebodin alexandrebodin left a comment

Choose a reason for hiding this comment

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

LGTM. Good job 💯

@alexandrebodin alexandrebodin merged commit 8a407cc into master Oct 2, 2020
@alexandrebodin alexandrebodin deleted the features/templates-cli branch October 2, 2020 14:46
@bernardro
Copy link

hey @remidej @alexandrebodin - thanks for this
I noticed the documentation says this:

A template is only useful once applied on top of a default Strapi app via the CLI

When I look at the CLI documentation I only see this

strapi new <name> --dbclient=<dbclient> --dbhost=<dbhost> --dbport=<dbport> --dbname=<dbname> --dbusername=<dbusername> --dbpassword=<dbpassword> --dbssl=<dbssl> --dbauth=<dbauth> --dbforce

is create-strapi-app --template the only way to apply a template for now?

@alexandrebodin
Copy link
Member

Hi @bernardro indeed create-strapi-app is the one you need to be using for this.

@bernardro
Copy link

thank you @alexandrebodin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: enhancement Issue suggesting an enhancement to an existing feature source: core:strapi Source is core/strapi package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants