Skip to content

Commit

Permalink
Add function to detect current shell (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mquevill authored Sep 17, 2021
1 parent 4e4bfa3 commit bc3c4cb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
15 changes: 15 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -20,6 +20,9 @@ const defaultShell = (() => {
}

return env.SHELL || '/bin/sh';
})();
};

// Stores default shell when imported.
const defaultShell = detectDefaultShell();

export default defaultShell;
3 changes: 2 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expectType} from 'tsd';
import defaultShell from './index.js';
import defaultShell, {detectDefaultShell} from './index.js';

expectType<string>(defaultShell);
expectType<string>(detectDefaultShell());
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'
```
6 changes: 5 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit bc3c4cb

Please sign in to comment.