diff options
author | Chris Knight <cknight1234@gmail.com> | 2020-03-14 21:46:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-14 17:46:39 -0400 |
commit | ea4f3ab852eba0eeb27004616163a0bbf9971215 (patch) | |
tree | 0e135fa6d55df589d795464294be2906d55f37bf /std/node/_fs/_fs_chmod.ts | |
parent | bf51f7b2ec1b64a7ff14f7255eba231409ee5414 (diff) |
feat: add chmod Node polyfill and related file system constants (#4358)
Diffstat (limited to 'std/node/_fs/_fs_chmod.ts')
-rw-r--r-- | std/node/_fs/_fs_chmod.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/std/node/_fs/_fs_chmod.ts b/std/node/_fs/_fs_chmod.ts new file mode 100644 index 000000000..cecb878ec --- /dev/null +++ b/std/node/_fs/_fs_chmod.ts @@ -0,0 +1,52 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { CallbackWithError } from "./_fs_common.ts"; + +const allowedModes = /^[0-7]{3}/; + +/** + * TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these + * are implemented. See https://github.com/denoland/deno/issues/3403 + */ +export function chmod( + path: string, + mode: string | number, + callback: CallbackWithError +): void { + new Promise(async (resolve, reject) => { + try { + await Deno.chmod(path, getResolvedMode(mode)); + resolve(); + } catch (err) { + reject(err); + } + }) + .then(() => { + callback(); + }) + .catch(err => { + callback(err); + }); +} + +/** + * TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these + * are implemented. See https://github.com/denoland/deno/issues/3403 + */ +export function chmodSync(path: string, mode: string | number): void { + Deno.chmodSync(path, getResolvedMode(mode)); +} + +function getResolvedMode(mode: string | number): number { + if (typeof mode === "number") { + return mode; + } + + if (typeof mode === "string") { + if (!allowedModes.test(mode)) { + throw new Error("Unrecognized mode: " + mode); + } + } + + return parseInt(mode, 8); +} |