loreanvictor / tmplr

Automate Code Scaffolding
MIT License
26 stars 0 forks source link

Add a `write` command #23

Closed loreanvictor closed 1 year ago

loreanvictor commented 1 year ago

The command would simply write to a file. This can be useful for creating files without source templates, however the main idea is to allow the command to write to specific parts of a file, if they are for example json or yaml files.

# create a file
write: some stuff
to: myfile.txt
# add a dependency
write: '^6.1.0'
to: package.json
at: 'devDependencies/@typescript-eslint~1eslint-plugin'
# add a workflow step
write: |
      - name: check code style
        run: npm run lint
to: .github/workflows/publish.yml
after: jobs/run/steps/4

JSON Pointer format is used here. I suspect json-source-map and mapped-yaml (or equivalent libraries) can be used to precisely alter some JSON or YAML file while maintaining its formatting as much as possible.

loreanvictor commented 1 year ago

This command could also benefit from the proposed filtering of files if it were to support glob patterns, specifically to allow the content to also be evaluated per file. This could basically remove the need for the proposed append and prepend commands.

write: '{{ license }}\n{{ content }}'
to: **/*.(jsx?|tsx?|cpp|java)
where:
  steps:
    - read: content
      from file: __file__
  eval: '{{ content | not matches: /.../ }}'
loreanvictor commented 1 year ago

base command added via 8ce8644. additional proposals (filtering and JSON / YAML support) should be tracked in separate issues (as they also involve multiple commands).