generated from Richienb/node-module-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (40 loc) · 1.27 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
"use strict"
const isWindows = require("is-windows")()
const commandExists = require("command-exists")
const execa = require("execa")
const { default: is } = require("@sindresorhus/is")
const { default: ow } = require("ow")
module.exports = async (page) => {
if (!isWindows) throw new Error("Only Windows is supported!")
if (!(await commandExists("chcp"))) throw new Error("chcp doesn't exist!")
ow(page, ow.any(ow.number, ow.undefined))
if (is.number(page)) {
try {
await execa("chcp", [page])
return
} catch (err) {
if (err.stderr === "Invalid code page") throw new TypeError("`page` is not a valid code page.")
throw err
}
} else {
const { stdout } = await execa("chcp")
return Number(stdout.match(/Active code page: (.+)/)[1])
}
}
module.exports.sync = (page) => {
if (!isWindows) throw new Error("Only Windows is supported!")
if (!commandExists.sync("chcp")) throw new Error("chcp doesn't exist!")
ow(page, ow.any(ow.number, ow.undefined))
if (is.number(page)) {
try {
execa.sync("chcp", [page])
return
} catch (err) {
if (err.stderr === "Invalid code page") throw new TypeError("`page` is not a valid code page.")
throw err
}
} else {
const { stdout } = execa.sync("chcp")
return Number(stdout.match(/Active code page: (.+)/)[1])
}
}