diff --git a/packages/tools/README.md b/packages/tools/README.md index a26dd2e6af..90811a72c6 100644 --- a/packages/tools/README.md +++ b/packages/tools/README.md @@ -1,4 +1,5 @@ # tools for internal development use - json-edit: A CLI tool to edit JSON files such as package.json in e2e tests +- json-sort: A CLI tool to sort JSON keys in a file - snap-test: run snapshot tests for CLI diff --git a/packages/tools/snap-tests/json-sort/array.json b/packages/tools/snap-tests/json-sort/array.json new file mode 100644 index 0000000000..c7522ca60d --- /dev/null +++ b/packages/tools/snap-tests/json-sort/array.json @@ -0,0 +1,18 @@ +[ + { + "name": "test", + "age": 18 + }, + { + "name": "abc", + "age": 20 + }, + { + "name": "def", + "age": 15 + }, + { + "name": "ghi", + "age": 18 + } +] diff --git a/packages/tools/snap-tests/json-sort/snap.txt b/packages/tools/snap-tests/json-sort/snap.txt new file mode 100644 index 0000000000..e8913e1bb5 --- /dev/null +++ b/packages/tools/snap-tests/json-sort/snap.txt @@ -0,0 +1,59 @@ +> cat array.json # should show original array.json file +[ + { + "name": "test", + "age": 18 + }, + { + "name": "abc", + "age": 20 + }, + { + "name": "def", + "age": 15 + }, + { + "name": "ghi", + "age": 18 + } +] + +> tool json-sort array.json '_.name' && cat array.json # should sort array.json file by name +[ + { + "name": "abc", + "age": 20 + }, + { + "name": "def", + "age": 15 + }, + { + "name": "ghi", + "age": 18 + }, + { + "name": "test", + "age": 18 + } +] + +> tool json-sort array.json '_.age' && cat array.json # should sort array.json file by age +[ + { + "name": "def", + "age": 15 + }, + { + "name": "ghi", + "age": 18 + }, + { + "name": "test", + "age": 18 + }, + { + "name": "abc", + "age": 20 + } +] diff --git a/packages/tools/snap-tests/json-sort/steps.json b/packages/tools/snap-tests/json-sort/steps.json new file mode 100644 index 0000000000..2995823891 --- /dev/null +++ b/packages/tools/snap-tests/json-sort/steps.json @@ -0,0 +1,9 @@ +{ + "env": { + }, + "commands": [ + "cat array.json # should show original array.json file", + "tool json-sort array.json '_.name' && cat array.json # should sort array.json file by name", + "tool json-sort array.json '_.age' && cat array.json # should sort array.json file by age" + ] +} diff --git a/packages/tools/src/index.ts b/packages/tools/src/index.ts index 6b93612d2e..0e6bc138c1 100644 --- a/packages/tools/src/index.ts +++ b/packages/tools/src/index.ts @@ -13,8 +13,12 @@ switch (subcommand) { const { syncRemote } = await import('./sync-remote-deps'); syncRemote(); break; + case 'json-sort': + const { jsonSort } = await import('./json-sort'); + jsonSort(); + break; default: console.error(`Unknown subcommand: ${subcommand}`); - console.error('Available subcommands: snap-test, replace-file-content, sync-remote'); + console.error('Available subcommands: snap-test, replace-file-content, sync-remote, json-sort'); process.exit(1); } diff --git a/packages/tools/src/json-sort.ts b/packages/tools/src/json-sort.ts new file mode 100755 index 0000000000..4e596614fa --- /dev/null +++ b/packages/tools/src/json-sort.ts @@ -0,0 +1,39 @@ +#!/usr/bin/env node + +import assert from 'node:assert'; +import { readFileSync, writeFileSync } from 'node:fs'; +import { parseArgs } from 'node:util'; + +export function jsonSort() { + const { positionals } = parseArgs({ + allowPositionals: true, + args: process.argv.slice(3), + }); + + const filename = positionals[0]; + const script = positionals[1]; + + if (!filename || !script) { + console.error('Usage: tool json-sort