Skip to content

Commit

Permalink
make work with dnt
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfiszel committed Nov 12, 2024
1 parent 21a4c61 commit c03b1fe
Show file tree
Hide file tree
Showing 27 changed files with 276 additions and 272 deletions.
5 changes: 2 additions & 3 deletions command/upgrade/_check_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { bold, yellow } from "@std/fmt/colors";
import { Command } from "../command.ts";

/** Check if new version is available and add hint to version. */
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
export async function checkVersion(cmd: Command<any>): Promise<void> {
const mainCommand = cmd.getMainCommand();
const upgradeCommand = mainCommand.getCommand("upgrade");
Expand All @@ -16,8 +16,7 @@ export async function checkVersion(cmd: Command<any>): Promise<void> {
if (!currentVersion || currentVersion === latestVersion) {
return;
}
const versionHelpText =
`(New version available: ${latestVersion}. Run '${mainCommand.getName()} upgrade' to upgrade to the latest version!)`;
const versionHelpText = `(New version available: ${latestVersion}. Run '${mainCommand.getName()} upgrade' to upgrade to the latest version!)`;

mainCommand.version(`${currentVersion} ${bold(yellow(versionHelpText))}`);
}
Expand Down
2 changes: 1 addition & 1 deletion command/upgrade/get_runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface GetRuntimeResult {

/** Get runtime handler for current runtime. */
export async function getRuntime(): Promise<GetRuntimeResult> {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;

if (Deno?.version?.deno) {
Expand Down
11 changes: 4 additions & 7 deletions command/upgrade/runtime/bun_runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ export class BunRuntime extends NodeRuntime {
protected override async execute(
cmdArgs: string[],
isJsr: boolean,
logger?: Logger,
logger?: Logger
): Promise<void> {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const Bun = (globalThis as any).Bun;
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const process = (globalThis as any).process;

cmdArgs = isJsr
? [`${process.execPath}x`, "jsr", ...cmdArgs]
: [process.execPath, ...cmdArgs];

logger?.log(
dim("$ %s"),
cmdArgs.join(" "),
);
logger?.log(dim("$ %s"), cmdArgs.join(" "));

const proc = Bun.spawn(cmdArgs, { stdout: "pipe", stderr: "pipe" });
await proc.exited;
Expand Down
4 changes: 2 additions & 2 deletions command/upgrade/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class Spinner {
* ```
*/
start() {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
if (this.#active || (globalThis as any).Deno?.stdout.writable.locked) {
return;
}
Expand All @@ -212,7 +212,7 @@ export class Spinner {
const frame = encoder.encode(
noColor
? this.#spinner[i] + " " + this.message
: color + this.#spinner[i] + COLOR_RESET + " " + this.message,
: color + this.#spinner[i] + COLOR_RESET + " " + this.message
);
// call writeSync once to reduce flickering
const writeData = new Uint8Array(LINE_CLEAR.length + frame.length);
Expand Down
78 changes: 42 additions & 36 deletions flags/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ const DefaultTypes: Record<ArgumentType, TypeHandler> = {
export function parseFlags<
TFlags extends Record<string, unknown>,
TFlagOptions extends FlagOptions,
TFlagsResult extends ParseFlagsContext,
TFlagsResult extends ParseFlagsContext
>(
argsOrCtx: string[] | TFlagsResult,
opts: ParseFlagsOptions<TFlagOptions> = {},
opts: ParseFlagsOptions<TFlagOptions> = {}
): TFlagsResult & ParseFlagsContext<TFlags, TFlagOptions> {
let args: Array<string>;
let ctx: ParseFlagsContext<Record<string, unknown>>;
Expand Down Expand Up @@ -113,7 +113,7 @@ export function parseFlags<
}

function validateOptions<TFlagOptions extends FlagOptions>(
opts: ParseFlagsOptions<TFlagOptions>,
opts: ParseFlagsOptions<TFlagOptions>
) {
opts.flags?.forEach((opt) => {
opt.depends?.forEach((flag) => {
Expand All @@ -132,17 +132,13 @@ function validateOptions<TFlagOptions extends FlagOptions>(
function parseArgs<TFlagOptions extends FlagOptions>(
ctx: ParseFlagsContext<Record<string, unknown>>,
args: Array<string>,
opts: ParseFlagsOptions<TFlagOptions>,
opts: ParseFlagsOptions<TFlagOptions>
): Map<string, FlagOptions> {
/** Option name mapping: propertyName -> option.name */
const optionsMap: Map<string, FlagOptions> = new Map();
let inLiteral = false;

for (
let argsIndex = 0;
argsIndex < args.length;
argsIndex++
) {
for (let argsIndex = 0; argsIndex < args.length; argsIndex++) {
let option: FlagOptions | undefined;
let current: string = args[argsIndex];
let currentValue: string | undefined;
Expand Down Expand Up @@ -233,17 +229,20 @@ function parseArgs<TFlagOptions extends FlagOptions>(
}

if (option.type && !option.args?.length) {
option.args = [{
type: option.type,
optional: option.optionalValue,
variadic: option.variadic,
list: option.list,
separator: option.separator,
}];
option.args = [
{
type: option.type,
optional: option.optionalValue,
variadic: option.variadic,
list: option.list,
separator: option.separator,
},
];
}

if (
opts.flags?.length && !option.args?.length &&
opts.flags?.length &&
!option.args?.length &&
typeof currentValue !== "undefined"
) {
throw new UnexpectedOptionValueError(option.name, currentValue);
Expand Down Expand Up @@ -273,9 +272,12 @@ function parseArgs<TFlagOptions extends FlagOptions>(
const value = option.value(ctx.flags[propName], previous);
setFlagValue(value);
} else if (option.collect) {
const value: unknown[] = typeof previous !== "undefined"
? (Array.isArray(previous) ? previous : [previous])
: [];
const value: unknown[] =
typeof previous !== "undefined"
? Array.isArray(previous)
? previous
: [previous]
: [];

value.push(ctx.flags[propName]);
setFlagValue(value);
Expand Down Expand Up @@ -333,7 +335,7 @@ function parseArgs<TFlagOptions extends FlagOptions>(
throw new InvalidOptionValueError(
option.name,
arg.type ?? "?",
nextValue,
nextValue
);
}
return value;
Expand Down Expand Up @@ -393,15 +395,19 @@ function parseArgs<TFlagOptions extends FlagOptions>(
}
// require optional values to be called with an equal sign: foo=bar
if (
option.equalsSign && arg.optional && !arg.variadic &&
option.equalsSign &&
arg.optional &&
!arg.variadic &&
typeof currentValue === "undefined"
) {
return false;
}
if (arg.optional || arg.variadic) {
return nextValue[0] !== "-" ||
return (
nextValue[0] !== "-" ||
typeof currentValue !== "undefined" ||
(arg.type === OptionType.NUMBER && !isNaN(Number(nextValue)));
(arg.type === OptionType.NUMBER && !isNaN(Number(nextValue)))
);
}

return false;
Expand All @@ -411,15 +417,15 @@ function parseArgs<TFlagOptions extends FlagOptions>(
function parseValue(
option: FlagOptions,
arg: ArgumentOptions,
value: string,
value: string
): unknown {
const result: unknown = opts.parse
? opts.parse({
label: "Option",
type: arg.type || OptionType.STRING,
name: `--${option.name}`,
value,
})
label: "Option",
type: arg.type || OptionType.STRING,
name: `--${option.name}`,
value,
})
: parseDefaultType(option, arg, value);

if (typeof result !== "undefined") {
Expand Down Expand Up @@ -449,11 +455,11 @@ function parseDottedOptions(ctx: ParseFlagsContext): void {
if (~key.indexOf(".")) {
key.split(".").reduce(
(
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
result: Record<string, any>,
subKey: string,
index: number,
parts: string[],
parts: string[]
) => {
if (index === parts.length - 1) {
result[subKey] = ctx.flags[key];
Expand All @@ -462,14 +468,14 @@ function parseDottedOptions(ctx: ParseFlagsContext): void {
}
return result[subKey];
},
result,
result
);
} else {
result[key] = ctx.flags[key];
}
return result;
},
{},
{}
);
}

Expand Down Expand Up @@ -498,9 +504,9 @@ function splitFlags(flag: string): Array<string> {
function parseDefaultType(
option: FlagOptions,
arg: ArgumentOptions,
value: string,
value: string
): unknown {
const type: ArgumentType = arg.type as ArgumentType || OptionType.STRING;
const type: ArgumentType = (arg.type as ArgumentType) || OptionType.STRING;
const parseType = DefaultTypes[type];

if (!parseType) {
Expand Down
10 changes: 5 additions & 5 deletions flags/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Parser options. */
export interface ParseFlagsOptions<
TFlagOptions extends FlagOptions = FlagOptions,
TFlagOptions extends FlagOptions = FlagOptions
> {
/** An array of flag options. */
flags?: Array<TFlagOptions>;
Expand Down Expand Up @@ -107,20 +107,20 @@ export type DefaultValue<TValue = unknown> =
export type DefaultValueHandler<TValue = unknown> = () => TValue;

/** A callback method for custom processing or mapping of flag values. */
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
export type ValueHandler<TValue = any, TReturn = TValue> = (
val: TValue,
previous?: TReturn,
previous?: TReturn
) => TReturn;

/**
* Parse result. The parse context will be returned by the `parseFlags` method
* and can be also passed as first argument to the `parseFlags` method.
*/
export interface ParseFlagsContext<
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
TFlags extends Record<string, any> = Record<string, any>,
TStandaloneOption extends FlagOptions = FlagOptions,
TStandaloneOption extends FlagOptions = FlagOptions
> {
/** An object of parsed flags. */
flags: TFlags;
Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/delete_env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @param name The name of the environment variable.
*/
export function deleteEnv(name: string): string | undefined {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;

if (Deno) {
Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @param code The exit code.
*/
export function exit(code: number): never {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;
const exit: (code: number) => never = Deno?.exit ?? process?.exit;

Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/get_args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @internal
*/
export function getArgs(): Array<string> {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;

return Deno?.args ?? process?.argv.slice(2) ?? [];
Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/get_columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
export function getColumns(): number | null {
try {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;

// Catch error in none tty mode: Inappropriate ioctl for device (os error 25)
Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/get_env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @param name The name of the environment variable.
*/
export function getEnv(name: string): string | undefined {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;

if (Deno) {
Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/get_os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getOs():
| "openbsd"
| "sunos"
| "win32" {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;

if (Deno) {
Expand Down
10 changes: 5 additions & 5 deletions internal/runtime/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* @internal
*/
export function inspect(value: unknown, colors: boolean): string {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno } = globalThis as any;

return Deno?.inspect(
value,
{ depth: 1, colors, trailingComma: false },
) ?? JSON.stringify(value, null, 2);
return (
Deno?.inspect(value, { depth: 1, colors, trailingComma: false }) ??
JSON.stringify(value, null, 2)
);
}
2 changes: 1 addition & 1 deletion internal/runtime/is_terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @internal
*/
export function isTerminal(): boolean {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;

if (Deno) {
Expand Down
7 changes: 4 additions & 3 deletions internal/runtime/no_color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
* @internal
*/
export function getNoColor(): boolean {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, process } = globalThis as any;

if (Deno) {
return Deno.noColor;
} else if (process) {
return process?.env.NO_COLOR === "1" ||
process?.env.NODE_DISABLE_COLORS === "1";
return (
process?.env.NO_COLOR === "1" || process?.env.NODE_DISABLE_COLORS === "1"
);
}

throw new Error("unsupported runtime");
Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @param data Uint8Array to store the data.
*/
export async function read(data: Uint8Array): Promise<number | null> {
// deno-lint-ignore no-explicit-any
// dnt-shim-ignore deno-lint-ignore no-explicit-any
const { Deno, Bun, process } = globalThis as any;

if (Deno) {
Expand Down
Loading

0 comments on commit c03b1fe

Please sign in to comment.