Netlify CMS - save data to project root (e.g. `__dirname`)

Viewed 154

I'm using Netlify CMS within a Gatsby Theme. Here is an example of my project structure:

root/
  themes/
    gatsby-theme-example/
      pages/
        index.js
      static/
        admin/
          config.yml
      gatsby-config.js
      package.json
  sites/
    example-theme-site/
      data/
        content.md
      gatsby-config.js
      package.json
  package.json

More info:

  • gatsby-theme-example uses gatsby-plugin-netlify-cms, with the config.yml file located at root/themes/gatsby-theme-example/static/admin/config.yml
  • gatsby-theme-example and example-theme-site are connected via yarn workspaces, however I intend to publish gatsby-theme-example to npm once it's fully developed so users can install it as a package. example-theme-site is only used for development testing purposes.

This means that:

  1. Running yarn workspace example-theme-site develop will run gatsby develop for example-theme-site
  2. Users can then visit http://localhost:8000/admin and access Netlify CMS. Once logged in, this will load Netlify CMS using the config file located within root/themes/gatsby-theme-example/static/admin/config.yml

The next step I want to occur after this is:

  • Users add content to Netlify CMS
  • When saved/published, this new data is saved under the root/sites/example-theme-site/data directory.

This is the issue I'm having difficulty with, as the config file is located in gatsby-theme-example however I want the data to save to example-theme-site. I'm not sure how to make the path relative to the root directory without hard-coding it in.

I see that I can specify the location to save the data under file or folder in the config.yml, however since config.yml is a .yml file, I don't see how I can use something like __dirname to specify the correct directory. The only option I can see would be to hard-code in the file path (e.g. file: "../../sites/example-theme-site/data/content.md") while in development, then change it so it's relative to node_modules just before I publish the theme to npm.

Is there any other more appropriate solution here?

1 Answers

The location of the config file in your repo doesn't matter - the config file is processed in the browser. All paths in your config are always relative to the repo root.

Related