-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathindex.js
138 lines (108 loc) · 3.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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 exec(command, args)).stdout;
}
function getStdOutSync(command, args) {
return execSync(command, args);
}
function getEnvLocale(env = process.env) {
return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
}
function parseLocale(string) {
const env = {};
for (const definition of string.split('\n')) {
const [key, value] = definition.split('=');
env[key] = value.replace(/^"|"$/g, '');
}
return getEnvLocale(env);
}
function getLocale(string) {
return (string && string.replace(/[.:].*/, ''));
}
async function getLocales() {
return getStdOut('locale', ['-a']);
}
function getLocalesSync() {
return getStdOutSync('locale', ['-a']);
}
function getSupportedLocale(locale, locales = '') {
return locales.includes(locale) ? locale : defaultLocale;
}
async function getAppleLocale() {
const results = await Promise.all([
getStdOut('defaults', ['read', '-globalDomain', 'AppleLocale']),
getLocales(),
]);
return getSupportedLocale(results[0], results[1]);
}
function getAppleLocaleSync() {
return getSupportedLocale(
getStdOutSync('defaults', ['read', '-globalDomain', 'AppleLocale']),
getLocalesSync(),
);
}
async function getUnixLocale() {
return getLocale(parseLocale(await getStdOut('locale')));
}
function getUnixLocaleSync() {
return getLocale(parseLocale(getStdOutSync('locale')));
}
async function getWinLocale() {
const stdout = await getStdOut('wmic', ['os', 'get', 'locale']);
const lcidCode = Number.parseInt(stdout.replace('Locale', ''), 16);
return lcid.from(lcidCode);
}
function getWinLocaleSync() {
const stdout = getStdOutSync('wmic', ['os', 'get', 'locale']);
const lcidCode = Number.parseInt(stdout.replace('Locale', ''), 16);
return lcid.from(lcidCode);
}
function normalise(input) {
return input.replace(/_/, '-');
}
const cache = new Map();
export async function osLocale(options = defaultOptions) {
if (cache.has(options.spawn)) {
return cache.get(options.spawn);
}
let locale;
try {
const envLocale = getEnvLocale();
if (envLocale || options.spawn === false) {
locale = getLocale(envLocale);
} else if (process.platform === 'win32') {
locale = await getWinLocale();
} else if (process.platform === 'darwin') {
locale = await getAppleLocale();
} else {
locale = await getUnixLocale();
}
} catch {}
const normalised = normalise(locale || defaultLocale);
cache.set(options.spawn, normalised);
return normalised;
}
export function osLocaleSync(options = defaultOptions) {
if (cache.has(options.spawn)) {
return cache.get(options.spawn);
}
let locale;
try {
const envLocale = getEnvLocale();
if (envLocale || options.spawn === false) {
locale = getLocale(envLocale);
} else if (process.platform === 'win32') {
locale = getWinLocaleSync();
} else if (process.platform === 'darwin') {
locale = getAppleLocaleSync();
} else {
locale = getUnixLocaleSync();
}
} catch {}
const normalised = normalise(locale || defaultLocale);
cache.set(options.spawn, normalised);
return normalised;
}