From bc3c4cb0776db12126809fed994ec5b6acd690af Mon Sep 17 00:00:00 2001 From: Michael Quevillon Date: Fri, 17 Sep 2021 10:47:12 -0400 Subject: [PATCH] Add function to detect current shell (#8) --- index.d.ts | 15 +++++++++++++++ index.js | 7 +++++-- index.test-d.ts | 3 ++- readme.md | 6 +++++- test.js | 6 +++++- 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 4b665bd..a826bc5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -17,3 +17,18 @@ console.log(defaultShell); declare const defaultShell: string; export default defaultShell; + +/** +This can be useful if the default shell changes at runtime. + +@returns The user's current default shell. + +@example +``` +import {detectDefaultShell} from 'default-shell'; + +console.log(detectDefaultShell()); +//=> '/bin/bash' +``` +*/ +export function detectDefaultShell(): string; diff --git a/index.js b/index.js index 6147bbb..8c4b63a 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ import process from 'node:process'; import {userInfo} from 'node:os'; -const defaultShell = (() => { +export const detectDefaultShell = () => { const {env} = process; if (process.platform === 'win32') { @@ -20,6 +20,9 @@ const defaultShell = (() => { } return env.SHELL || '/bin/sh'; -})(); +}; + +// Stores default shell when imported. +const defaultShell = detectDefaultShell(); export default defaultShell; diff --git a/index.test-d.ts b/index.test-d.ts index b05b790..70c1284 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,4 +1,5 @@ import {expectType} from 'tsd'; -import defaultShell from './index.js'; +import defaultShell, {detectDefaultShell} from './index.js'; expectType(defaultShell); +expectType(detectDefaultShell()); diff --git a/readme.md b/readme.md index bb17a75..16b4b1d 100644 --- a/readme.md +++ b/readme.md @@ -11,7 +11,7 @@ npm install default-shell ## Usage ```js -import defaultShell from 'default-shell'; +import defaultShell, {detectDefaultShell} from 'default-shell'; // macOS console.log(defaultShell); @@ -20,4 +20,8 @@ console.log(defaultShell); // Windows console.log(defaultShell); //=> 'C:\\WINDOWS\\system32\\cmd.exe' + +// This can be useful if the default shell changes at runtime. +console.log(detectDefaultShell()); +//=> '/bin/bash' ``` diff --git a/test.js b/test.js index 86bd409..6d06ee5 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,11 @@ import process from 'node:process'; import test from 'ava'; -import defaultShell from './index.js'; +import defaultShell, {detectDefaultShell} from './index.js'; test('main', t => { t.true(defaultShell.startsWith(process.platform === 'win32' ? 'C:\\' : '/bin/')); }); + +test('detect', t => { + t.is(detectDefaultShell(), defaultShell); +});