Skip to content

Commit 728ed42

Browse files
committed
Require Node.js 12.20 and move to ESM
1 parent 05056f1 commit 728ed42

File tree

10 files changed

+114
-117
lines changed

10 files changed

+114
-117
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
13+
- 16
1614
steps:
1715
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
1917
with:
2018
node-version: ${{ matrix.node-version }}
2119
- run: npm install

example.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {osLocaleSync} from './index.js';
2+
3+
// TODO: Enable when ESLint supports top-level await.
4+
// console.log(await osLocale());
5+
console.log(osLocaleSync());

exec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Mini wrapper around `child_process` to make it behave a little like `execa`.
2+
3+
import {promisify} from 'node:util';
4+
import childProcess from 'node:child_process';
5+
6+
const execFile = promisify(childProcess.execFile);
7+
8+
/**
9+
@param {string} command
10+
@param {string[]} arguments_
11+
12+
@returns {Promise<import('child_process').ChildProcess>}
13+
*/
14+
export async function exec(command, arguments_) {
15+
const subprocess = await execFile(command, arguments_, {encoding: 'utf8'});
16+
subprocess.stdout = subprocess.stdout.trim();
17+
return subprocess;
18+
}
19+
20+
/**
21+
@param {string} command
22+
@param {string[]} arguments_
23+
24+
@returns {string}
25+
*/
26+
export function execSync(command, arguments_) {
27+
return childProcess.execFileSync(command, arguments_, {encoding: 'utf8'}).trim();
28+
}

execa.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

index.d.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
declare namespace osLocale {
2-
interface Options {
3-
/**
4-
Set to `false` to avoid spawning subprocesses and instead only resolve the locale from environment variables.
5-
6-
@default true
7-
*/
8-
readonly spawn?: boolean;
9-
}
1+
export interface Options {
2+
/**
3+
Set to `false` to avoid spawning subprocesses and instead only resolve the locale from environment variables.
4+
5+
@default true
6+
*/
7+
readonly spawn?: boolean;
108
}
119

12-
declare const osLocale: {
13-
/**
14-
Get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)).
10+
/**
11+
Get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)).
1512
16-
@returns The locale.
13+
@returns The locale.
1714
18-
@example
19-
```
20-
import osLocale = require('os-locale');
15+
@example
16+
```
17+
import {osLocale} from 'os-locale';
2118
22-
(async () => {
23-
console.log(await osLocale());
24-
//=> 'en-US'
25-
})();
26-
```
27-
*/
28-
(options?: osLocale.Options): Promise<string>;
19+
console.log(await osLocale());
20+
//=> 'en-US'
21+
```
22+
*/
23+
export function osLocale(options?: Options): Promise<string>;
2924

30-
/**
31-
Synchronously get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)).
25+
/**
26+
Synchronously get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)).
3227
33-
@returns The locale.
34-
*/
35-
sync(options?: osLocale.Options): string;
36-
};
28+
@returns The locale.
29+
30+
@example
31+
```
32+
import {osLocaleSync} from 'os-locale';
3733
38-
export = osLocale;
34+
console.log(osLocaleSync());
35+
//=> 'en-US'
36+
```
37+
*/
38+
export function osLocaleSync(options?: Options): string;

index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
'use strict';
2-
const execa = require('./execa.js');
3-
const lcid = require('lcid');
1+
import lcid from 'lcid';
2+
import {exec, execSync} from './exec.js';
43

54
const defaultOptions = {spawn: true};
65
const defaultLocale = 'en-US';
76

87
async function getStdOut(command, args) {
9-
return (await execa(command, args)).stdout;
8+
return (await exec(command, args)).stdout;
109
}
1110

1211
function getStdOutSync(command, args) {
13-
return execa.sync(command, args).stdout;
12+
return execSync(command, args).stdout;
1413
}
1514

1615
function getEnvLocale(env = process.env) {
@@ -46,7 +45,7 @@ function getSupportedLocale(locale, locales = '') {
4645
async function getAppleLocale() {
4746
const results = await Promise.all([
4847
getStdOut('defaults', ['read', '-globalDomain', 'AppleLocale']),
49-
getLocales()
48+
getLocales(),
5049
]);
5150

5251
return getSupportedLocale(results[0], results[1]);
@@ -55,7 +54,7 @@ async function getAppleLocale() {
5554
function getAppleLocaleSync() {
5655
return getSupportedLocale(
5756
getStdOutSync('defaults', ['read', '-globalDomain', 'AppleLocale']),
58-
getLocalesSync()
57+
getLocalesSync(),
5958
);
6059
}
6160

@@ -87,7 +86,7 @@ function normalise(input) {
8786

8887
const cache = new Map();
8988

90-
module.exports = async (options = defaultOptions) => {
89+
export async function osLocale(options = defaultOptions) {
9190
if (cache.has(options.spawn)) {
9291
return cache.get(options.spawn);
9392
}
@@ -111,9 +110,9 @@ module.exports = async (options = defaultOptions) => {
111110
const normalised = normalise(locale || defaultLocale);
112111
cache.set(options.spawn, normalised);
113112
return normalised;
114-
};
113+
}
115114

116-
module.exports.sync = (options = defaultOptions) => {
115+
export function osLocaleSync(options = defaultOptions) {
117116
if (cache.has(options.spawn)) {
118117
return cache.get(options.spawn);
119118
}
@@ -136,4 +135,4 @@ module.exports.sync = (options = defaultOptions) => {
136135
const normalised = normalise(locale || defaultLocale);
137136
cache.set(options.spawn, normalised);
138137
return normalised;
139-
};
138+
}

index.test-d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {expectType} from 'tsd';
2-
import osLocale = require('.');
2+
import {osLocale, osLocaleSync} from './index.js';
33

44
expectType<Promise<string>>(osLocale());
55
expectType<Promise<string>>(osLocale({spawn: false}));
66

7-
expectType<string>(osLocale.sync());
8-
expectType<string>(osLocale.sync({spawn: false}));
7+
expectType<string>(osLocaleSync());
8+
expectType<string>(osLocaleSync({spawn: false}));

package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,39 @@
1010
"email": "[email protected]",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=10"
16+
"node": ">=12.20"
1517
},
1618
"scripts": {
17-
"test": "xo && ava && tsd"
19+
"//test": "xo && ava && tsd",
20+
"test": "xo && tsd"
1821
},
1922
"files": [
2023
"index.js",
21-
"index.d.ts"
24+
"index.d.ts",
25+
"exec.js"
2226
],
2327
"keywords": [
2428
"locale",
25-
"lang",
2629
"language",
2730
"system",
2831
"os",
2932
"string",
30-
"str",
3133
"user",
3234
"country",
3335
"id",
3436
"identifier",
3537
"region"
3638
],
3739
"dependencies": {
38-
"execa": "^4.0.0",
39-
"lcid": "^3.0.0"
40+
"lcid": "^3.1.1"
4041
},
4142
"devDependencies": {
42-
"ava": "^2.1.0",
43+
"ava": "^3.15.0",
4344
"proxyquire": "^2.1.3",
44-
"tsd": "^0.11.0",
45-
"xo": "^0.38.2"
45+
"tsd": "^0.17.0",
46+
"xo": "^0.42.0"
4647
}
4748
}

readme.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ $ npm install os-locale
1515
## Usage
1616

1717
```js
18-
const osLocale = require('os-locale');
18+
import {osLocale} from 'os-locale';
1919

20-
(async () => {
21-
console.log(await osLocale());
22-
//=> 'en-US'
23-
})();
20+
console.log(await osLocale());
21+
//=> 'en-US'
2422
```
2523
## API
2624

0 commit comments

Comments
 (0)