Search code examples
jsonnet

Build multiple jsonnet files at once


The current jsonnet compiler only allows compile one file at a time. Is there any way to build many jsonnet files in one command, maybe with a rule like all jsonnet files matching pattern *.jsonnet in folder recursively?


Solution

  • To answer your question: no, the jsonnet CLI only accepts a single jsonnet file, note that this is by design, to be able to output (i.e. manifest) a valid JSON stream. Recall that JSON itself doesn't have a multi-doc "feature" like YAML has (i.e. ---\n separated chunks).

    That said, depending on your use case, you may be able to use the -m <dir> CLI feature, which creates a file per (main) object key, for example:

    $ cat multi.jsonnet
    {
      'out1.json': { foo: 'bar' },
      'out2.json': { qqq: 'baz' },
    }
    
    $ jsonnet multi.jsonnet
    {
       "out1.json": {
          "foo": "bar"
       },
       "out2.json": {
          "qqq": "baz"
       }
    }
    
    $ jsonnet -m . multi.jsonnet
    ./out1.json
    ./out2.json
    

    Then, it would be matter of setting these "main" object keys to the per-file manifested content you'd need.