2023-02-14 11:38:45 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-06-27 02:18:22 -04:00
|
|
|
|
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts";
|
2023-06-16 06:43:59 -04:00
|
|
|
import { pathFromURL } from "ext:deno_web/00_infra.js";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { promisify } from "ext:deno_node/internal/util.mjs";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
type SymlinkType = "file" | "dir";
|
|
|
|
|
|
|
|
export function symlink(
|
|
|
|
target: string | URL,
|
|
|
|
path: string | URL,
|
|
|
|
typeOrCallback: SymlinkType | CallbackWithError,
|
|
|
|
maybeCallback?: CallbackWithError,
|
|
|
|
) {
|
2023-06-16 06:43:59 -04:00
|
|
|
target = target instanceof URL ? pathFromURL(target) : target;
|
|
|
|
path = path instanceof URL ? pathFromURL(path) : path;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
const type: SymlinkType = typeof typeOrCallback === "string"
|
|
|
|
? typeOrCallback
|
|
|
|
: "file";
|
|
|
|
|
|
|
|
const callback: CallbackWithError = typeof typeOrCallback === "function"
|
|
|
|
? typeOrCallback
|
|
|
|
: (maybeCallback as CallbackWithError);
|
|
|
|
|
|
|
|
if (!callback) throw new Error("No callback function supplied");
|
|
|
|
|
|
|
|
Deno.symlink(target, path, { type }).then(() => callback(null), callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const symlinkPromise = promisify(symlink) as (
|
|
|
|
target: string | URL,
|
|
|
|
path: string | URL,
|
|
|
|
type?: SymlinkType,
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
export function symlinkSync(
|
|
|
|
target: string | URL,
|
|
|
|
path: string | URL,
|
|
|
|
type?: SymlinkType,
|
|
|
|
) {
|
2023-06-16 06:43:59 -04:00
|
|
|
target = target instanceof URL ? pathFromURL(target) : target;
|
|
|
|
path = path instanceof URL ? pathFromURL(path) : path;
|
2023-02-14 11:38:45 -05:00
|
|
|
type = type || "file";
|
|
|
|
|
|
|
|
Deno.symlinkSync(target, path, { type });
|
|
|
|
}
|