feat: scripts

This commit is contained in:
2025-04-05 16:57:26 +08:00
parent bd4fe63cdc
commit 3e996ffab3
62 changed files with 4905 additions and 0 deletions

37
scripts/viewjson.js Normal file
View File

@@ -0,0 +1,37 @@
#! /usr/bin/env runjs
var argsEx = require('component-args.js');
var System = java.lang.System;
var FastJSON = Packages.com.alibaba.fastjson.JSON;
var RStream = Packages.me.hatter.tools.commons.io.RStream;
var main = () => {
var args = argsEx.parseDefARGs(['h', 'help', 'n']);
if (args.flg('h', 'help')) {
println('viewjson.js [FLAGS] [FILENAME]');
println(' -h, --help Print help');
println(' -n Do not print new line at last')
return;
}
var json = null;
if (args.length < 1) {
json = RStream.from(System.in).string();
} else {
var j = $$.file(args[0]);
if (!(j.exists())) {
xprintln('[ERROR] File not exists: ' + j);
return;
}
json = $$.rFile(j).string();
}
try {
var formatedJSON = FastJSON.toJSONString($$.parseJSON(json), true).replaceAll('\t', ' ');
if (args.flg('n')) { print(formatedJSON) } else { println(formatedJSON); }
} catch (e) {
xprintln('[ERROR] Parse JSON failed.');
}
};
main();