-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jk transform #257
jk transform #257
Conversation
673978f
to
1788f45
Compare
A fun issue that has come up:
|
For more context, the
That allows to both: bootstrap a project from scratch and get a full fledged node module ready to commit and regenerate the build files without touching certain files that may have been modified by the user. The above seems like a nice enough use case to keep /me thinks. |
This relates somewhat to not writing files if they are identical to what already exists on the fs: #189 |
No argument here; I was hoping to keep all use cases supported as they are now. I think your use indicates a slight preference for controlling the overwrite behaviour entirely from the JavaScript -- what do you reckon? |
Yes, I think so too, because it has to be decided by the script author on a per-file basis (in the case of |
I have realised that it is odd to have |
Re |
@dlespiau I also think that latter (raise an error rather than overwriting a file) would be a good default for |
This adds a no-op `transform` command, and factors out some commonality with the run command.
This gives `jk transform` an implementation which corresponds to jk transform --stdout -c <expr> <file>... It's the simplest thing to get working, because it's the most like previous commands.
This implements the `-m` (resolve as a module) flag, and the no flag (resolve as a filesystem path) case. This is similar to `-c` (literal JavaScript), except that the supplied argument is used as an import, rather than the body of a function. I decided it would be easier to use two different templates -- internal/transform-module.js and internal/transform-exec.js -- rather than one. But this approach does mean the core bit needs to be in a public module (transform.js). Though I'd factored out the code for finding out an appropriate base path for resolving things (establishScriptDir), it turns out in this case the appropriate base path is the current directory. As with `jk generate`, the path or module is imported from a _temporary module_, and unless its own path is the current directory, the resolution won't work.
This makes `jk transform` transparently apply its transformation to all the objects in a YAML or JSON multidoc. I realised that once the inputs are transformed, we are essentially in the same situation as `jk generate` -- we have a bunch of objects that need to be output, and hints as to their format. For that reason, I've factored that out of the JavaScript side of generate, so it can be called from transform.
- pass `overwrite` flag through to generate.js (NB this changes `jk generate`, by changing the default of overwrite from true to false) - add a test for writing to files
Previously the choice for a write that would overwrite a file was to skip it, or overwrite the file. For `jk transform` (and possibly other things), the ideal is to throw an error unless it's been explicitly allowed. This commit makes the overwrite field an enum, and changes the runtime protocol to return an error if that overwrite option is used. A boolean is also accepted in the JS API, for backward compatibility.
If the input files (and therefore the inferred output files) are different formats, then it's not possible to output them as a stream to stdout. Since `jk transform` delegates the output bit to generate, it carries over this prohibition; this adds a test to make sure it remains so.
Rather than having --stdout=true be the default, meaning you need to supply --stdout=false if you want to write to files, it is more consistent with `jk generate` to assume file output. To keep it safe and clean, the default is _also_ to raise an error rather than overwriting.
e9913f6
to
06f6819
Compare
Wrinkle: |
The flag --input-directory has no meaning for `jk transform`, because its arguments explicitly name the files to run through the transform. This commit splits the global flag initialisation into two bits, so that the transform command can omit that particular flag.
oooh, exciting, can't wait to play with it! |
Addresses #253.
-c, --exec
)-m, --module
)[ ] accept input from stdin (but in which format?)--overwrite
is not set