Skip to content

Commit bbbcdf7

Browse files
committed
simplifying
1 parent fc406dc commit bbbcdf7

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

src/Registry.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,12 @@ export class Registry {
3737

3838
Registry.VALUES = '$values'
3939
Registry.DEFAULT = ''
40-
Registry.DEFAULT_VERBOSE = null
41-
Registry.VALUENOTSET_VERBOSE = null
4240

4341
// Only names and paths are affected, not the data
4442
Registry.lowercase = true
4543
// Calls return simplified output format by default.
4644
// Can be 'simple', 'complex', (TODO) 'naive'
4745
Registry.format = 'simple'
4846

49-
Registry.FORMAT_NAIVE = 'naive'
5047
Registry.FORMAT_SIMPLE = 'simple'
5148
Registry.FORMAT_COMPLEX = 'complex'

src/get.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Registry.hasValue = async function(path, name = Registry.DEFAULT) {
189189
if (result === undefined) return false
190190
// Default value name is represented by a word default in brackets.
191191
if (name === Registry.DEFAULT)
192-
name = Registry.DEFAULT_VERBOSE
192+
name = VALUE_DEFAULT
193193
// Split the result into lines and try to find the searched one.
194194
// NOTE: not needed to lowercase the result, REG always returns queried value name in the same caps.
195195
return result
@@ -278,9 +278,9 @@ function processValueLine(line, options) {
278278
// value line starts with 4 spaces and consists of three items that are also spaces by 4 spaces.
279279
// WARNING: Do not trim. Lines only start with 4 spaces, they don't end with 4 spaces unsless the value is empty string.
280280
var [name, type, data] = line.slice(4).split(' ')
281-
if (name === Registry.DEFAULT_VERBOSE)
281+
if (name === VALUE_DEFAULT)
282282
name = Registry.DEFAULT
283-
if (data === Registry.VALUENOTSET_VERBOSE)
283+
if (data === VALUE_NOT_SET)
284284
data = undefined
285285
else if (options.lowercase)
286286
name = name.toLowerCase()

src/util.mjs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
import {Registry} from './Registry.mjs'
2-
import cp, { exec } from 'child_process'
2+
import cp from 'child_process'
33
import {SZ, MULTI_SZ, EXPAND_SZ, DWORD, QWORD, BINARY, NONE} from './constants.mjs'
44

55

66
let ERR_NOT_FOUND
7+
export let VALUE_DEFAULT = undefined
8+
export let VALUE_NOT_SET = undefined
9+
710
let errMessagePromise
811
let defaultValuesPromise
912

1013
function getErrorLine(stderr) {
1114
return stderr.trim().split('\r\n')[0]
1215
}
1316

14-
function getDefaultValues(stdout) {
17+
function setDefaultValues(stdout) {
1518
// indexOf() because it's fastest.
1619
let iNextStr = stdout.indexOf('\r\n', 1)
1720
let iNameBracketOpen = stdout.indexOf('(', iNextStr)
1821
let iNameBracketClose = stdout.indexOf(')', iNameBracketOpen)
1922
let iValBracketOpen = stdout.indexOf('(', iNameBracketClose)
2023
let iValBracketClose = stdout.indexOf(')', iValBracketOpen)
21-
let defaultNameStr = stdout.slice(iNameBracketOpen, iNameBracketClose+1)
22-
let valueNotSetStr = stdout.slice(iValBracketOpen, iValBracketClose+1)
23-
return [defaultNameStr, valueNotSetStr]
24+
VALUE_DEFAULT = stdout.slice(iNameBracketOpen, iNameBracketClose+1)
25+
VALUE_NOT_SET = stdout.slice(iValBracketOpen, iValBracketClose+1)
2426
}
2527

2628
// Method for calling the reg.exe commands.
@@ -32,21 +34,19 @@ execute = async args => {
3234
// Ensure we get localized messages only once.
3335

3436
// ERR_NOT_FOUND message.
35-
if (!errMessagePromise)
36-
errMessagePromise = spawnProcess(['QUERY', 'HKLM\\NONEXISTENT'])
37-
.then(res => getErrorLine(res.stderr))
37+
if (!errMessagePromise) {
38+
errMessagePromise = spawn('reg.exe', ['QUERY', 'HKLM\\NONEXISTENT'])
39+
.then(res => ERR_NOT_FOUND = getErrorLine(res.stderr))
40+
}
3841

3942
// (Default) and (value not set) values.
40-
if (!defaultValuesPromise)
41-
defaultValuesPromise = spawnProcess(['QUERY', 'HKCR', '/ve'])
42-
.then(res => getDefaultValues(res.stdout))
43+
if (!defaultValuesPromise) {
44+
defaultValuesPromise = spawn('reg.exe', ['QUERY', 'HKCR', '/ve'])
45+
.then(res => setDefaultValues(res.stdout))
46+
}
4347

4448
// Postpone all execute() calls until the localized messages are resolved.
45-
let [notFoundMsg, [defaultNameStr, valueNotSetStr]] = await Promise
46-
.all([errMessagePromise, defaultValuesPromise])
47-
ERR_NOT_FOUND = notFoundMsg
48-
Registry.DEFAULT_VERBOSE = defaultNameStr
49-
Registry.VALUENOTSET_VERBOSE = valueNotSetStr
49+
await Promise.all([errMessagePromise, defaultValuesPromise])
5050

5151
// Replace this temporary function with actual execute().
5252
execute = _execute
@@ -56,7 +56,7 @@ execute = async args => {
5656

5757
// Actual execute() function.
5858
var _execute = async args => {
59-
var {stdout, stderr} = await spawnProcess(args)
59+
var {stdout, stderr} = await spawn('reg.exe', args)
6060
// REG command has finished running, resolve result or throw error if any occured.
6161
if (stderr.length === 0) return stdout
6262
var line = getErrorLine(stderr)
@@ -71,9 +71,9 @@ function promiseOnce(eventEmitter, event) {
7171
}
7272

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

7878
var stdout = ''
7979
var stderr = ''
@@ -93,11 +93,11 @@ var spawnProcess = async args => {
9393
return {stdout, stderr}
9494
}
9595

96-
// Replaces default spawnProcess() with custom means of spawning reg.exe.
96+
// Replaces default spawn('reg.exe', ) with custom means of spawning reg.exe.
9797
// For example allows to run the library in restricted environments.
98-
// Default spawnProcess() uses Node's child_process.spawn().
99-
export function _replaceProcessSpawner(externalHook) {
100-
spawnProcess = externalHook
98+
// Default spawn('reg.exe', ) uses Node's child_process.spawn().
99+
export function _replaceSpawn(externalHook) {
100+
spawn = externalHook
101101
}
102102

103103
class RegError extends Error {

0 commit comments

Comments
 (0)