Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/tools/README.md
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions packages/tools/snap-tests/json-sort/array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"name": "test",
"age": 18
},
{
"name": "abc",
"age": 20
},
{
"name": "def",
"age": 15
},
{
"name": "ghi",
"age": 18
}
]
59 changes: 59 additions & 0 deletions packages/tools/snap-tests/json-sort/snap.txt
Original file line number Diff line number Diff line change
@@ -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
}
]
9 changes: 9 additions & 0 deletions packages/tools/snap-tests/json-sort/steps.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
6 changes: 5 additions & 1 deletion packages/tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
39 changes: 39 additions & 0 deletions packages/tools/src/json-sort.ts
Original file line number Diff line number Diff line change
@@ -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 <filename> <script>');
console.error("Example: tool json-sort array.json '_.name'");
process.exit(1);
}

const data = JSON.parse(readFileSync(filename, 'utf-8'));
assert(Array.isArray(data), 'json data must be an array');
// sort json by script
const func = new Function('_', `return ${script};`);
const sortedJson = data.sort((a: any, b: any) => {
const aValue = func(a);
const bValue = func(b);
if (aValue < bValue) {
return -1;
}
if (aValue > bValue) {
return 1;
}
return 0;
});

writeFileSync(filename, JSON.stringify(sortedJson, null, 2) + '\n', 'utf-8');
}
Loading