Skip to content

Commit d08cec6

Browse files
committed
added methods for unicode
1 parent bbbcdf7 commit d08cec6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Registry.mjs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {HIVES, shortenHive, extendHive} from './constants.mjs'
2-
import {sanitizePath} from './util.mjs'
2+
import {sanitizePath, spawn} from './util.mjs'
33

44

55
// Collection of static methods for interacting with any key in windows registry.
@@ -33,11 +33,39 @@ export class Registry {
3333
return args
3434
}
3535

36+
static async setCodePage(encoding) {
37+
try {
38+
await spawn('cmd.exe', ['/c', 'chcp', encoding])
39+
} catch (e) {
40+
throw new Error(`Invalid code page: ${encoding}`)
41+
}
42+
}
43+
44+
static async enableUnicode() {
45+
if (this.unicode) return
46+
var {stdout} = await spawn('cmd.exe', ['/c', 'chcp'])
47+
var cp = Number(stdout.split(':')[1])
48+
if (Number.isNaN(cp)) throw new Error(`Can't get current code page`)
49+
this.lastCodePage = cp
50+
this.unicode = true
51+
}
52+
53+
static async disableUnicode() {
54+
if (!this.unicode) return
55+
await this.setCodePage(this.lastCodePage)
56+
this.lastCodePage = undefined
57+
this.unicode = false
58+
}
59+
3660
}
3761

3862
Registry.VALUES = '$values'
3963
Registry.DEFAULT = ''
4064

65+
66+
Registry.unicode = false
67+
Registry.lastCodePage
68+
4169
// Only names and paths are affected, not the data
4270
Registry.lowercase = true
4371
// Calls return simplified output format by default.

src/util.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function promiseOnce(eventEmitter, event) {
7171
}
7272

7373
// Promise wrapper for child_process.spawn().
74-
var spawn = async (program, args) => {
74+
export var spawn = async (program, args) => {
7575
var stdio = ['ignore', 'pipe', 'pipe']
7676
var proc = cp.spawn(program, args, {stdio})
7777

0 commit comments

Comments
 (0)