Skip to content

Commit

Permalink
Require Node.js 16 and move to ESM
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
sindresorhus committed Aug 8, 2023
1 parent 8b20f12 commit a3d3762
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 69 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 20
- 18
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Check if the process is running inside [Windows Subsystem for Linux](https://msd
@example
```
import isWsl = require('is-wsl');
import isWsl from 'is-wsl';
// When running inside Windows Subsystem for Linux
console.log(isWsl);
Expand All @@ -12,4 +12,4 @@ console.log(isWsl);
*/
declare const isWsl: boolean;

export = isWsl;
export default isWsl;
20 changes: 8 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const os = require('os');
const fs = require('fs');
const isDocker = require('is-docker');
import process from 'node:process';
import os from 'node:os';
import fs from 'node:fs';
import isDocker from 'is-docker';

const isWsl = () => {
if (process.platform !== 'linux') {
Expand All @@ -17,15 +17,11 @@ const isWsl = () => {
}

try {
return fs.readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft') ?
!isDocker() : false;
} catch (_) {
return fs.readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft')
? !isDocker() : false;
} catch {
return false;
}
};

if (process.env.__IS_WSL_TEST__) {
module.exports = isWsl;
} else {
module.exports = isWsl();
}
export default process.env.__IS_WSL_TEST__ ? isWsl : isWsl();
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import isWsl = require('.');
import isWsl from './index.js';

expectType<boolean>(isWsl);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
25 changes: 18 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": ">=8"
"node": ">=16"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -34,13 +39,19 @@
"is"
],
"dependencies": {
"is-docker": "^2.0.0"
"is-docker": "^3.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"clear-module": "^3.2.0",
"proxyquire": "^2.1.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^5.3.1",
"esmock": "^2.3.6",
"tsd": "^0.28.1",
"xo": "^0.55.1"
},
"ava": {
"serial": true,
"nodeArguments": [
"--loader=esmock",
"--no-warnings"
]
}
}
18 changes: 3 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,16 @@ Can be useful if you need to work around unimplemented or buggy features in WSL.

## Install

```
$ npm install is-wsl
```sh
npm install is-wsl
```

## Usage

```js
const isWsl = require('is-wsl');
import isWsl from 'is-wsl';

// When running inside Windows Subsystem for Linux
console.log(isWsl);
//=> true
```

---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-is-wsl?utm_source=npm-is-wsl&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
49 changes: 24 additions & 25 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import process from 'node:process';
import test from 'ava';
import proxyquire from 'proxyquire';
import clearModule from 'clear-module';
import esmock from 'esmock';

test.beforeEach(() => {
clearModule('.');
});
const requireFresh = modulePath => import(`${modulePath}?cacheBust=${Date.now()}`);

test('inside WSL 1', t => {
test('inside WSL 1', async t => {
process.env.__IS_WSL_TEST__ = true;

const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {value: 'linux'});

const isWsl = proxyquire('.', {
const isWsl = await esmock('./index.js', {
fs: {
readFileSync: () => 'Linux version 3.4.0-Microsoft (Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014'
}
readFileSync: () => 'Linux version 3.4.0-Microsoft (Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014',
},
});

t.true(isWsl());
Expand All @@ -24,16 +22,16 @@ test('inside WSL 1', t => {
Object.defineProperty(process, 'platform', {value: originalPlatform});
});

test('inside WSL 2', t => {
test('inside WSL 2', async t => {
process.env.__IS_WSL_TEST__ = true;

const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {value: 'linux'});

const isWsl = proxyquire('.', {
const isWsl = await esmock('./index.js', {
fs: {
readFileSync: () => 'Linux version 4.19.43-microsoft-standard (oe-user@oe-host) (gcc version 7.3.0 (GCC)) #1 SMP Mon May 20 19:35:22 UTC 2019'
}
readFileSync: () => 'Linux version 4.19.43-microsoft-standard (oe-user@oe-host) (gcc version 7.3.0 (GCC)) #1 SMP Mon May 20 19:35:22 UTC 2019',
},
});

t.true(isWsl());
Expand All @@ -42,32 +40,33 @@ test('inside WSL 2', t => {
Object.defineProperty(process, 'platform', {value: originalPlatform});
});

test('not inside WSL', t => {
test('not inside WSL', async t => {
process.env.__IS_WSL_TEST__ = true;

const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {value: 'darwin'});

const isWsl = require('.');
const {default: isWsl} = await requireFresh('./index.js');

t.false(isWsl());

delete process.env.__IS_WSL_TEST__;
Object.defineProperty(process, 'platform', {value: originalPlatform});
});

test('not inside WSL, but inside Linux', t => {
test('not inside WSL, but inside Linux', async t => {
process.env.__IS_WSL_TEST__ = true;

const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {value: 'linux'});

const isWsl = proxyquire('.', {
const isWsl = await esmock('./index.js', {
fs: {
readFileSync: () => 'Linux version 4.19.43-standard (oe-user@oe-host) (gcc version 7.3.0 (GCC)) #1 SMP Mon May 20 19:35:22 UTC 2019'
readFileSync: () => 'Linux version 4.19.43-standard (oe-user@oe-host) (gcc version 7.3.0 (GCC)) #1 SMP Mon May 20 19:35:22 UTC 2019',
},
os: {
release: () => ''
}
release: () => '',
},
});

t.false(isWsl());
Expand All @@ -76,20 +75,20 @@ test('not inside WSL, but inside Linux', t => {
Object.defineProperty(process, 'platform', {value: originalPlatform});
});

test('inside WSL, but inside docker', t => {
test('inside WSL, but inside docker', async t => {
process.env.__IS_WSL_TEST__ = true;

const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {value: 'linux'});

const isWsl = proxyquire('.', {
const isWsl = await esmock('./index.js', {
fs: {
readFileSync: () => 'Linux version 4.19.43-microsoft-standard (oe-user@oe-host) (gcc version 7.3.0 (GCC)) #1 SMP Mon May 20 19:35:22 UTC 2019'
readFileSync: () => 'Linux version 4.19.43-microsoft-standard (oe-user@oe-host) (gcc version 7.3.0 (GCC)) #1 SMP Mon May 20 19:35:22 UTC 2019',
},
'is-docker': () => true,
os: {
release: () => 'microsoft'
}
release: () => 'microsoft',
},
});

t.false(isWsl());
Expand Down

0 comments on commit a3d3762

Please sign in to comment.