egghead-next icon indicating copy to clipboard operation
egghead-next copied to clipboard

feat: add Sanity Studio environment switcher

Open jbranchaud opened this issue 2 years ago • 3 comments

The way that we separate both data and schema between staging and production Sanity is to use separate projects. This is a solution that we sort of hacked together because there isn't an official way to get this kind of separation. Because of that, it is tedious to swap out the staging and production config values when wanting to switch between those two projects. This script introduces an ERB template that makes it is to toggle between the environments by running a single command.

The way it works is, based on the environment you provide (either staging or production), it will generate and overwrite the sanity.json file using the ERB template and the environment-specific values.

This is a spike. Ideally we'd find a way to do this in JS/TS. I'm not sure what the ERB equivalent is in the Node ecosystem.

This is something I had written earlier in the week before the internal discussion on preferring Node tooling for this kinds of scripts. I'd love to discuss how something equivalent to this could be implemented with TS and some templating library in that ecosystem.

template

jbranchaud avatar Jul 21 '22 15:07 jbranchaud

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
egghead-io-nextjs ✅ Ready (Inspect) Visit Preview Aug 16, 2022 at 9:36PM (UTC)
egghead-next-storybook ✅ Ready (Inspect) Visit Preview Aug 16, 2022 at 9:36PM (UTC)

vercel[bot] avatar Jul 21 '22 15:07 vercel[bot]

Try rewriting this using handlebars.js: https://handlebarsjs.com/

Tutorial: https://vahid.blog/post/2021-03-19-how-to-use-handlebars.js-for-templating/

  • Read in the template file
  • Compile the template into a function let templateFunc = Handlebars.compile(template);
  • Generate env-specific file using template function let templateObject = templateFunc(envSpecificData);
  • Write out the result to sanity.json

More reference material here: https://github.com/skillrecordings/products/pull/366/files

jbranchaud avatar Jul 21 '22 16:07 jbranchaud

I haven't read this article yet but it seems relevant to us https://www.sanity.io/guides/multi-environment-deployments

Seems to address our concerns regarding testing bulk migrations, CI, changing data structure, and changing the studio experience.

Creeland avatar Aug 09 '22 21:08 Creeland

Latest update to this PR: the script now uses Node and handlebars instead of Ruby and ERB.

jbranchaud avatar Aug 16 '22 22:08 jbranchaud