Gatsby: Add postinstall hello script to official starters

Created on 23 Feb 2018  Â·  5Comments  Â·  Source: gatsbyjs/gatsby

Saw this on a starter @stefanjudis is building for Contentful and it's a nice pattern we should copy https://github.com/contentful-userland/gatsby-contentful-starter/blob/af2b3dc5284319eba95f0bdec7019c40754c0b53/package.json#L46

Most helpful comment

Oo, that's a nice one! I'll get on it! I think I'll start by shamelessly copying the content in https://github.com/contentful-userland/gatsby-contentful-starter/blob/af2b3dc5284319eba95f0bdec7019c40754c0b53/bin/hello.js#L4-L30 as a starting point. You should see some PRs popping up in each of the starters soon enough! 😅

All 5 comments

Oo, that's a nice one! I'll get on it! I think I'll start by shamelessly copying the content in https://github.com/contentful-userland/gatsby-contentful-starter/blob/af2b3dc5284319eba95f0bdec7019c40754c0b53/bin/hello.js#L4-L30 as a starting point. You should see some PRs popping up in each of the starters soon enough! 😅

@KyleAMathews said on Discord:

actually, thinking about this — we should just make this a little package that then anyone can drop into their starter

I created a postinstall for the default starter:
https://github.com/LeKoArts/gatsby-starter-default/blob/feature/add-postinstall/bin/hello.js

Who would create that package?

@LeKoArts I really like the format you've got there (options, support channels, etc.).

I wonder how hard it would be to make this package extendable in a way that Gatsby things (e.g. CLI commands/options, support channels) are part of the package and always show unless the developer explicitly opts out, and custom messages can be inserted to add details about the starter.

I can see this being useful for teams as well: if I was rolling out a Gatsby site to a large company, being able to show every developer helpful links and instructions after they set up the repo would be _huge_ as a way of smoothing the onboarding process.

This could also be set up as a CLI command (e.g. gatsby postinstall), and extended with gatsby-config.js:

module.exports = {
  // ...
  postinstall: {
    showDefaults: true,
    message: [
      'This is a custom message',
      '',
      'Each string in the array will be joined by a newline.',
    ],
  },
};

or

const pkg = require('./package.json');

const customMessage = `
This is a custom message for ${pkg.name}.

You can put whatever you want in here. 🎉
`;

module.exports = {
  // ...
  postinstall: {
    showDefaults: true,
    message: customMessage,
  },
};

The command could work sort of like this:

const EOL = require('os').EOL;

const GATSBY_POSTINSTALL_MESSAGE = `
This would be Gatsby info.
`;

function showPostInstall(showDefaults = true, custom = false) {
  const msg = [
    showDefaults && GATSBY_POSTINSTALL_MESSAGE,
    custom,
  ];

  return msg.filter(Boolean).join(EOL);
}

That way, we could set up a script in package.json that always runs the same, and starters and/or teams can modify it as necessary using only config:

{
  "scripts": {
    "postinstall": "gatsby postinstall"
  }
}

In theory, this could also be opened up to plugins, but that could get noisy in a hurry. Anyone have strong opinions here?

Does this seem like a reasonable/manageable approach?

@jlengstorf that sounds perfect! It should probably be a standalone package though to avoid adding single-purpose code to gatsby-cli. E.g. a new package named gatsby-postinstall.

And yeah! Hadn't thought about the internal use case but this would be extra perfect for that. It's a really nice pattern we should adopt and encourage.

This is part of a broader conversation re: CLI improvements (more info forthcoming!).

As such--it's a bit of a misfit and not the way we want to solve this _correctly_ going forward, so let's close this out and we'll encapsulate the work with other, broader CLI improvements.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timbrandin picture timbrandin  Â·  3Comments

andykais picture andykais  Â·  3Comments

hobochild picture hobochild  Â·  3Comments

totsteps picture totsteps  Â·  3Comments

brandonmp picture brandonmp  Â·  3Comments