richie5um / vscode-sort-json

VSCode Extension to Sort JSON objects
https://marketplace.visualstudio.com/items?itemName=richie5um2.vscode-sort-json
MIT License
107 stars 20 forks source link

choose which json to sort #59

Closed StojanovicMilos closed 2 years ago

StojanovicMilos commented 2 years ago

Hi, love the plugin, it is very useful. I know the title might be a bit confusing, so let me try to clear it up. We have multiple json files in the project. We want to sort some (json files used for translations), we do not want to sort some other ones (for example package.json). If we setup that json is sorted on save, it will sort package.json whenever we change it, but otherwise, we have to manually sort the files (right click -> sort or run the sortJSON while file is open). This is a problem because we have npm scripts that generate unsorted json files used for translations. This means that every time I add a single (or multiple) translations, I have to manually go trough 4 json files (number of languages we currently support) and sort it manually like mentioned before. It would be great if there was support for either of these possibilities:

  1. to be able to have whitelist/blacklist of files that are affected for saving if we enable sortJson on save (for example, on each save sort all json file in the list or sort all json files not in the list)
  2. even better, to be able to run sortJson npm scripts on chosen file(s). Something along the lines of having an npm script "generateTranslations & sortJson path/to/file.json path/to/anotherfile.json"

I did not see any way to solve this problem with what's already supported, please correct me if I'm wrong. If that's the case, is this request feasible? Thanks.

richie5um commented 2 years ago

Hi.

Thanks for the message.

I've not had time to look at the plugin for a long time, so I don't know if it is possible to do 'sort-on-save' for specific files only. I do like the idea of a cmd line that'll expose it as a npm command - I'll have to publish the logic separately to npm, but that isn't too difficult.

I'll see if I can get time to investigate. If you don't hear back from me, please (!) feel free to send me a reminder message - i.e. add a comment to this issue and I'll get the email.

Best, Rich

On Wed, 10 Nov 2021, at 2:39 PM, Milos Stojanovic wrote:

Hi, love the plugin, it is very useful. I know the title might be a bit confusing, so let me try to clear it up. We have multiple json files in the project. We want to sort some (json files used for translations), we do not want to sort some other ones (for example package.json). If we setup that json is sorted on save, it will sort package.json whenever we change it, but otherwise, we have to manually sort the files (right click -> sort or run the sortJSON while file is open). This is a problem because we have npm scripts that generate unsorted json files used for translations. This means that every time I add a single (or multiple) translations, I have to manually go trough 4 json files (number of languages we currently support) and sort it manually like mentioned before. It would be great if there was support for either of these possibilities:

  1. to be able to have whitelist/blacklist of files that are affected for saving if we enable sortJson on save (for example, on each save sort all json file in the list or sort all json files not in the list)
  2. even better, to be able to run sortJson npm scripts on chosen file(s). Something along the lines of having an npm script "generateTranslations & sortJson path/to/file.json path/to/anotherfile.json" I did not see any way to solve this problem with what's already supported, please correct me if I'm wrong. If that's the case, is this request feasible? Thanks.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/richie5um/vscode-sort-json/issues/59, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG5OGMINXK4NKBBK6DHKITULJ72FANCNFSM5HYCACPQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

StojanovicMilos commented 2 years ago

Hi, I hope you are doing well. Is there any possibility of an update on the topic? Thanks.

richie5um commented 2 years ago

Sorry, I've not managed to find any free time yet. Let me know if you are interested in taking a look and even submitting a PR.

StojanovicMilos commented 2 years ago

Okay, a small update. I am able to exclude files such as package.json and package-lock.json with excluded files configuration. I've re-enabled sorting all json files (excepted excluded ones) on save. I'd like to take this one step further though and still have sorting automated by using an npm script.

I would be more than happy to do it, but I'm not sure where to start or what to search for. Could you please give me some directions on where to look? How do I expose a command for sortJSON.sortNormal?

P.S. If anyone is interested, this is how you setup excluded files. Add the following code in settings.json in your vscode: "sortJSON.excludedFiles": [ "package.json", "package-lock.json", ...]

gbrunow commented 2 years ago

@StojanovicMilos I don't think what you want to do is possible with a vscode extension, as far as automating it with npm. You'd need something like json-sort-cli that can run in the terminal and independently from vscode.

StojanovicMilos commented 2 years ago

@StojanovicMilos I don't think what you want to do is possible with a vscode extension, as far as automating it with npm. You'd need something like json-sort-cli that can run in the terminal and independently from vscode.

@gbrunow I will take a look. Thank you!

edit: it works like a charm! Thanks!