1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 16:49:18 -05:00
denoland-deno/std/node/_fs/_fs_chmod.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import type { CallbackWithError } from "./_fs_common.ts";
import { fromFileUrl } from "../path.ts";
const allowedModes = /^[0-7]{3}/;
/**
* TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
export function chmod(
path: string | URL,
mode: string | number,
callback: CallbackWithError,
): void {
path = path instanceof URL ? fromFileUrl(path) : path;
Deno.chmod(path, getResolvedMode(mode))
.then(() => callback())
.catch(callback);
}
/**
* TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
export function chmodSync(path: string | URL, mode: string | number): void {
path = path instanceof URL ? fromFileUrl(path) : path;
Deno.chmodSync(path, getResolvedMode(mode));
}
function getResolvedMode(mode: string | number): number {
if (typeof mode === "number") {
return mode;
}
2020-04-29 21:36:44 -04:00
if (typeof mode === "string" && !allowedModes.test(mode)) {
throw new Error("Unrecognized mode: " + mode);
}
return parseInt(mode, 8);
}