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

@formatjs/cli Can't use a custom formatter to extract messages to a non-JSON format #4149

Closed
jrnail23 opened this issue Jul 12, 2023 · 16 comments

Comments

@jrnail23
Copy link

jrnail23 commented Jul 12, 2023

Which package?

@formatjs/cli

Describe the bug

The extract script runs stringify on the formatted extracted result, which produces invalid results for non-JSON based formats. In my case, I created a custom formatter to convert the extracted messages to CSV, since none of the JSON-based formats that our TMS (Phrase) accepts as an import/push source supports message descriptions.

As a workaround, I had to write an additional script to transform the output file's content to CSV format.

Expected behavior

I expected the resulting formatted file to contain the exact content produced by my custom formatter.

To address this issue while avoiding a breaking change to existing users, perhaps an optional flag can be introduced for the extract command to indicate that the supplied formatter's output should be written directly as an unformatted string (bypassing the stringify call).

@jrnail23 jrnail23 added the bug label Jul 12, 2023
@jrnail23 jrnail23 changed the title Can't use a custom formatter to extract messages to a non-JSON format @formatjs/cli Can't use a custom formatter to extract messages to a non-JSON format Jul 12, 2023
@jrnail23
Copy link
Author

FWIW, @longlho, if you're open to accepting a PR to address this, I'd be willing to submit one.

@longlho
Copy link
Member

longlho commented Jul 12, 2023

Yeah i'd be open to that

@jrnail23
Copy link
Author

@longlho, do you have any preference for the naming of the CLI flag?

@jrnail23
Copy link
Author

Actually, another way to go here might be that if the formatter result is a string, just return that string directly with no stringify, etc. That's simpler (no CLI flag needed) and seems unlikely to break existing usages. What do you think, @longlho?

@longlho
Copy link
Member

longlho commented Jul 12, 2023

I guess we can infer based on output extension?

@jrnail23
Copy link
Author

that's not a bad idea. Are you thinking anything with *.json would be stringified, and anything else would not?

@longlho
Copy link
Member

longlho commented Jul 12, 2023

I think json should be the default and then *.csv uses the csv formatter

@jrnail23
Copy link
Author

jrnail23 commented Jul 12, 2023

I wasn't looking to add a CSV formatter to the core project -- different usages might need different column names and CSV generation options.
I just wanted to be able to use my own custom CSV formatter.

Also, doing CSV correctly would probably require adding a new dependency, which I'm sure you don't take lightly.

@longlho
Copy link
Member

longlho commented Jul 12, 2023

Hmm I thought we already support custom formatter?

@jrnail23
Copy link
Author

jrnail23 commented Jul 12, 2023

you do, but the point here is that the result of the custom formatter currently gets stringified by the extract function, which prevents it from working with non-JSON output formats (contaminates a valid CSV string).

https://github.com/formatjs/formatjs/blob/main/packages/cli-lib/src/extract.ts#L250

@longlho
Copy link
Member

longlho commented Jul 12, 2023

Kk hmm then maybe I can extend the formatter API to allow custom stringify? Does that work for u?

@jrnail23
Copy link
Author

absolutely!

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Aug 12, 2023
@jrnail23
Copy link
Author

(bumping to keep the stale bot away)

@github-actions github-actions bot removed the Stale label Aug 13, 2023
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@jrnail23
Copy link
Author

@longlho, thanks for addressing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants