Skip to content

Commit c430515

Browse files
committed
Just: simplify forwarder using --justfile
1 parent fba96c4 commit c430515

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

misc/just/forward-command.ts

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@ import * as child_process from "child_process";
22
import * as path from "path";
33
import * as fs from "fs";
44

5-
function commonDir(paths: string[]): string {
5+
function commonJustfile(paths: string[]): string {
66
if (paths.length === 0) return "";
77
const splitPaths = paths.map((p) => p.split(path.sep));
8-
let i;
9-
for (i = 0; i < splitPaths[0].length; i++) {
8+
let justfile: string | undefined = undefined;
9+
for (let i = 0; i < splitPaths[0].length; i++) {
10+
let candidate = path.join(...splitPaths[0].slice(0, i), "justfile");
11+
if (fs.existsSync(candidate)) {
12+
justfile = candidate;
13+
}
1014
if (!splitPaths.every((parts) => parts[i] === splitPaths[0][i])) {
1115
break;
1216
}
1317
}
14-
const commonParts = splitPaths[0].slice(0, i);
15-
let ret = commonParts.join(path.sep);
16-
if (!fs.existsSync(ret)) {
17-
throw new Error(`Common directory does not exist: ${ret}`);
18-
}
19-
if (!fs.lstatSync(ret).isDirectory()) {
20-
ret = path.dirname(ret);
18+
if (justfile === undefined) {
19+
throw new Error("No common justfile found");
2120
}
22-
return ret;
21+
return justfile;
2322
}
2423

2524
function forwardCommand(args: string[]): number {
@@ -48,40 +47,20 @@ function forwardCommand(args: string[]): number {
4847
return 1;
4948
}
5049

51-
const commonPath = commonDir(positionalArgs);
52-
let relativeArgs = positionalArgs.map(
53-
(arg) => path.relative(commonPath, arg) || ".",
54-
);
55-
if (relativeArgs.length === 1 && relativeArgs[0] === ".") {
56-
relativeArgs = [];
57-
}
58-
let relativeFlags = flags.map((arg) => {
59-
// this might break in specific corner cases, but is good enough for most uses
60-
// workaround if this doesn't work is to not use the forwarder (call just directly in the relevant directory)
61-
if (arg.includes("=") && arg.includes(path.sep)) {
62-
let [flags, flag_arg] = arg.split("=", 2);
63-
flag_arg = flag_arg
64-
.split(path.delimiter)
65-
.map((p) =>
66-
path.isAbsolute(p) ? p : path.relative(commonPath, p),
67-
)
68-
.join(path.delimiter);
69-
return `${flags}=${flag_arg}`;
70-
}
71-
return arg;
72-
});
50+
const justfile = commonJustfile(positionalArgs);
7351

7452
const invocation = [
7553
process.env["JUST_EXECUTABLE"] || "just",
54+
"--justfile",
55+
justfile,
7656
cmd,
77-
...relativeFlags,
78-
...relativeArgs,
57+
...flags,
58+
...positionalArgs,
7959
];
80-
console.log(`-> ${commonPath}: just ${invocation.slice(1).join(" ")}`);
60+
console.log(`-> ${justfile}: ${invocation.slice(1).join(" ")}`);
8161
try {
8262
child_process.execFileSync(invocation[0], invocation.slice(1), {
8363
stdio: "inherit",
84-
cwd: commonPath,
8564
});
8665
} catch (error) {
8766
return 1;

0 commit comments

Comments
 (0)