Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pathutils_nodejs_support #14512

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 34 additions & 12 deletions compiler/pathutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
## to avoid the never ending time sink in getting path handling right.

import os, pathnorm

when defined(nodejs):
import jsffi
let fs = require("fs")
proc existsSync(fs: JsObject,file:cstring): bool =
fs.existsSync(file)
fs.existsSync = bindMethod existsSync

type
AbsoluteFile* = distinct string
AbsoluteDir* = distinct string
Expand All @@ -22,25 +28,39 @@ type
proc isEmpty*(x: AnyPath): bool {.inline.} = x.string.len == 0

proc copyFile*(source, dest: AbsoluteFile) =
os.copyFile(source.string, dest.string)
when defined(nodejs):
fs.createReadStream(source.cstring).pipe(fs.createWriteStream(dest.cstring));
else:
os.copyFile(source.string, dest.string)
Copy link
Member

@timotheecour timotheecour May 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is the wrong place to branch; instead make os.copyFile work with with nodejs
likewise with other procs.

That way is much more useful as it'll make clients of os work with nodejs.


proc removeFile*(x: AbsoluteFile) {.borrow.}
when defined(nodejs):
proc removeFile*(x: AbsoluteFile) = fs.unlinkSync(x.cstring)
else:
proc removeFile*(x: AbsoluteFile) {.borrow.}

proc splitFile*(x: AbsoluteFile): tuple[dir: AbsoluteDir, name, ext: string] =
let (a, b, c) = splitFile(x.string)
result = (dir: AbsoluteDir(a), name: b, ext: c)

proc extractFilename*(x: AbsoluteFile): string {.borrow.}

proc fileExists*(x: AbsoluteFile): bool {.borrow.}
proc dirExists*(x: AbsoluteDir): bool {.borrow.}
when defined(nodejs):
proc fileExists*(x: AbsoluteFile): bool = fs.existsSync(x.cstring)
proc dirExists*(x: AbsoluteDir): bool = fs.existsSync(x.cstring)
else:
proc fileExists*(x: AbsoluteFile): bool {.borrow.}
proc dirExists*(x: AbsoluteDir): bool {.borrow.}

proc quoteShell*(x: AbsoluteFile): string {.borrow.}
proc quoteShell*(x: AbsoluteDir): string {.borrow.}
when not defined(js):
proc quoteShell*(x: AbsoluteFile): string {.borrow.}
proc quoteShell*(x: AbsoluteDir): string {.borrow.}

proc cmpPaths*(x, y: AbsoluteDir): int {.borrow.}

proc createDir*(x: AbsoluteDir) {.borrow.}
when defined(nodejs):
proc createDir*(x: AbsoluteDir) = fs.mkdirSync(x.cstring)
else:
proc createDir*(x: AbsoluteDir) {.borrow.}

proc toAbsoluteDir*(path: string): AbsoluteDir =
result = if path.isAbsolute: AbsoluteDir(path)
Expand Down Expand Up @@ -70,16 +90,18 @@ when true:
assert(not isAbsolute(f.string), f.string)
result = AbsoluteFile newStringOfCap(base.string.len + f.string.len)
var state = 0
addNormalizePath(base.string, result.string, state)
addNormalizePath(f.string, result.string, state)
when not defined(js):
addNormalizePath(base.string, result.string, state)
addNormalizePath(f.string, result.string, state)

proc `/`*(base: AbsoluteDir; f: RelativeDir): AbsoluteDir =
let base = postProcessBase(base)
assert(not isAbsolute(f.string))
result = AbsoluteDir newStringOfCap(base.string.len + f.string.len)
var state = 0
addNormalizePath(base.string, result.string, state)
addNormalizePath(f.string, result.string, state)
when not defined(js):
addNormalizePath(base.string, result.string, state)
addNormalizePath(f.string, result.string, state)

proc relativeTo*(fullPath: AbsoluteFile, baseFilename: AbsoluteDir;
sep = DirSep): RelativeFile =
Expand Down