loreanvictor / tmplr

Automate Code Scaffolding
MIT License
26 stars 0 forks source link

Add support for writing to / reading from specific fields in JSON / YAML files #26

Open loreanvictor opened 1 year ago

loreanvictor commented 1 year ago

write and from file commands can benefit from being able to specific fields in JSON / YAML files, specifically for recipes that want to alter an existing projects. For example, the following could read the name of the package:

read: package_name
from file: package.json
at: /name

Or the following could add dependencies:

write: '^6.0.1'
to: package.json
at: /dependencies/some-package

Write can support at, before and after properties to allow inserting to arrays as well. The field addressing syntax is JSON Pointer. It is important to not screw up files that are written to, maintaining their styling, for which packages such as json-source-map and mapped-yaml can be used (perhaps a separate package should be written for this specific purpose). Additionally, the filesystem object might want to do some caching of files that are read / written to, to save on processing costs (though this might be a later-on feature).

loreanvictor commented 1 year ago

It generally is a nice idea to support more config formats. That said, JavaScript itself is used commonly as a config format and will be enforced on large projects such as eslint. Perhaps tools like recast can be used to handle JS code in a similar manner to JSON or YAML, however JS config files do not have a specific format, and this might result in flaky recipes that only work in particular scenarios. Can this be helped with additional helpers? If so, to which extent? Something to think about.

loreanvictor commented 11 months ago

a note on the flaky recipes of js configs: this will only result in flaky reusable recipes. templates can control the config format, so they wouldn't have issues with such shenanigans.

fwextensions commented 9 months ago

This would be very handy for updating a package.json file with variations on a name string entered by the user. Right now, I think I'll need to maintain a template package.json and keep it in sync with the real one.