Skip to content

Commit

Permalink
feat: support json5format
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Oct 27, 2024
1 parent 4b59f4d commit 40ea83e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [Unreleased](https://github.com/hougesen/mdsf/compare/v0.2.7...HEAD)

- feat: support json5format [`#522`](https://github.com/hougesen/mdsf/pull/522)
- feat: support kdoc-formatter [`#521`](https://github.com/hougesen/mdsf/pull/521)
- feat: support djade [`#520`](https://github.com/hougesen/mdsf/pull/520)
- feat: support tsqllint [`#519`](https://github.com/hougesen/mdsf/pull/519)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ mdsf init
<!-- START_SECTION:supported-tools -->

`mdsf` currently supports 212 commands. Feel free to open an issue/pull-request if your favorite tool is missing! 😃
`mdsf` currently supports 213 commands. Feel free to open an issue/pull-request if your favorite tool is missing! 😃

| Name | Command |
| ------------------------ | -------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -280,6 +280,7 @@ mdsf init
| `isort` | `isort --quiet PATH` |
| `joker` | `joker --format --write PATH` |
| `js-beautify` | `js-beautify -r --type js -f PATH` |
| `json5format` | `json5format -r PATH` |
| `jsona:format` | `jsona format PATH` |
| `jsona:lint` | `jsona lint PATH` |
| `jsonlint` | `jsonlint -i PATH` |
Expand Down
35 changes: 35 additions & 0 deletions mdsf/src/tools/json_5_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::process::Command;

use crate::{error::MdsfError, execution::execute_command, runners::CommandType};

#[inline]
fn set_json_5_format_args(mut cmd: Command, file_path: &std::path::Path) -> Command {
cmd.arg("-r");
cmd.arg(file_path);
cmd
}

#[inline]
pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfError> {
let commands = [CommandType::Direct("json5format")];

for (index, cmd) in commands.iter().enumerate() {
let cmd = set_json_5_format_args(cmd.build(), file_path);
let execution_result = execute_command(cmd, file_path);

if index == commands.len() - 1 {
return execution_result;
}

if let Ok(r) = execution_result {
if !r.0 {
return Ok(r);
}
}
}

Ok((true, None))
}

#[cfg(test)]
mod test_json_5_format {}
7 changes: 7 additions & 0 deletions mdsf/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub mod imba_fmt;
pub mod isort;
pub mod joker;
pub mod js_beautify;
pub mod json_5_format;
pub mod jsona_format;
pub mod jsona_lint;
pub mod jsonlint;
Expand Down Expand Up @@ -583,6 +584,10 @@ pub enum Tooling {
/// `js-beautify -r --type js -f $PATH`
JsBeautify,

#[serde(rename = "json5format")]
/// `json5format -r $PATH`
Json5Format,

#[serde(rename = "jsona:format")]
/// `jsona format $PATH`
JsonaFormat,
Expand Down Expand Up @@ -1164,6 +1169,7 @@ impl Tooling {
Self::Isort => isort::run(snippet_path),
Self::Joker => joker::run(snippet_path),
Self::JsBeautify => js_beautify::run(snippet_path),
Self::Json5Format => json_5_format::run(snippet_path),
Self::JsonaFormat => jsona_format::run(snippet_path),
Self::JsonaLint => jsona_lint::run(snippet_path),
Self::Jsonlint => jsonlint::run(snippet_path),
Expand Down Expand Up @@ -1385,6 +1391,7 @@ impl AsRef<str> for Tooling {
Self::Isort => "isort",
Self::Joker => "joker",
Self::JsBeautify => "js_beautify",
Self::Json5Format => "json_5_format",
Self::JsonaFormat => "jsona_format",
Self::JsonaLint => "jsona_lint",
Self::Jsonlint => "jsonlint",
Expand Down
5 changes: 5 additions & 0 deletions schemas/v0.2.7/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,11 @@
"type": "string",
"enum": ["js-beautify"]
},
{
"description": "`json5format -r $PATH`",
"type": "string",
"enum": ["json5format"]
},
{
"description": "`jsona format $PATH`",
"type": "string",
Expand Down
15 changes: 15 additions & 0 deletions tools/json5format/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "../tool.schema.json",
"binary": "json5format",
"categories": ["formatter"],
"commands": {
"": ["-r", "$PATH"]
},
"description": "JSON5 (a.k.a., \"JSON for Humans\") formatter that preserves contextual comments",
"homepage": "https://github.com/google/json5format",
"languages": ["json", "json5"],
"name": null,
"npm": null,
"php": null,
"tests": []
}

0 comments on commit 40ea83e

Please sign in to comment.