Skip to content

Gatsby cookiecutter template#85

Merged
beamalsky merged 13 commits into
masterfrom
gatsby-cookiecutter
Sep 28, 2020
Merged

Gatsby cookiecutter template#85
beamalsky merged 13 commits into
masterfrom
gatsby-cookiecutter

Conversation

@beamalsky
Copy link
Copy Markdown
Contributor

@beamalsky beamalsky commented Jun 18, 2020

Overview

This PR adds a cookiecutter template for a DataMade-style Gatsby app to docker/templates. It's based on gatsby-starter-default but includes Bootstrap setup, a Github Action for linting, and is containerized.

@jeancochrane or @hancush, I'd love to hear your thoughts on the containerization here. When I'm developing a Gatsby project and add or delete a dependency, I have to stop and rebuild the container. Is there a way I can modify my Dockerfile here so that it live updates node_modules?

Testing

  • Follow the instructions in the README here to set up a new Gatsby app
  • Follow the 🚀 Quick start guide in your new directory's README to run the site

@beamalsky beamalsky changed the title Start of a Gatsby cookiecutter Gatsby cookiecutter template Jun 18, 2020
@beamalsky beamalsky requested review from hancush and jeancochrane and removed request for hancush August 13, 2020 22:58
Copy link
Copy Markdown
Member

@hancush hancush left a comment

Choose a reason for hiding this comment

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

Thanks for this, @beamalsky!

I'm getting an error when I try to build the cookiecutter:

call-me-hank:templates hannah$ docker-compose run --rm cookiecutter -f new-gatsby-app
app_name [my-new-gatsby-app]:
app_verbose_name [My New Gatsby App's Title]:
description [A longer description of my new app]:
Traceback (most recent call last):
  File "/usr/local/bin/cookiecutter", line 10, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/cookiecutter/cli.py", line 120, in main
    password=os.environ.get('COOKIECUTTER_REPO_PASSWORD')
  File "/usr/local/lib/python3.7/site-packages/cookiecutter/main.py", line 94, in cookiecutter
    output_dir=output_dir
  File "/usr/local/lib/python3.7/site-packages/cookiecutter/generate.py", line 364, in generate_files
    generate_file(project_dir, infile, context, env)
  File "/usr/local/lib/python3.7/site-packages/cookiecutter/generate.py", line 166, in generate_file
    tmpl = env.get_template(infile_fwd_slashes)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python3.7/site-packages/jinja2/loaders.py", line 125, in load
    code = environment.compile(source, name, filename)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 591, in compile
    self.handle_exception(exc_info, source_hint=source_hint)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "./src/components/layout.js", line 32, in template
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 497, in _parse
    return Parser(self, source, name, encode_filename(filename)).parse()
  File "/usr/local/lib/python3.7/site-packages/jinja2/parser.py", line 901, in parse
    result = nodes.Template(self.subparse(), lineno=1)
  File "/usr/local/lib/python3.7/site-packages/jinja2/parser.py", line 876, in subparse
    self.stream.expect('variable_end')
  File "/usr/local/lib/python3.7/site-packages/jinja2/lexer.py", line 384, in expect
    self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got ':'
  File "./src/components/layout.js", line 32
    margin: `0 auto`,

Re: dependencies, how are you installing them? I had a slightly different mount in Stormwater, but I think the important thing is that it matches the work dir, which yours does.

This is what I run: docker-compose run --rm app add ${PACKAGE}

ENTRYPOINT [ "yarn" ]
CMD [ "start" ]

ENTRYPOINT [ "gatsby" ]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Docker only recognizes the last entrypoint in the Dockerfile, so the first entrypoint is not being run. What's your intention with your entry points and commands?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's the gatsby build! Thanks for the catch

@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 gatsbyjs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Copyright (c) 2015 gatsbyjs
Copyright (c) 2020 DataMade


- **To dive straight into code samples, head [to our documentation](https://www.gatsbyjs.org/docs/).** In particular, check out the _Guides_, _API Reference_, and _Advanced Tutorials_ sections in the sidebar.

### 💫 TK: Deploy
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we link to our documentation here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah! Do you mean our deployment documentation specifically?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nevermind I see! Adding.


3. **`.gitignore`**: This file tells git which files it should not track / not maintain a version history for.

4. **`.prettierrc`**: This is a configuration file for [Prettier](https://prettier.io/). Prettier is a tool to help keep the formatting of your code consistent.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we use this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We don't, in favor of ESLint. Good catch again ty!

@beamalsky
Copy link
Copy Markdown
Contributor Author

Thanks @hancush! I've added edits based on your comments. After a little investigative work I believe that the Jinja error you noted above was caused by the double brackets of JSX syntax, eg:

  <header
    style={{
      background: `#e49fb2`,
      marginBottom: `1.45rem`,
    }}
  />

An unfortunate collision, but I've gotten around it by adding a line in cookiecutter.json to avoid rendering the culprit files, as this comment recommends: https://stackoverflow.com/a/39151217

@beamalsky beamalsky requested a review from hancush September 10, 2020 20:27
Copy link
Copy Markdown
Member

@hancush hancush left a comment

Choose a reason for hiding this comment

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

It works! Overall, this looks great to me. I have one outstanding question about style preferences, and then I think this is good to go. Really great idea to start from Gatsby's own template. 🚀

const Header = ({ siteTitle }) => (
<header
style={{
background: `#e49fb2`,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Love this choice. :-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

💅


const Header = ({ siteTitle }) => (
<header
style={{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Style question: We tend to avoid inline styles when writing HTML. What's our stance on inline styling for components?

Copy link
Copy Markdown
Contributor Author

@beamalsky beamalsky Sep 24, 2020

Choose a reason for hiding this comment

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

Great question, and one that I'd like to have a stronger stance on! (I suspect going through the course at https://mastergatsby.com/ would supply it)

This section of the Gatsby tutorial gives a good account of the different CSS approaches: global styles, component-scoped styles, and CSS-in-JS (aka inline styles).

I've used all 3 at different times, and the approach that I've liked most has been to have a global style sheet (which this repo doesn't include, but maybe should—here it is on the courts app) and to handle the component-level styling with CSS-in-JSS. The Gatsby docs also recommend using CSS modules, but I've found it cumbersome to have to create a second CSS file to update alongside every component.

I've also used the styled-components package for CSS-in-JS, and I'd originally included it in this template, but I haven't always actually found it necessary. In courts, David started his components with it and I've ended up writing it out.

This is a good overview of the benefits of CSS-in-JSS: https://medium.com/seek-blog/a-unified-styling-language-d0c208de2660

I'm inclined to add a global style sheet to this repo for now, and plan to come back to this question later and flesh out official guidance on DataMade's React CSS practices if I go through that Mastering Gatsby class. I agree that avoiding inline CSS styles in Django projects is still a best practice. What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's a good plan! I agree it'd be nice to have an example of global styles in the template.

@@ -0,0 +1,16 @@
module.exports = {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice! We should deprecate this guidance after we bring this in.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It'd be nice to bring this testing-guidelines repo into how-to... what do you think about the sequencing of tasks here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We decided to leave the external repos (data-analysis-guidelines, data-making-guidelines, testing-guidelines, deploy-a-site before it was deprecated) separate, to preserve any external links to them. (data-making-guidelines, in particular, has a life of its own.) I think that, after this comes in, we should deprecate the JavaScript testing portion of testing guidelines, since it was never really adopted anyway, but leave everything else over there for now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I can set up a PR with that deletion.

@beamalsky
Copy link
Copy Markdown
Contributor Author

@hancush I've set this up with a global styles sheet! Let me know if this looks good to merge. I'll open a PR now in testing guidelines

@beamalsky beamalsky merged commit 813925f into master Sep 28, 2020
@beamalsky beamalsky deleted the gatsby-cookiecutter branch September 28, 2020 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants