diff --git a/README.md b/README.md index f500718..b507cc5 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Available via the [`json-sort` package][json-sort package], the binary is called ### Via the source code -Clone the repository and run in the sourcecode folder: +Clone the repository and run in the source code folder: ```sh cargo build --release @@ -72,7 +72,7 @@ just serve Open and use the page to sort JSON object keys locally in the browser. -Try the online demo on Github pages, find the link in the badge on top of this +Try the online demo on GitHub Pages; the link is available in the badge at the top of this file. ### Via Nix @@ -83,6 +83,33 @@ You can use the package from this repository with Nix. If you have Nix installed nix run github:drupol/json-sort ``` +### Treefmt module + +This repository also exposes a reusable `treefmtModule` as `treefmtModules.default`, so you can add the +`json-sort` formatter to any `treefmt` configuration without duplicating the setup. + +```nix + treefmt = { + imports = [ + inputs.json-sort.treefmtModules.default + ]; + projectRootFile = "flake.nix"; + programs = { + json-sort.enable = true; + }; + }; +``` + +Make sure to add the `json-sort` flake input: + +```nix + inputs = { + # ... 8< ... + json-sort.url = "github:drupol/json-sort"; + # ... >8 ... + }; +``` + ## Usage ### Command Line diff --git a/nix/modules/fmt.nix b/nix/modules/fmt.nix index 530a1d8..a326519 100644 --- a/nix/modules/fmt.nix +++ b/nix/modules/fmt.nix @@ -1,12 +1,18 @@ { inputs, ... }: { - imports = [ inputs.treefmt-nix.flakeModule ]; + imports = [ + inputs.treefmt-nix.flakeModule + ]; perSystem = { treefmt = { + imports = [ + inputs.self.treefmtModules.default + ]; projectRootFile = "flake.nix"; programs = { jsonfmt.enable = true; + json-sort.enable = true; nixfmt.enable = true; prettier.enable = true; statix.enable = true; diff --git a/nix/modules/treefmt-module.nix b/nix/modules/treefmt-module.nix new file mode 100644 index 0000000..b521b07 --- /dev/null +++ b/nix/modules/treefmt-module.nix @@ -0,0 +1,74 @@ +{ + inputs, + ... +}: +let + treefmtModule = + { + lib, + config, + pkgs, + ... + }: + let + cfg = config.programs.json-sort; + in + { + config = lib.mkIf cfg.enable { + settings.formatter.json-sort = { + inherit (cfg) includes; + command = lib.getExe cfg.package; + options = [ "--fix" ]; + } + // lib.optionalAttrs (cfg.excludes != [ ]) { inherit (cfg) excludes; } + // lib.optionalAttrs (cfg.priority != null) { inherit (cfg) priority; }; + }; + options.programs.json-sort = { + enable = lib.mkEnableOption "json-sort, the JSON formatter"; + package = lib.mkOption { + default = pkgs.json-sort; + defaultText = lib.literalExpression "pedantix.packages.\${system}.pedantix-wrapped"; + description = "The pedantix package to run. The default wrapper ships nixfmt, alejandra and nixpkgs-fmt on PATH; use the unwrapped `pedantix` package if you provide the base formatter yourself."; + type = lib.types.package; + }; + excludes = lib.mkOption { + default = [ ]; + description = "Path / file patterns to exclude."; + example = [ "generated/*.json" ]; + type = lib.types.listOf lib.types.str; + }; + includes = lib.mkOption { + default = [ "*.json" ]; + description = "Path / file patterns to include."; + type = lib.types.listOf lib.types.str; + }; + priority = lib.mkOption { + default = null; + description = "treefmt priority, for ordering relative to other formatters on the same files."; + type = lib.types.nullOr lib.types.int; + }; + }; + }; +in +{ + imports = [ + inputs.flake-parts.flakeModules.flakeModules + ]; + + flake = { + flakeModules = { + default = + { flake-parts-lib, ... }: + { + options.perSystem = flake-parts-lib.mkPerSystemOption { + config.treefmt = { + imports = [ treefmtModule ]; + }; + }; + }; + }; + treefmtModules = { + default = treefmtModule; + }; + }; +} diff --git a/web/index.html b/web/index.html index 5db83f2..6436010 100644 --- a/web/index.html +++ b/web/index.html @@ -22,7 +22,8 @@

json-sort

v...

- Sort JSON object keys in the browser with the Rust library compiled to WebAssembly. + Sort JSON object keys in the browser with the Rust library compiled + to WebAssembly.

@@ -59,7 +60,11 @@

json-sort

/>
-
@@ -75,16 +80,30 @@

json-sort

readonly >
- +
-
+