Skip to content

Commit

Permalink
wip/refactor ~ add DenoVx
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Oct 13, 2024
1 parent 30228e1 commit f9b94d1
Show file tree
Hide file tree
Showing 6 changed files with 13,024 additions and 11 deletions.
4 changes: 2 additions & 2 deletions eg/WanCopy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// spell-checker:ignore (names) Deno ; (vars) ARGX LOGLEVEL PATHEXT arr defaultCDNforNPM gmsu ; (utils) dprint dprintrc ; (yargs) nargs positionals

import { Deprecated } from '../src/lib/$deprecated.ts';
import { DenoVx } from '../src/lib/$deprecated.ts';

import {
copy as streamCopy,
Expand Down Expand Up @@ -327,7 +327,7 @@ const TARGET = await (async () => {
if (argv._?.length) argv._ = [];
if (!appState.usageError && target.length < 1) {
// note: isTerminal() added to stderr, stdin, and stdout in Deno v1.40.0 ; added to Deno.FsFile in Deno v1.41.0
if (!Deprecated.Deno.isatty(Deprecated.Deno.stdout.rid)) {
if (!DenoVx.isatty(Deno.stdout)) {
target.push('-');
} else {
appState.usageError = true;
Expand Down
104 changes: 97 additions & 7 deletions src/lib/$deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,109 @@

// Deno: { run: Deno.run, RunOptions: Deno.RunOptions },
// };
import deno = Deno;

// const deno = Deno;

// export const isDenoV1 = Deno.version.deno.startsWith('1.');

import * as _DenoV1NS from '../../vendor/@types/[email protected]';

export type DenoV1RID = DenoV1NS.RID;

// type guards

export function isDenoV1(o: unknown): o is typeof DenoV1NS.Deno {
const v = (o as { version?: { deno?: string } })?.version?.deno;
return !!v?.startsWith('1.');
}

export function isDenoV1RID(id: unknown): id is DenoV1NS.RID {
return typeof id === 'number';
}

function hasIsTerminalMethod(id: unknown): id is { isTerminal: () => boolean } {
return (
typeof id === 'object' &&
id != null &&
'isTerminal' in id &&
typeof id?.isTerminal === 'function' &&
typeof id.isTerminal() === 'boolean'
);
}

//

export const DenoV1 = isDenoV1(Deno) ? (Deno as unknown as typeof DenoV1NS.Deno) : undefined;
// export const DenoAsVx = DenoAsV1 ?? globalThis.Deno;

export const DenoVx = {
/** Close the given resource ID (`rid`) which has been previously opened, such
* as via opening or creating a file. Closing a file when you are finished
* with it is important to avoid leaking resources.
*
* ```ts
* const file = await Deno.open("my_file.txt");
* // do work with "file" object
* Deno.close(file.rid);
* ```
*
* It is recommended to define the variable with the `using` keyword so the
* runtime will automatically close the resource when it goes out of scope.
* Doing so negates the need to manually close the resource.
*
* ```ts
* using file = await Deno.open("my_file.txt");
* // do work with "file" object
* ```
*
* @category I/O
*/
close: (file: DenoV1NS.Deno.FsFile | globalThis.Deno.FsFile): void => {
if (file instanceof DenoV1NS.Deno.FsFile) {
return DenoV1?.close(file.rid);
}
return file.close();
},
/**
* Check if a given resource id (`rid`) is a TTY (a terminal).
*
* ```ts
* // This example is system and context specific
* const nonTTYRid = Deno.openSync("my_file.txt").rid;
* const ttyRid = Deno.openSync("/dev/tty6").rid;
* console.log(Deno.isatty(nonTTYRid)); // false
* console.log(Deno.isatty(ttyRid)); // true
* ```
*
* @category I/O
*/
isatty: (
id:
| globalThis.Deno.FsFile
| DenoV1NS.Deno.FsFile
| { isTerminal: () => boolean }
| { rid: DenoV1RID }
| DenoV1RID,
): boolean => {
if (id == null) return false;
if (hasIsTerminalMethod(id)) {
return id.isTerminal();
}
const rid = typeof id === 'number' ? id : id.rid;
return DenoV1?.isatty(rid) ?? false;
},
};

export namespace Deprecated {
export namespace Deno {
// deprecated since: ...
// use instead: ...
// remove with: Deno v2.0.0
export const close = (rid: number) => deno.close(rid);
export const isatty = (rid: number) => deno.isatty(rid);
export const stderr = { rid: deno.stderr.rid };
export const stdin = { rid: deno.stdin.rid };
export const stdout = { rid: deno.stdout.rid };
export const run = deno.run;
export const stderr = { rid: globalThis.Deno.stderr.rid };
export const stdin = { rid: globalThis.Deno.stdin.rid };
export const stdout = { rid: globalThis.Deno.stdout.rid };

export const run = DenoV1?.run ?? globalThis.Deno.run;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/consoleSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

//===

import { Deprecated } from './$deprecated.ts';
import { DenoVx, Deprecated } from './$deprecated.ts';

//===

Expand Down Expand Up @@ -205,7 +205,7 @@ export function consoleSizeViaDenoAPI(
// console.warn(`fallbackFileName = ${fallbackFileName}; isatty(...) = ${file && Deno.isatty(file.rid)}`);
size = file && denoConsoleSizeNT(file.rid);
// note: Deno.FsFile added (with close()) in Deno v1.19.0
file && Deprecated.Deno.close(file.rid);
file && DenoVx.close(file);
}

return size;
Expand Down
Loading

0 comments on commit f9b94d1

Please sign in to comment.