plentico / plenti

Static Site Generator with Go backend and Svelte frontend
https://plenti.co
Apache License 2.0
1.01k stars 49 forks source link

Error when using CMS editor with GitLab Pages in a monorepo #289

Open alexnitta opened 1 year ago

alexnitta commented 1 year ago

My project is set up as a monorepo, which means that my JSON content lives in a non-standard folder. For example, my _index.json file is in packages/web/src/content/_index.json, rather than content/_index.json. This seems to break the publishing capability of the CMS editor.

When I make some changes in the CMS editor and click "Save", I see an "Error Saving" message in the button. I opened the network tab in the dev tools and found the request body that gets sent to https://gitlab.com/api/v4/projects/..., which looks like this:

{
    "branch": "main",
    "commit_message": "Update content/_index.json",
    "actions": [
        {
            "action": "update",
            "file_path": "content/_index.json",
            "encoding": "text",
            "content": "(lots of content here)"
        }
    ]
}

and the 400 response body is:

{"message":"A file with this name doesn't exist"}

This makes sense, because my repo doesn't have a file at content/_index.json; it's actually at packages/web/src/content/_index.json. It seems that the file_path for each element of the actions array should be modified if the content folder is in a non-standard location. This should be possible by locating the plenti.json file and using its location as the starting directory for the file_path in each action, or possibly by adding some kind of config option in plenti.json to help with non-standard (i.e. nested) locations for the content folder that contains the JSON files.

jimafisk commented 1 year ago

Good idea @alexnitta, I had not thought about monorepo-style projects. Is your plenti.json at the project root or packages/web/src/plenti.json?

I think we could probably just add a file_prefix option to the cms settings defined in plenti.json. We're planning on modifying that API a bit anyways now that we're adding other providers like Gitea.

alexnitta commented 1 year ago

My plenti.json file is at packages/web/src/plenti.json - so it's in the usual location relative to the content folder.

I think the extra config option sounds great. Do you have any idea of when that feature might make it into a release? I'm on the verge of finishing a project for a client, and this is the last piece of the CMS that is not working.

Thanks for building such a great framework for nonprofits, by the way. I'm really excited to deliver this project to the client - which happens to be my son's preschool, a nonprofit organization - and Plenti is a perfect match for their needs.

alexnitta commented 1 year ago

I was able to work around this by adding a shell script to my .gitlab-ci.yml. The shell script uses sed to append the packages/web/src/ subdirectory before the file path in pubic/spa/core/cms/publish.js. Now I am able to use the CMS editor to save changes - so @jimafisk there's no need to rush the official fix into a release, but whenever you have time for that I'd be happy to remove this patch from my codebase.

To restate the problem, my issue is that my JSON content is not located in ./content relative to the root of the repo; since I'm using a monorepo, it's located in ./packages/web/src/content instead. This breaks the current version of the CMS editor when trying to save changes, because the generated JS file in public/spa/core/cms/publish.js assumes the file paths are relative to the root of the repository.

For anyone else facing this same problem, here's my .gitlab-ci.yml with some comments added where I changed things.

image: docker:stable

pages:
    before_script:
        - apk add --update curl wget && rm -rf /var/cache/apk/*
    script:
        # Move to the subdirectory of the monorepo where the plenti.json file is. 
        # This is also where the `content` folder is located which contains all of the JSON content.
        - cd packages/web/src
        # These next three steps are the same as in the example at https://plenti.co/docs/gitlab-piplines/
        - wget -c $(curl -s https://api.github.com/repos/plentico/plenti/releases/latest | grep -o 'http.*linux_64-bit.tar.gz')
        - tar -zxvf *_linux_64-bit.tar.gz
        - ./plenti build
        # Move back up one folder to where the patch script is located
        - cd ..
        # Run the patch script
        - ./patch_cms_publish.sh
    artifacts:
        paths:
            - packages/web/public
    publish: packages/web/public
    only:
        - main

And here is the packages/web/patch_cms_publish.sh script:

#!/bin/sh

# This script is used to patch the public/spa/core/cms/publish.js script to use the correct file path
# when committing changes with the GitLab API. This is necessary because the script expects the
# JSON `content` folder path to start from the root of the repo, but our JSON `content` folder is in
# a subdirectory of the monorepo.

sed -i.bak 's/file_path:/&"packages\/web\/src\/" + /' public/spa/core/cms/publish.js

The script needs to be committed with git add --chmod=+x packages/web/patch_cms_publish.sh so that it can be executed.

jimafisk commented 1 year ago

I'm really excited to deliver this project to the client - which happens to be my son's preschool, a nonprofit organization

That's wonderful, so glad it's fitting your needs! Providing a low-overhead solution for nonprofits is one of our primary goals.

I was able to work around this by adding a shell script

That's very clever, thanks for sharing your workaround!

there's no need to rush the official fix into a release

Sounds great, thanks for letting me know. I'm a little overbooked this month and next, but this is something I'm interested in adding to the project. Thanks for the suggestion!

alexnitta commented 1 year ago

My pleasure, glad that I could be helpful in some way! As I mentioned, this is a really great fit for my client's needs, and I totally appreciate your contributions to the ecosystem. Plenti fills a niche that I haven't seen addressed by any other framework.