add hjq scroll
This commit is contained in:
parent
701e5d138a
commit
d92bc3b437
4 changed files with 54 additions and 20 deletions
44
shelf/hjq/main.ts
Normal file
44
shelf/hjq/main.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
// a basic drop-in replacement for jq to handle HJSON
|
||||
// tries to preserve comments but doesn't do a very good job :(
|
||||
|
||||
import Hjson from "npm:hjson";
|
||||
import jqModule from "npm:jq-web";
|
||||
import fs from "node:fs";
|
||||
|
||||
const jq = await jqModule;
|
||||
|
||||
var filter = process.argv[2];
|
||||
if (!filter) {
|
||||
filter = '.';
|
||||
}
|
||||
|
||||
const input = fs.readFileSync(0, 'utf8');
|
||||
|
||||
try {
|
||||
const data = Hjson.parse(input, { keepWsc: true });
|
||||
|
||||
const result = jq.json(data, filter);
|
||||
|
||||
// Only try to preserve comments if the result is an object or array
|
||||
if (result !== null && typeof result === 'object') {
|
||||
// Extract comments before transformation
|
||||
const comments = Hjson.comments.extract(data);
|
||||
|
||||
// Merge comments back into the transformed result
|
||||
if (comments) {
|
||||
Hjson.comments.merge(comments, result);
|
||||
}
|
||||
|
||||
const output = Hjson.stringify(result, {
|
||||
keepWsc: true,
|
||||
bracesSameLine: true
|
||||
});
|
||||
|
||||
console.log(output);
|
||||
} else {
|
||||
console.log(JSON.stringify(result));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
40
shelf/hjq/package.nix
Normal file
40
shelf/hjq/package.nix
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
{ pkgs, target }:
|
||||
pkgs.stdenv.mkDerivation rec {
|
||||
pname = "hjq";
|
||||
version = "0.1.0";
|
||||
src = ./.;
|
||||
|
||||
__noChroot = true;
|
||||
|
||||
dontStrip = true;
|
||||
|
||||
denort = pkgs.fetchurl {
|
||||
url = "https://dl.deno.land/release/v${pkgs.deno.version}/denort-${target}.zip";
|
||||
hash = "sha256-J2LfvHPbJvcOpbQOd6EmGxHDciez7tG10etK4bqQhLI=";
|
||||
};
|
||||
|
||||
nativeBuildInputs = with pkgs; [ deno unzip ];
|
||||
|
||||
configurePhase = ''
|
||||
export DENO_DIR=./.deno_cache;
|
||||
mkdir -p $TMPDIR/denort
|
||||
pushd $TMPDIR/denort
|
||||
unzip ${denort}
|
||||
chmod +x denort
|
||||
popd
|
||||
'';
|
||||
|
||||
buildPhase = ''
|
||||
runHook preBuild
|
||||
|
||||
export DENORT_BIN="$TMPDIR/denort/denort"
|
||||
|
||||
deno compile --target=${target} --output app main.ts
|
||||
runHook postBuild
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
cp app $out/bin/${pname}
|
||||
'';
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue