diff options
author | Stanislav <62983943+stanislavstrelnikov@users.noreply.github.com> | 2020-07-07 04:45:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-06 21:45:39 -0400 |
commit | 158ae0bfe900d2bac3076390c4fe3d2b54d94fe5 (patch) | |
tree | 209e4b5682e2a899041767c49428e34329e48084 /cli/js/ops/fs | |
parent | ab4c574f5202f607ceb6068f56b3cc8aed1bbbaf (diff) |
clean up code in cli/js (#6611)
Diffstat (limited to 'cli/js/ops/fs')
-rw-r--r-- | cli/js/ops/fs/chmod.ts | 7 | ||||
-rw-r--r-- | cli/js/ops/fs/chown.ts | 7 | ||||
-rw-r--r-- | cli/js/ops/fs/copy_file.ts | 17 | ||||
-rw-r--r-- | cli/js/ops/fs/dir.ts | 1 | ||||
-rw-r--r-- | cli/js/ops/fs/link.ts | 1 | ||||
-rw-r--r-- | cli/js/ops/fs/make_temp.ts | 1 | ||||
-rw-r--r-- | cli/js/ops/fs/mkdir.ts | 19 | ||||
-rw-r--r-- | cli/js/ops/fs/open.ts | 11 | ||||
-rw-r--r-- | cli/js/ops/fs/read_dir.ts | 9 | ||||
-rw-r--r-- | cli/js/ops/fs/read_link.ts | 1 | ||||
-rw-r--r-- | cli/js/ops/fs/real_path.ts | 1 | ||||
-rw-r--r-- | cli/js/ops/fs/remove.ts | 13 | ||||
-rw-r--r-- | cli/js/ops/fs/rename.ts | 1 | ||||
-rw-r--r-- | cli/js/ops/fs/seek.ts | 1 | ||||
-rw-r--r-- | cli/js/ops/fs/stat.ts | 45 | ||||
-rw-r--r-- | cli/js/ops/fs/symlink.ts | 9 | ||||
-rw-r--r-- | cli/js/ops/fs/sync.ts | 1 | ||||
-rw-r--r-- | cli/js/ops/fs/truncate.ts | 7 | ||||
-rw-r--r-- | cli/js/ops/fs/umask.ts | 1 | ||||
-rw-r--r-- | cli/js/ops/fs/utime.ts | 1 |
20 files changed, 82 insertions, 72 deletions
diff --git a/cli/js/ops/fs/chmod.ts b/cli/js/ops/fs/chmod.ts index 76a3c8f49..a2236935b 100644 --- a/cli/js/ops/fs/chmod.ts +++ b/cli/js/ops/fs/chmod.ts @@ -1,13 +1,12 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; export function chmodSync(path: string | URL, mode: number): void { - path = pathFromURL(path); - sendSync("op_chmod", { path, mode }); + sendSync("op_chmod", { path: pathFromURL(path), mode }); } export async function chmod(path: string | URL, mode: number): Promise<void> { - path = pathFromURL(path); - await sendAsync("op_chmod", { path, mode }); + await sendAsync("op_chmod", { path: pathFromURL(path), mode }); } diff --git a/cli/js/ops/fs/chown.ts b/cli/js/ops/fs/chown.ts index 3afe07f16..52735e097 100644 --- a/cli/js/ops/fs/chown.ts +++ b/cli/js/ops/fs/chown.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -7,8 +8,7 @@ export function chownSync( uid: number | null, gid: number | null ): void { - path = pathFromURL(path); - sendSync("op_chown", { path, uid, gid }); + sendSync("op_chown", { path: pathFromURL(path), uid, gid }); } export async function chown( @@ -16,6 +16,5 @@ export async function chown( uid: number | null, gid: number | null ): Promise<void> { - path = pathFromURL(path); - await sendAsync("op_chown", { path, uid, gid }); + await sendAsync("op_chown", { path: pathFromURL(path), uid, gid }); } diff --git a/cli/js/ops/fs/copy_file.ts b/cli/js/ops/fs/copy_file.ts index 6bbb3f599..fcb147bdd 100644 --- a/cli/js/ops/fs/copy_file.ts +++ b/cli/js/ops/fs/copy_file.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -6,18 +7,18 @@ export function copyFileSync( fromPath: string | URL, toPath: string | URL ): void { - fromPath = pathFromURL(fromPath); - toPath = pathFromURL(toPath); - - sendSync("op_copy_file", { from: fromPath, to: toPath }); + sendSync("op_copy_file", { + from: pathFromURL(fromPath), + to: pathFromURL(toPath), + }); } export async function copyFile( fromPath: string | URL, toPath: string | URL ): Promise<void> { - fromPath = pathFromURL(fromPath); - toPath = pathFromURL(toPath); - - await sendAsync("op_copy_file", { from: fromPath, to: toPath }); + await sendAsync("op_copy_file", { + from: pathFromURL(fromPath), + to: pathFromURL(toPath), + }); } diff --git a/cli/js/ops/fs/dir.ts b/cli/js/ops/fs/dir.ts index 14b6240ed..dbf468c62 100644 --- a/cli/js/ops/fs/dir.ts +++ b/cli/js/ops/fs/dir.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync } from "../dispatch_json.ts"; export function cwd(): string { diff --git a/cli/js/ops/fs/link.ts b/cli/js/ops/fs/link.ts index 92fb58834..05ff358ef 100644 --- a/cli/js/ops/fs/link.ts +++ b/cli/js/ops/fs/link.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function linkSync(oldpath: string, newpath: string): void { diff --git a/cli/js/ops/fs/make_temp.ts b/cli/js/ops/fs/make_temp.ts index 85dea5f20..3996744d1 100644 --- a/cli/js/ops/fs/make_temp.ts +++ b/cli/js/ops/fs/make_temp.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export interface MakeTempOptions { diff --git a/cli/js/ops/fs/mkdir.ts b/cli/js/ops/fs/mkdir.ts index 374a00d83..61ea1c218 100644 --- a/cli/js/ops/fs/mkdir.ts +++ b/cli/js/ops/fs/mkdir.ts @@ -1,11 +1,21 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; -type MkdirArgs = { path: string; recursive: boolean; mode?: number }; +export interface MkdirOptions { + recursive?: boolean; + mode?: number; +} + +interface MkdirArgs { + path: string; + recursive: boolean; + mode?: number; +} function mkdirArgs(path: string, options?: MkdirOptions): MkdirArgs { const args: MkdirArgs = { path, recursive: false }; - if (options) { + if (options != null) { if (typeof options.recursive == "boolean") { args.recursive = options.recursive; } @@ -16,11 +26,6 @@ function mkdirArgs(path: string, options?: MkdirOptions): MkdirArgs { return args; } -export interface MkdirOptions { - recursive?: boolean; - mode?: number; -} - export function mkdirSync(path: string, options?: MkdirOptions): void { sendSync("op_mkdir", mkdirArgs(path, options)); } diff --git a/cli/js/ops/fs/open.ts b/cli/js/ops/fs/open.ts index 3742d0b52..edd52c376 100644 --- a/cli/js/ops/fs/open.ts +++ b/cli/js/ops/fs/open.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -18,8 +19,7 @@ export interface OpenOptions { export function openSync(path: string | URL, options: OpenOptions): number { const mode: number | undefined = options?.mode; - path = pathFromURL(path); - return sendSync("op_open", { path, options, mode }); + return sendSync("op_open", { path: pathFromURL(path), options, mode }); } export function open( @@ -27,10 +27,5 @@ export function open( options: OpenOptions ): Promise<number> { const mode: number | undefined = options?.mode; - path = pathFromURL(path); - return sendAsync("op_open", { - path, - options, - mode, - }); + return sendAsync("op_open", { path: pathFromURL(path), options, mode }); } diff --git a/cli/js/ops/fs/read_dir.ts b/cli/js/ops/fs/read_dir.ts index 09c5d1c12..6ffe6116e 100644 --- a/cli/js/ops/fs/read_dir.ts +++ b/cli/js/ops/fs/read_dir.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -18,13 +19,13 @@ function res(response: ReadDirResponse): DirEntry[] { } export function readDirSync(path: string | URL): Iterable<DirEntry> { - path = pathFromURL(path); - return res(sendSync("op_read_dir", { path }))[Symbol.iterator](); + return res(sendSync("op_read_dir", { path: pathFromURL(path) }))[ + Symbol.iterator + ](); } export function readDir(path: string | URL): AsyncIterable<DirEntry> { - path = pathFromURL(path); - const array = sendAsync("op_read_dir", { path }).then(res); + const array = sendAsync("op_read_dir", { path: pathFromURL(path) }).then(res); return { async *[Symbol.asyncIterator](): AsyncIterableIterator<DirEntry> { yield* await array; diff --git a/cli/js/ops/fs/read_link.ts b/cli/js/ops/fs/read_link.ts index bcd27ccb7..33fef7e36 100644 --- a/cli/js/ops/fs/read_link.ts +++ b/cli/js/ops/fs/read_link.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function readLinkSync(path: string): string { diff --git a/cli/js/ops/fs/real_path.ts b/cli/js/ops/fs/real_path.ts index ab95b9583..c424d99bc 100644 --- a/cli/js/ops/fs/real_path.ts +++ b/cli/js/ops/fs/real_path.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function realPathSync(path: string): string { diff --git a/cli/js/ops/fs/remove.ts b/cli/js/ops/fs/remove.ts index d1a8702f1..52f4cad40 100644 --- a/cli/js/ops/fs/remove.ts +++ b/cli/js/ops/fs/remove.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { pathFromURL } from "../../util.ts"; @@ -10,14 +11,18 @@ export function removeSync( path: string | URL, options: RemoveOptions = {} ): void { - path = pathFromURL(path); - sendSync("op_remove", { path, recursive: !!options.recursive }); + sendSync("op_remove", { + path: pathFromURL(path), + recursive: !!options.recursive, + }); } export async function remove( path: string | URL, options: RemoveOptions = {} ): Promise<void> { - path = pathFromURL(path); - await sendAsync("op_remove", { path, recursive: !!options.recursive }); + await sendAsync("op_remove", { + path: pathFromURL(path), + recursive: !!options.recursive, + }); } diff --git a/cli/js/ops/fs/rename.ts b/cli/js/ops/fs/rename.ts index 9f02c8bc0..f0789d3eb 100644 --- a/cli/js/ops/fs/rename.ts +++ b/cli/js/ops/fs/rename.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function renameSync(oldpath: string, newpath: string): void { diff --git a/cli/js/ops/fs/seek.ts b/cli/js/ops/fs/seek.ts index c7e4c9172..2e23e084b 100644 --- a/cli/js/ops/fs/seek.ts +++ b/cli/js/ops/fs/seek.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { SeekMode } from "../../io.ts"; diff --git a/cli/js/ops/fs/stat.ts b/cli/js/ops/fs/stat.ts index 402adeafc..f444190fd 100644 --- a/cli/js/ops/fs/stat.ts +++ b/cli/js/ops/fs/stat.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; import { build } from "../../build.ts"; import { pathFromURL } from "../../util.ts"; @@ -44,7 +45,7 @@ export interface StatResponse { // @internal export function parseFileInfo(response: StatResponse): FileInfo { - const isUnix = build.os === "darwin" || build.os === "linux"; + const unix = build.os === "darwin" || build.os === "linux"; return { isFile: response.isFile, isDirectory: response.isDirectory, @@ -54,15 +55,15 @@ export function parseFileInfo(response: StatResponse): FileInfo { atime: response.atime != null ? new Date(response.atime) : null, birthtime: response.birthtime != null ? new Date(response.birthtime) : null, // Only non-null if on Unix - dev: isUnix ? response.dev : null, - ino: isUnix ? response.ino : null, - mode: isUnix ? response.mode : null, - nlink: isUnix ? response.nlink : null, - uid: isUnix ? response.uid : null, - gid: isUnix ? response.gid : null, - rdev: isUnix ? response.rdev : null, - blksize: isUnix ? response.blksize : null, - blocks: isUnix ? response.blocks : null, + dev: unix ? response.dev : null, + ino: unix ? response.ino : null, + mode: unix ? response.mode : null, + nlink: unix ? response.nlink : null, + uid: unix ? response.uid : null, + gid: unix ? response.gid : null, + rdev: unix ? response.rdev : null, + blksize: unix ? response.blksize : null, + blocks: unix ? response.blocks : null, }; } @@ -75,37 +76,33 @@ export async function fstat(rid: number): Promise<FileInfo> { } export async function lstat(path: string | URL): Promise<FileInfo> { - path = pathFromURL(path); - const res = (await sendAsync("op_stat", { - path, + const res = await sendAsync("op_stat", { + path: pathFromURL(path), lstat: true, - })) as StatResponse; + }); return parseFileInfo(res); } export function lstatSync(path: string | URL): FileInfo { - path = pathFromURL(path); const res = sendSync("op_stat", { - path, + path: pathFromURL(path), lstat: true, - }) as StatResponse; + }); return parseFileInfo(res); } export async function stat(path: string | URL): Promise<FileInfo> { - path = pathFromURL(path); - const res = (await sendAsync("op_stat", { - path, + const res = await sendAsync("op_stat", { + path: pathFromURL(path), lstat: false, - })) as StatResponse; + }); return parseFileInfo(res); } export function statSync(path: string | URL): FileInfo { - path = pathFromURL(path); const res = sendSync("op_stat", { - path, + path: pathFromURL(path), lstat: false, - }) as StatResponse; + }); return parseFileInfo(res); } diff --git a/cli/js/ops/fs/symlink.ts b/cli/js/ops/fs/symlink.ts index fde611b55..7d4741928 100644 --- a/cli/js/ops/fs/symlink.ts +++ b/cli/js/ops/fs/symlink.ts @@ -1,14 +1,15 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; -export type symlinkOptions = { +export interface SymlinkOptions { type: "file" | "dir"; -}; +} export function symlinkSync( oldpath: string, newpath: string, - options?: symlinkOptions + options?: SymlinkOptions ): void { sendSync("op_symlink", { oldpath, newpath, options }); } @@ -16,7 +17,7 @@ export function symlinkSync( export async function symlink( oldpath: string, newpath: string, - options?: symlinkOptions + options?: SymlinkOptions ): Promise<void> { await sendAsync("op_symlink", { oldpath, newpath, options }); } diff --git a/cli/js/ops/fs/sync.ts b/cli/js/ops/fs/sync.ts index 567aab55b..7f208b8bd 100644 --- a/cli/js/ops/fs/sync.ts +++ b/cli/js/ops/fs/sync.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; export function fdatasyncSync(rid: number): void { diff --git a/cli/js/ops/fs/truncate.ts b/cli/js/ops/fs/truncate.ts index 2b805e5ac..d18e5d9d9 100644 --- a/cli/js/ops/fs/truncate.ts +++ b/cli/js/ops/fs/truncate.ts @@ -1,12 +1,9 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; function coerceLen(len?: number): number { - if (!len) { - return 0; - } - - if (len < 0) { + if (len == null || len < 0) { return 0; } diff --git a/cli/js/ops/fs/umask.ts b/cli/js/ops/fs/umask.ts index 38bf8ff6c..fbc94091e 100644 --- a/cli/js/ops/fs/umask.ts +++ b/cli/js/ops/fs/umask.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync } from "../dispatch_json.ts"; export function umask(mask?: number): number { diff --git a/cli/js/ops/fs/utime.ts b/cli/js/ops/fs/utime.ts index 2755a7e96..fa86038c6 100644 --- a/cli/js/ops/fs/utime.ts +++ b/cli/js/ops/fs/utime.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + import { sendSync, sendAsync } from "../dispatch_json.ts"; function toSecondsFromEpoch(v: number | Date): number { |