Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"imports": {
"is-what": "https://deno.land/x/is_what@v4.1.15/src/index.ts",
"deno/": "https://deno.land/std@0.196.0/",
"deno/": "https://deno.land/std@0.204.0/",
"outdent": "https://deno.land/x/outdent@v0.8.0/mod.ts"
}
}
15 changes: 8 additions & 7 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import * as outdent from "https://deno.land/x/outdent@v0.8.0/mod.ts"
export { outdent }

// importing super specifically to reduce final npm bundle size
import * as crypto from "https://deno.land/std@0.196.0/crypto/mod.ts"
import { moveSync } from "https://deno.land/std@0.196.0/fs/move.ts"
import { readLines } from "https://deno.land/std@0.196.0/io/read_lines.ts"
import { writeAll } from "https://deno.land/std@0.196.0/streams/write_all.ts"
import { parse as parseYaml } from "https://deno.land/std@0.196.0/yaml/parse.ts"
import { SEP } from "https://deno.land/std@0.196.0/path/mod.ts"
import * as crypto from "https://deno.land/std@0.204.0/crypto/mod.ts"
import { moveSync } from "https://deno.land/std@0.204.0/fs/move.ts"
import { readLines } from "https://deno.land/std@0.204.0/io/read_lines.ts"
import { writeAll } from "https://deno.land/std@0.204.0/streams/write_all.ts"
import { parse as parseYaml } from "https://deno.land/std@0.204.0/yaml/parse.ts"
import { SEP } from "https://deno.land/std@0.204.0/path/mod.ts"
import { fromFileUrl } from "https://deno.land/std@0.204.0/path/from_file_url.ts"

const streams = { writeAll }
const io = { readLines }
const fs = { moveSync }
const deno = { readLines, crypto, fs, io, streams, parseYaml, SEP }
const deno = { readLines, crypto, fs, io, streams, parseYaml, SEP, fromFileUrl }

export { deno }
4 changes: 2 additions & 2 deletions src/hooks/useConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { flatmap } from "../utils/misc.ts"
import { deno } from "../deps.ts"
import host from "../utils/host.ts"
import Path from "../utils/Path.ts"

Expand Down Expand Up @@ -64,8 +65,7 @@ export function ConfigDefault(env = Deno.env.toObject()): Config {

function getv(): string | undefined {
if (typeof Deno === 'undefined') {
const url = new URL(import.meta.url)
const path = new Path(url.pathname).parent().parent().parent().join("package.json")
const path = new Path(deno.fromFileUrl(import.meta.url)).parent().parent().parent().join("package.json")
const blob = Deno.readFileSync(path.string)
const txt = new TextDecoder().decode(blob)
const { version } = JSON.parse(txt)
Expand Down
7 changes: 2 additions & 5 deletions src/hooks/useTestConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import useConfig, { ConfigDefault } from "./useConfig.ts"
import { fromFileUrl } from "deno/path/from_file_url.ts"
import Path from "../utils/Path.ts"

export function useBaseTestConfig(env?: Record<string, string>) {
Expand Down Expand Up @@ -31,11 +32,7 @@ export const srcroot = (() => {
if (Path.cwd().parent().parent().join("fixtures").isDirectory()) {
return Path.cwd().parent().parent()
} else {
let path = new URL(import.meta.url).pathname
if (Deno.build.os == 'windows') {
path = path.slice(1) // /D:/foo/bar -> D:/foo/bar
}
return new Path(path).parent().parent().parent()
return new Path(fromFileUrl(import.meta.url)).parent().parent().parent()
}
})()

Expand Down
9 changes: 9 additions & 0 deletions src/utils/Path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,22 @@ Deno.test({
assertEquals(p.string, "Y:\\")
assertEquals(p.parent().string, "Y:\\")
assertEquals(p.parent().parent().parent().string, "Y:\\")

const q = new Path("\\\\bar\\foo\\baz")

assertEquals(q.string, "\\\\bar\\foo\\baz")
assertEquals(q.parent().string, "\\\\bar\\foo")
assertEquals(q.parent().parent().parent().string, "\\\\bar\\foo") // the first path after the hostname is actually a root
}
})

Deno.test("join roots", () => {
if (Deno.build.os == "windows") {
assertEquals(new Path("C:\\foo").join("D:\\bar").string, "D:\\bar")
assertEquals(new Path("C:").join("D:\\bar\baz").string, "D:\\bar\baz")

assertEquals(new Path("c:\\foo\bar").join("\\\\bar\\baz").string, "\\\\bar\\baz")

} else {
assertEquals(new Path("/foo").join("/bar").string, "/bar")
}
Expand Down
20 changes: 12 additions & 8 deletions src/utils/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ export default class Path {
if (!input.startsWith("/") && !input.startsWith("\\")) {
throw new Error(`invalid absolute path: ${input}`)
}
//TODO shouldn’t be C: necessarily
// should it be based on PWD or system default drive?
// NOTE also: maybe we shouldn't do this anyway?
input = `C:\\${input}`
if (!input.startsWith('\\\\')) {
// ^^ \\network\drive is valid path notation on windows

//TODO shouldn’t be C: necessarily
// should it be based on PWD or system default drive?
// NOTE also: maybe we shouldn't do this anyway?
input = `C:\\${input}`
}
}
input = input.replace(/\//g, '\\')
} else if (input[0] != '/') {
Expand All @@ -72,10 +76,10 @@ export default class Path {
this.string = normalize(input)

function normalize(path: string): string {
const segments = path.split(SEP);
const result = [];
const segments = path.split(SEP)
const result = []

const start = Deno.build.os == 'windows' ? (segments.shift() ?? 'C:') + '\\' : '/'
const start = Deno.build.os == 'windows' ? (segments.shift() || '\\') + '\\' : '/'

for (const segment of segments) {
if (segment === '..') {
Expand Down Expand Up @@ -151,7 +155,7 @@ export default class Path {
return this
}
function isAbsolute(part: string) {
if (Deno.build.os == 'windows' && part?.match(/^[a-zA-Z]:/)) {
if (Deno.build.os == 'windows' && (part?.match(/^[a-zA-Z]:/) || part?.startsWith("\\\\"))) {
return true
} else {
return part.startsWith('/')
Expand Down