forked from pkgxdev/pkgx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add search path inspection * stupid ubuntu * ok. bin and usr/bin are symlinks, at least in ubuntu:latest. * is nothing sacred? * move PathUtils.ts to its own library * markdownlint
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Path from "path" | ||
|
||
export default { | ||
envPath, | ||
addPath, | ||
rmPath, | ||
findBinary, | ||
} | ||
|
||
function envPath(pathVar: string | undefined = undefined): Path[] { | ||
const pathVar_ = pathVar || Deno.env.get("PATH")! | ||
return pathVar_.split(":").map(x => new Path(x)) | ||
} | ||
|
||
function addPath(path: Path | string, pathVar: string | undefined = undefined): Path[] { | ||
const pathVar_ = envPath(pathVar) | ||
const path_ = new Path(path) | ||
if (!pathVar_.includes(path_)) { | ||
pathVar_.push(path_) | ||
Deno.env.set("PATH", pathVar_.join(":")) | ||
} | ||
return pathVar_ | ||
} | ||
|
||
function rmPath(path: Path | string, pathVar: string | undefined = undefined): Path[] { | ||
const path_ = new Path(path) | ||
const pathVar_ = envPath(pathVar).filter(x => x.string != path_.string) | ||
Deno.env.set("PATH", pathVar_.join(":")) | ||
return pathVar_ | ||
} | ||
|
||
function findBinary(name: string, pathVar: string | Path[] | undefined = undefined): Path | undefined { | ||
let pathVar_: Path[] | ||
if (pathVar instanceof Array) { | ||
pathVar_ = pathVar | ||
} else { | ||
pathVar_ = envPath(pathVar) | ||
} | ||
return pathVar_.find(x => x.join(name).isExecutableFile())?.join(name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Path from "path" | ||
import PathUtils from "path-utils" | ||
import { assertArrayIncludes, assertEquals } from "deno/testing/asserts.ts" | ||
|
||
Deno.test("test PathUtils", async test => { | ||
await test.step("modify $PATH", () => { | ||
const envPath = PathUtils.envPath() | ||
|
||
assertArrayIncludes(envPath, [Path.root.join("usr/bin")]) | ||
|
||
const a = Path.home().join("tmp/bin") | ||
const b = PathUtils.addPath(a) | ||
assertArrayIncludes(b, [a]) | ||
|
||
const c = PathUtils.envPath() | ||
assertArrayIncludes(c, [a]) | ||
|
||
const d = PathUtils.rmPath(a) | ||
assertEquals(d, envPath) | ||
|
||
const e = PathUtils.envPath() | ||
assertEquals(e, envPath) | ||
}) | ||
|
||
await test.step("search $PATH", () => { | ||
const usrBin = Path.root.join("usr/bin") | ||
const bin = Path.root.join("bin") | ||
const sbin = Path.root.join("sbin") | ||
const envPath = PathUtils.envPath() | ||
|
||
assertArrayIncludes(envPath, [usrBin]) | ||
|
||
const a = PathUtils.findBinary("env", envPath) | ||
assertEquals(a, usrBin.join("env")) | ||
|
||
const b = PathUtils.findBinary("env") | ||
assertEquals(b, usrBin.join("env")) | ||
|
||
const c = PathUtils.findBinary("bloogargle", envPath) | ||
assertEquals(c, undefined) | ||
|
||
const d = PathUtils.findBinary("ls", [bin]) | ||
assertEquals(d, bin.join("ls")) | ||
|
||
const e = PathUtils.findBinary("ls", [sbin]) | ||
assertEquals(e, undefined) | ||
}) | ||
}) |