Copy json file to functions folder after buidl

Hi,

My app is written in hugo and it is generating a json file with list of products. Products are added as markdown files, and a json file is generated on this basis.
Is there a way to copy this json file to functions folder after hugo generates a site and this json file?

Hi @xlawok

A good question that I wasn’t sure of the answer to, so I tested.

I set up a postbuild script to copy a file to the functions directory and this worked for me.

Using Vue as my example, I had functions in the default netlify/functions directory.

scripts: {
  "build": "vue-cli-service build",
  "postbuild": "cp dist/data.json netlify/functions"
}

So as soon as the build script is finished, postbuild runs. This is done prior to the Functions building starting.

Edit:

Of course, hugo doesn’t use a package.json and build scripts, so you could change the build command from hugo to hugo && cp path/to/file.json netlify/functions.

1 Like

Hi @coelmay

Thanks to you i made it like this in netlify.toml:

[build]
    command = "hugo && cp public/products/index.json functions && cd functions && npm i && cd .."
    publish = "public"
    functions = "functions"
[functions]
    node_bundler = "esbuild"

Can you confirm if this worked for you or not @xlawok

actually not. I forgot I had json thrown in there manually. So when I’ve changed some data in product markdown I saw that in json file in functions folder it is not updated.

Unfortunately it also doesn’t work when I switched it from netlify.toml to package.json

{
  "name": "proatkiv",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "hugo",
    "test": "echo \"Error: no test specified\" && exit 1",
    "postbuild": "cp -f public/products/index.json netlify/functions  && cd functions && npm i && cd .."
  },

Hold the horses it was working back than, but I forgot to add subfolder and that’s why i haven’t seen this json

[build]
    command = "hugo && cp -f public/products/index.json functions/data && cd functions && npm i && cd .."
    publish = "public"
    functions = "functions"
[functions]
    node_bundler = "esbuild"
2 Likes

So this is working for you now, copying the newly build JSON from hugo to the functions directory so you can access it at runtime?

Yeap, now it is working like a charm.
Thank you so much.

1 Like