Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 29, 2021
1 parent 05056f1 commit 728ed42
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 117 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
5 changes: 5 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {osLocaleSync} from './index.js';

// TODO: Enable when ESLint supports top-level await.
// console.log(await osLocale());
console.log(osLocaleSync());
28 changes: 28 additions & 0 deletions exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Mini wrapper around `child_process` to make it behave a little like `execa`.

import {promisify} from 'node:util';
import childProcess from 'node:child_process';

const execFile = promisify(childProcess.execFile);

/**
@param {string} command
@param {string[]} arguments_
@returns {Promise<import('child_process').ChildProcess>}
*/
export async function exec(command, arguments_) {
const subprocess = await execFile(command, arguments_, {encoding: 'utf8'});
subprocess.stdout = subprocess.stdout.trim();
return subprocess;
}

/**
@param {string} command
@param {string[]} arguments_
@returns {string}
*/
export function execSync(command, arguments_) {
return childProcess.execFileSync(command, arguments_, {encoding: 'utf8'}).trim();
}
31 changes: 0 additions & 31 deletions execa.js

This file was deleted.

60 changes: 30 additions & 30 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
declare namespace osLocale {
interface Options {
/**
Set to `false` to avoid spawning subprocesses and instead only resolve the locale from environment variables.
@default true
*/
readonly spawn?: boolean;
}
export interface Options {
/**
Set to `false` to avoid spawning subprocesses and instead only resolve the locale from environment variables.
@default true
*/
readonly spawn?: boolean;
}

declare const osLocale: {
/**
Get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)).
/**
Get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)).
@returns The locale.
@returns The locale.
@example
```
import osLocale = require('os-locale');
@example
```
import {osLocale} from 'os-locale';
(async () => {
console.log(await osLocale());
//=> 'en-US'
})();
```
*/
(options?: osLocale.Options): Promise<string>;
console.log(await osLocale());
//=> 'en-US'
```
*/
export function osLocale(options?: Options): Promise<string>;

/**
Synchronously get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)).
/**
Synchronously get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)).
@returns The locale.
*/
sync(options?: osLocale.Options): string;
};
@returns The locale.
@example
```
import {osLocaleSync} from 'os-locale';
export = osLocale;
console.log(osLocaleSync());
//=> 'en-US'
```
*/
export function osLocaleSync(options?: Options): string;
21 changes: 10 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';
const execa = require('./execa.js');
const lcid = require('lcid');
import lcid from 'lcid';
import {exec, execSync} from './exec.js';

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

async function getStdOut(command, args) {
return (await execa(command, args)).stdout;
return (await exec(command, args)).stdout;
}

function getStdOutSync(command, args) {
return execa.sync(command, args).stdout;
return execSync(command, args).stdout;
}

function getEnvLocale(env = process.env) {
Expand Down Expand Up @@ -46,7 +45,7 @@ function getSupportedLocale(locale, locales = '') {
async function getAppleLocale() {
const results = await Promise.all([
getStdOut('defaults', ['read', '-globalDomain', 'AppleLocale']),
getLocales()
getLocales(),
]);

return getSupportedLocale(results[0], results[1]);
Expand All @@ -55,7 +54,7 @@ async function getAppleLocale() {
function getAppleLocaleSync() {
return getSupportedLocale(
getStdOutSync('defaults', ['read', '-globalDomain', 'AppleLocale']),
getLocalesSync()
getLocalesSync(),
);
}

Expand Down Expand Up @@ -87,7 +86,7 @@ function normalise(input) {

const cache = new Map();

module.exports = async (options = defaultOptions) => {
export async function osLocale(options = defaultOptions) {
if (cache.has(options.spawn)) {
return cache.get(options.spawn);
}
Expand All @@ -111,9 +110,9 @@ module.exports = async (options = defaultOptions) => {
const normalised = normalise(locale || defaultLocale);
cache.set(options.spawn, normalised);
return normalised;
};
}

module.exports.sync = (options = defaultOptions) => {
export function osLocaleSync(options = defaultOptions) {
if (cache.has(options.spawn)) {
return cache.get(options.spawn);
}
Expand All @@ -136,4 +135,4 @@ module.exports.sync = (options = defaultOptions) => {
const normalised = normalise(locale || defaultLocale);
cache.set(options.spawn, normalised);
return normalised;
};
}
6 changes: 3 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {expectType} from 'tsd';
import osLocale = require('.');
import {osLocale, osLocaleSync} from './index.js';

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

expectType<string>(osLocale.sync());
expectType<string>(osLocale.sync({spawn: false}));
expectType<string>(osLocaleSync());
expectType<string>(osLocaleSync({spawn: false}));
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,39 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava && tsd"
"//test": "xo && ava && tsd",
"test": "xo && tsd"
},
"files": [
"index.js",
"index.d.ts"
"index.d.ts",
"exec.js"
],
"keywords": [
"locale",
"lang",
"language",
"system",
"os",
"string",
"str",
"user",
"country",
"id",
"identifier",
"region"
],
"dependencies": {
"execa": "^4.0.0",
"lcid": "^3.0.0"
"lcid": "^3.1.1"
},
"devDependencies": {
"ava": "^2.1.0",
"ava": "^3.15.0",
"proxyquire": "^2.1.3",
"tsd": "^0.11.0",
"xo": "^0.38.2"
"tsd": "^0.17.0",
"xo": "^0.42.0"
}
}
8 changes: 3 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ $ npm install os-locale
## Usage

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

(async () => {
console.log(await osLocale());
//=> 'en-US'
})();
console.log(await osLocale());
//=> 'en-US'
```
## API

Expand Down
Loading

0 comments on commit 728ed42

Please sign in to comment.