Skip to content

Commit

Permalink
fix(hdiff): migrate CLI to TS, update wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 25, 2021
1 parent 5bad3e5 commit 32d77cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
12 changes: 12 additions & 0 deletions packages/hdiff/bin/hdiff
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# https://stackoverflow.com/a/246128/294515
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

/usr/bin/env node "$DIR/../cli.js" "$@"
2 changes: 1 addition & 1 deletion packages/hdiff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"module": "./index.js",
"typings": "./index.d.ts",
"bin": {
"hdiff": "bin/cli.js"
"hdiff": "bin/hdiff"
},
"sideEffects": false,
"repository": {
Expand Down
20 changes: 10 additions & 10 deletions packages/hdiff/bin/cli.js → packages/hdiff/src/cli.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

const fs = require("fs");
const cproc = require("child_process");
const hdiff = require("@thi.ng/hdiff");
// thing:no-export
import { readFileSync } from "fs";
import { execSync } from "child_process";
import { computeDiff } from "./diff.js";
import { generateHtml } from "./html.js";

let src1;
let src2;
Expand All @@ -11,17 +11,17 @@ let headerBody;
const args = process.argv.slice(2);
if (args.length === 3) {
const [path, rev1, rev2] = args;
src1 = cproc.execSync(`git show ${rev1}:${path}`).toString();
src2 = cproc.execSync(`git show ${rev2}:${path}`).toString();
src1 = execSync(`git show ${rev1}:${path}`).toString();
src2 = execSync(`git show ${rev2}:${path}`).toString();
headerBody = ["header", ["h1", path], ["code", `${rev1}${rev2}`]];
} else if (args.length === 2) {
const [rev1, rev2] = args;
src1 = fs.readFileSync(rev1).toString();
src2 = fs.readFileSync(rev2).toString();
src1 = readFileSync(rev1).toString();
src2 = readFileSync(rev2).toString();
headerBody = ["header", ["h1", "File diff"], ["code", `${rev1}${rev2}`]];
} else {
console.log("Usage:\n\thdiff file1 file2\n\thdiff relpath gitrev1 gitrev2");
process.exit(1);
}

console.log(hdiff.generateHtml(hdiff.computeDiff(src1, src2), headerBody));
console.log(generateHtml(computeDiff(src1, src2), headerBody));

0 comments on commit 32d77cf

Please sign in to comment.