Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(path): make isWindows check compatible with Node and Bun #4961

Merged
merged 18 commits into from
Aug 29, 2024
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 63 additions & 20 deletions path/_os.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,78 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

// Keep this up-to-date with Deno.build.os
export const isWindows: boolean = getIsWindows() === true;

function getIsWindows(): boolean | undefined {
return (
getIsWindowsOnDeno() || getIsWindowsOnBrowser() || getIsWindowsOnNodeOrBun()
);
}

/**
* Operating system type, equivalent to the type of
* {@linkcode https://deno.land/api?s=Deno.build | Deno.build.os}.
* @returns whether the os is windows or undefined if not running
* in a deno runtime, undefined if not a deno runtime.
*/
type OSType =
| "darwin"
| "linux"
| "windows"
| "freebsd"
| "netbsd"
| "aix"
| "solaris"
| "illumos";

const osType: OSType = (() => {
function getIsWindowsOnDeno(): boolean | undefined {
// deno-lint-ignore no-explicit-any
const { Deno } = globalThis as any;
if (typeof Deno?.build?.os === "string") {
return Deno.build.os;
return Deno.build.os === "windows";
}
}

/**
* @returns whether the os is windows or undefined if not running
* in a web browser, undefined if not a web browser
*/
function getIsWindowsOnBrowser(): boolean | undefined {
// deno-lint-ignore no-explicit-any
const { navigator } = globalThis as any;
if (navigator?.appVersion?.includes?.("Win")) {
return "windows";

return containsWindows(navigator?.userAgent);
}

/**
* according to documentation node's os module is implemented
* in bun as well.
* {@link https://bun.sh/docs/runtime/nodejs-apis#node-os}
*
* @returns whether the os is windows or undefined if not running
* on node or bun runtime
*/
function getIsWindowsOnNodeOrBun() {
const os = tryGettingNodeOsModule();

return containsWindows(os?.version());
}

type OsModule = {
version(): string;
};

// deno-lint-ignore no-explicit-any
declare const require: any;

function tryGettingNodeOsModule(): OsModule | undefined {
try {
return getNodeOsModule();
} catch {
return undefined;
}
}

return "linux";
})();
function getNodeOsModule(): OsModule | undefined {
if (require !== undefined) {
return require("os");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If node.js is in ESM mode (.mjs or type: module in package.json), then it doesn't have require.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I tested by manually converting to js and then running with node os.js, explains why this worked for me then.

I'll mark this PR as Draft again until I can come up with a better solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@std is published in JSR, and it's distributed as ESM in Node.js. So I think it's enough to only support ESM context.

} else {
return undefined;
}

Check warning on line 69 in path/_os.ts

View check run for this annotation

Codecov / codecov/patch

path/_os.ts#L66-L69

Added lines #L66 - L69 were not covered by tests
}

export const isWindows = osType === "windows";
function containsWindows(s?: string): boolean | undefined {
if (typeof s === "string") {
return s.toUpperCase().includes("WINDOWS");
} else {
return undefined;
}
}