Skip to content
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

Add Rename Option to Copy Command #126

Merged
merged 12 commits into from
Aug 12, 2022
Merged

Conversation

biniona-mongodb
Copy link
Contributor

@biniona-mongodb biniona-mongodb commented Aug 11, 2022

Description

Adds a --rename option to the copy command.

Use Case

Hey team, I ran into a use case where I'd like to be able to rename a file as I copy it.

My application has two files:

make_collection.py
insert.py

Both make_collection.py and insert.py have about five states with roughly the same structure. I'd like to add a new state that has the same structure for make_collection.py but a new structure for insert.py. I'd like to put my new code in a file named insert_<new_state>.py, as it is hard to place the logic I need in insert.py through state tags.

I'd like to be able to rename the file as I copy it so my test runner can find it and so that I can source it into docs without changing paths.

Behavior

The --rename option takes a JSON string as an argument. Keys are filenames that are to be renamed, and values are the new names of those files. For example, the following options rename any file name test.txt to test_new.txt

bluehawk copy test.txt -o test_dir --rename '{"test.txt":"test_new.txt"}'

Let me know your thoughts on this option!

@biniona-mongodb biniona-mongodb changed the title Add option to Rename File Add Option to Rename File in Copy Aug 11, 2022
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node"
"type": "node"
Copy link
Contributor Author

@biniona-mongodb biniona-mongodb Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pwa-node is no longer necessary as node debugger is no longer in preview. See this post for details.

"configurations": [
{
"name": "Run Tests",
"request": "launch",
"runtimeArgs": ["run", "test"],
"runtimeExecutable": "npm",
"cwd": "${workspaceFolder}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to add cwd in order to run the debugger with tests (was not referencing correct tests). Let me know if this wasn't necessary.

@biniona-mongodb biniona-mongodb requested review from cbush and removed request for cbush August 11, 2022 23:36
@biniona-mongodb biniona-mongodb changed the title Add Option to Rename File in Copy Add Rename Option to Copy Command Aug 11, 2022
Copy link
Collaborator

@cbush cbush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... I think this is okay for a new feature (with a few comments on the implementation), but it also needs docs, and a bit of clarity on how the matching works - is it only filenames?

So if we have a/test.txt and b/test.txt, and rename:test.txt renamed.txt, that means both a/test.txt and b/test.txt will become renamed.txt?

Would rename:"a/test.txt" actually work? There could be a test for that.

Thanks for the PR!

output: outputPath,
rootPath,
waitForListeners: true,
rename: '{"test.bin":"renamed.bin"}',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expected the JSON to be parsed into an object before passing to the actual action.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point made this change


// function to check if object has a new name specified
const getRename = (fileName: string) => {
if (rename && renameObj && (renameObj as any)[fileName] !== undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watch out for casting to any. This could be avoided if the parsing of the input were done in the interface layer (CLI) rather than action.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated type of rename to a Record<String, String> to avoid any type cast. Good catch!

docs/docs/cli.md Outdated
@@ -38,6 +38,10 @@ ignore patterns that omit matched files from output.
By default, this command generates output files that omit all `state`.
However, you can use the `--state` flag to generate output files that
include content from a single state that you specify.
If you would like to rename files as you copy them, use
the `--rename` flag. The `--rename` flag takes a JSON
string as an argument. Keys are filenames that are to be renamed, and values are the new names of those files.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
string as an argument. Keys are filenames that are to be renamed, and values are the new names of those files.
string as an argument. The JSON must represent an object whose keys are filenames that are to be renamed and values are the new names of those files.

Wondering if we should be explicit about the fact that specific paths would probably be ignored? i.e. this only works on filenames?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated docs with suggestion and added sentence explicitly stating that path keys/values are not supported at this time.

@@ -9,6 +9,7 @@ import {
CopyArgs,
copy,
} from "../..";
import { DOCS } from "../../bluehawk/const";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we do this anywhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed project + docs links as per slack discussion

Copy link
Collaborator

@cbush cbush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good Alek, thanks!

src/cli/commandModules/copy.ts Outdated Show resolved Hide resolved
src/bluehawk/actions/copy.test.ts Outdated Show resolved Hide resolved
src/cli/commandModules/copy.ts Outdated Show resolved Hide resolved
src/bluehawk/actions/copy.ts Outdated Show resolved Hide resolved
@biniona-mongodb biniona-mongodb merged commit d97fa76 into main Aug 12, 2022
@biniona-mongodb biniona-mongodb deleted the add-option-to-rename-file branch August 12, 2022 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants