├── .gitattributes ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .vscode ├── extensions.json └── settings.json ├── CHANGELOG.md ├── LICENSE ├── README.md ├── ci.ts ├── dark_mode.ts ├── docker.ts ├── gzip.ts ├── mod.ts ├── running.ts ├── ssh.ts ├── test ├── deps.ts ├── running.test.ts └── wsl.test.ts ├── util.ts └── wsl.ts /.gitattributes: -------------------------------------------------------------------------------- 1 | * eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | name: ${{ matrix.kind }} ${{ matrix.os }} 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | matrix: 11 | os: [macOS-latest, ubuntu-latest] 12 | 13 | env: 14 | GH_ACTIONS: true 15 | DENO_BUILD_MODE: release 16 | V8_BINARY: true 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | - name: Setup Deno 21 | uses: denolib/setup-deno@master 22 | with: 23 | deno-version: 1.3 24 | 25 | - name: Format 26 | run: deno fmt --check 27 | 28 | - name: Lint 29 | run: deno lint --unstable 30 | 31 | - name: Tests 32 | run: deno test --reload --unstable 33 | 34 | - name: Release 35 | uses: softprops/action-gh-release@v1 36 | if: | 37 | matrix.os == 'ubuntu-latest' && 38 | startsWith(github.repository, 'justjavac') && 39 | startsWith(github.ref, 'refs/tags/') 40 | env: 41 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 42 | with: 43 | draft: true 44 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | .cache 3 | .DS_Store 4 | *bak 5 | .history 6 | .temp/** 7 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": ["justjavac.vscode-deno"] 3 | } 4 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "deno.enable": true 3 | } 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 0.5.0 - [2020-08-05] 4 | 5 | - bump deno@1.2.2 6 | 7 | ## 0.4.0 - [2020-04-11] 8 | 9 | - bump deno@0.40.0 10 | 11 | ## 0.3.0 - [2019-09-10] 12 | 13 | - remove `Deno.platform` usage [#3](https://github.com/justjavac/deno-is/pull/3) 14 | 15 | ## 0.2.2 - [2019-09-02] 16 | 17 | - add `isSSH`/`isSSHSync` [#2](https://github.com/justjavac/deno-is/pull/2) 18 | 19 | ## 0.2.1 - [2019-09-02] 20 | 21 | - update readme 22 | 23 | ## 0.2.0 - [2019-09-02] 24 | 25 | - add async and sync mode 26 | 27 | ## 0.1.0 - [2019-08-30] 28 | 29 | first release 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) justjavac 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # deno-is 2 | 3 | [![tag](https://img.shields.io/github/release/justjavac/deno-is)](https://github.com/justjavac/deno-is/releases) 4 | [![Build Status](https://github.com/justjavac/deno-is/workflows/ci/badge.svg?branch=master)](https://github.com/justjavac/deno-is/actions) 5 | [![license](https://img.shields.io/github/license/justjavac/deno-is)](https://github.com/justjavac/deno-is/blob/master/LICENSE) 6 | [![](https://img.shields.io/badge/deno-v1.2-green.svg)](https://github.com/denoland/deno) 7 | 8 | > Detect the running environment and context of the current script. 9 | 10 | ## Usage 11 | 12 | ```ts 13 | import { isRunning, isRunningSync, isCI, isCISync } from "https://deno.land/x/is/mod.ts"; 14 | 15 | isCISync(); 16 | // => false 17 | isRunningSync(Deno.pid); 18 | // => true 19 | 20 | await isCI(); 21 | // => false 22 | await isRunning(Deno.pid); 23 | // => true 24 | 25 | ``` 26 | 27 | or 28 | 29 | ```ts 30 | import { isCI, isCISync } from "https://deno.land/x/is/ci.ts"; 31 | 32 | isCISync(); 33 | // => false 34 | 35 | await isCI(); 36 | // => false 37 | ``` 38 | 39 | ## Available methods 40 | 41 | _alphabetical order_: 42 | 43 | | Methods | Description | 44 | |-----------------------------|----------------------------------------------------------------------------------------| 45 | | `isCI`/`isCISync` | Whether the process is running on the CI server | 46 | | `isDocker`/`isDockerSync` | Whether the process is running inside Docker | 47 | | `isGzip`/`isGzipSync` | Whether a `Uint8Array` is a gzip file | 48 | | `isRunning`/`isRunningSync` | Whether the process(pid) is running | 49 | | `isSSH`/`isSSHSync` | Whether the process is running inside SSH | 50 | | `isWsl`/`isWslSync` | Whether the process is running inside [Windows Subsystem for Linux][1] | 51 | 52 | [1]: https://msdn.microsoft.com/commandline/wsl/about 53 | 54 | ### Credits 55 | 56 | - [justjavac](https://github.com/justjavac) 57 | 58 | ### License 59 | 60 | [deno-is](https://github.com/justjavac/deno-is) is released under the MIT License. See the bundled [LICENSE](./LICENSE) file for details. 61 | -------------------------------------------------------------------------------- /ci.ts: -------------------------------------------------------------------------------- 1 | import ci from "https://deno.land/x/ci/mod.ts"; 2 | 3 | /** 4 | * Return `true` if the process is running on the CI server. 5 | * 6 | * Requires the `--allow-env` flag. 7 | */ 8 | export async function isCI(): Promise { 9 | return ci.isCI; 10 | } 11 | 12 | /** 13 | * Return `true` if the process is running on the CI server synchronously. 14 | * 15 | * Requires the `--allow-env` flag. 16 | */ 17 | export function isCISync(): boolean { 18 | return ci.isCI; 19 | } 20 | -------------------------------------------------------------------------------- /dark_mode.ts: -------------------------------------------------------------------------------- 1 | import { decode } from "https://deno.land/std/strings/decode.ts"; 2 | 3 | /** 4 | * Return `true` if is set to dark mode/theme. 5 | * 6 | * see https://stackoverflow.com/questions/25207077/how-to-detect-if-os-x-is-in-dark-mode 7 | * 8 | * Requires the `--allow-run` flag. 9 | */ 10 | export async function isDarkMode(): Promise { 11 | if (Deno.platform.os === "mac") { 12 | try { 13 | const process: Deno.Process = Deno.run({ 14 | args: ["defaults", "read", "-g", "AppleInterfaceStyle"], 15 | stdout: "piped", 16 | stderr: "null" 17 | }); 18 | const output: string = decode(await process.output()); 19 | process.close(); 20 | return output.includes("Dark"); 21 | } catch { 22 | return false; 23 | } 24 | } 25 | 26 | return false; 27 | } 28 | -------------------------------------------------------------------------------- /docker.ts: -------------------------------------------------------------------------------- 1 | import { readFileStr, readFileStrSync } from "https://deno.land/std/fs/mod.ts"; 2 | 3 | // see: https://github.com/sindresorhus/is-docker 4 | 5 | async function hasDockerEnv(): Promise { 6 | try { 7 | await Deno.stat("/.dockerenv"); 8 | return true; 9 | } catch { 10 | return false; 11 | } 12 | } 13 | 14 | function hasDockerEnvSync(): boolean { 15 | try { 16 | Deno.statSync("/.dockerenv"); 17 | return true; 18 | } catch { 19 | return false; 20 | } 21 | } 22 | 23 | async function hasDockerCGroup(): Promise { 24 | try { 25 | return (await readFileStr("/proc/self/cgroup")).includes("docker"); 26 | } catch { 27 | return false; 28 | } 29 | } 30 | 31 | function hasDockerCGroupSync(): boolean { 32 | try { 33 | return readFileStrSync("/proc/self/cgroup").includes("docker"); 34 | } catch { 35 | return false; 36 | } 37 | } 38 | 39 | /** 40 | * Return `true` if the process is running inside a Docker container. 41 | * 42 | * Requires the `--allow-read` flag. 43 | */ 44 | export async function isDocker(): Promise { 45 | return await hasDockerEnv() || await hasDockerCGroup(); 46 | } 47 | 48 | /** 49 | * Return `true` if the process is running inside a Docker container synchronously. 50 | * 51 | * Requires the `--allow-read` flag. 52 | */ 53 | export function isDockerSync(): boolean { 54 | return hasDockerEnvSync() || hasDockerCGroupSync(); 55 | } 56 | -------------------------------------------------------------------------------- /gzip.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Return `true` if `src` is a gzip file. 3 | */ 4 | export async function isGzip(src: Uint8Array): Promise { 5 | if (src.length < 3) { 6 | return false; 7 | } 8 | 9 | return src[0] === 0x1f && src[1] === 0x8b && src[2] === 0x08; 10 | } 11 | 12 | /** 13 | * Return `true` if `src` is a gzip file synchronously. 14 | */ 15 | export function isGzipSync(src: Uint8Array): boolean { 16 | if (src.length < 3) { 17 | return false; 18 | } 19 | 20 | return src[0] === 0x1f && src[1] === 0x8b && src[2] === 0x08; 21 | } 22 | -------------------------------------------------------------------------------- /mod.ts: -------------------------------------------------------------------------------- 1 | export * from "./ci.ts"; 2 | export * from "./docker.ts"; 3 | export * from "./gzip.ts"; 4 | export * from "./running.ts"; 5 | export * from "./ssh.ts"; 6 | export * from "./wsl.ts"; 7 | -------------------------------------------------------------------------------- /running.ts: -------------------------------------------------------------------------------- 1 | // see: http://man7.org/linux/man-pages/man2/kill.2.html 2 | 3 | /** 4 | * Return `true` if the given PID is running. 5 | * 6 | * Requires the `--allow-run` flag. 7 | * Requires the `--unstable` flag. 8 | */ 9 | export async function isRunning(pid: number): Promise { 10 | try { 11 | Deno.kill(pid, Deno.Signal.SIGURG); 12 | return true; 13 | } catch (e) { 14 | return ( 15 | e.name === "PermissionDenied" || e.message !== "ESRCH: No such process" 16 | ); 17 | } 18 | } 19 | 20 | /** 21 | * Return `true` if the given PID is running synchronously. 22 | * 23 | * Requires the `--allow-run` flag. 24 | * Requires the `--unstable` flag. 25 | */ 26 | export function isRunningSync(pid: number): boolean { 27 | try { 28 | Deno.kill(pid, Deno.Signal.SIGURG); 29 | return true; 30 | } catch (e) { 31 | return ( 32 | e.name === "PermissionDenied" || e.message !== "ESRCH: No such process" 33 | ); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /ssh.ts: -------------------------------------------------------------------------------- 1 | function hasSSHClient(): boolean { 2 | return Deno.env.get("SSH_CLIENT") != null; 3 | } 4 | 5 | /** 6 | * Return `true` if the process is running inside SSH. 7 | * 8 | * Requires the `--allow-env` flag. 9 | */ 10 | export async function isSSH(): Promise { 11 | return hasSSHClient(); 12 | } 13 | 14 | /** 15 | * Return `true` if the process is running inside SSH synchronously. 16 | * 17 | * Requires the `--allow-env` flag. 18 | */ 19 | export function isSSHSync(): boolean { 20 | return hasSSHClient(); 21 | } 22 | -------------------------------------------------------------------------------- /test/deps.ts: -------------------------------------------------------------------------------- 1 | export { 2 | assert, 3 | assertEquals, 4 | assertNotEquals, 5 | assertThrowsAsync, 6 | assertThrows, 7 | } from "https://deno.land/std@0.63.0/testing/asserts.ts"; 8 | -------------------------------------------------------------------------------- /test/running.test.ts: -------------------------------------------------------------------------------- 1 | import { assert } from "./deps.ts"; 2 | 3 | import { isRunningSync } from "../running.ts"; 4 | 5 | // Deno.test({ 6 | // name: "not running", 7 | // fn(): void { 8 | // assert(!isRunningSync(12345678)); 9 | // }, 10 | // }); 11 | 12 | Deno.test({ 13 | name: "running", 14 | fn(): void { 15 | assert(isRunningSync(Deno.pid)); 16 | }, 17 | }); 18 | -------------------------------------------------------------------------------- /test/wsl.test.ts: -------------------------------------------------------------------------------- 1 | import { assert } from "./deps.ts"; 2 | 3 | import { isWsl, isWslSync } from "../wsl.ts"; 4 | 5 | Deno.test({ 6 | name: "not inside WSL synchronously", 7 | fn(): void { 8 | assert(!isWslSync()); 9 | }, 10 | }); 11 | 12 | Deno.test({ 13 | name: "not inside WSL", 14 | async fn(): Promise { 15 | assert(!await isWsl()); 16 | }, 17 | }); 18 | -------------------------------------------------------------------------------- /util.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Determines whether an object has a property with the specified name. 3 | * Avoid calling prototype builtin `hasOwnProperty` for two reasons: 4 | * 5 | * 1. `hasOwnProperty` is defined on the object as something else: 6 | * 7 | * const options = { 8 | * ending: 'utf8', 9 | * hasOwnProperty: 'foo' 10 | * }; 11 | * options.hasOwnProperty('ending') // throws a TypeError 12 | * 13 | * 2. The object doesn't inherit from `Object.prototype`: 14 | * 15 | * const options = Object.create(null); 16 | * options.ending = 'utf8'; 17 | * options.hasOwnProperty('ending'); // throws a TypeError 18 | * 19 | * @param obj A Object. 20 | * @param v A property name. 21 | * @see https://eslint.org/docs/rules/no-prototype-builtins 22 | * @internal 23 | */ 24 | export function hasOwnProperty(obj: T, v: PropertyKey): boolean { 25 | if (obj == null) { 26 | return false; 27 | } 28 | return Object.prototype.hasOwnProperty.call(obj, v); 29 | } 30 | -------------------------------------------------------------------------------- /wsl.ts: -------------------------------------------------------------------------------- 1 | // see: https://github.com/sindresorhus/is-wsl 2 | 3 | /** 4 | * Return `true` if the process is running inside [Windows Subsystem for Linux][1](Bash on Windows). 5 | * 6 | * [1]: https://msdn.microsoft.com/commandline/wsl/about 7 | * 8 | * Requires the `--allow-read` flag. 9 | */ 10 | export async function isWsl(): Promise { 11 | if (Deno.build.os !== "linux") { 12 | return false; 13 | } 14 | 15 | try { 16 | const decoder = new TextDecoder("utf-8"); 17 | const data = await Deno.readFile("/proc/version"); 18 | const fullversion: string = decoder.decode(data); 19 | return fullversion.toLowerCase().includes("microsoft"); 20 | } catch { 21 | return false; 22 | } 23 | } 24 | 25 | /** 26 | * Return `true` if the process is running inside [Windows Subsystem for Linux][1](Bash on Windows) synchronously. 27 | * 28 | * [1]: https://msdn.microsoft.com/commandline/wsl/about 29 | * 30 | * Requires the `--allow-read` flag. 31 | */ 32 | export function isWslSync(): boolean { 33 | if (Deno.build.os !== "linux") { 34 | return false; 35 | } 36 | 37 | try { 38 | const decoder = new TextDecoder("utf-8"); 39 | const data = Deno.readFileSync("/proc/version"); 40 | const fullversion: string = decoder.decode(data); 41 | return fullversion.toLowerCase().includes("microsoft"); 42 | } catch { 43 | return false; 44 | } 45 | } 46 | --------------------------------------------------------------------------------