@igor.dvlpr/regkeys

๐Ÿ“š An NPM package for fetching Windows registry keys. ๐Ÿ—

Usage no npm install needed!

<script type="module">
  import igorDvlprRegkeys from 'https://cdn.skypack.dev/@igor.dvlpr/regkeys';
</script>

README

RegKeys,

an NPM package for querying Windows registry keys.

Uses the reg.exe system executable.

If you are looking for a cool implementation of this module, click here.


โœจSince version 2.1.0 RegKeys is a hybrid module that supports both CommonJS (legacy) and ES modules, thanks to Modern Module.


โœจ Since v.2.0.0 async methods are available as well.

๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป

API

constructor(key): RegKeys

Creates the RegKeys object. Do not forget to set the key parameter.

  • key: string, the key you later want to manipulate with, i.e. read keys, check for children keys, etc.,

key can either be an expanded or a non-expanded key, i.e.:

Non-expanded Expanded
HKCR HKEY_CLASSES_ROOT
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKU HKEY_USERS
HKCC HKEY_CURRENT_CONFIGURATION

returns the RegKeys object.

Example
const RegKeys = require('@igor.dvlpr/regkeys')

// for your convenience, you can use forward slashes,
// since internally Windows only supports back slashes,
// which need to be escaped and look bad ๐Ÿ˜ซ๐Ÿ˜’

// so...
// ๐Ÿ˜’
const registry = new RegKeys('HKLM\\Software')

// is the same as

// ๐Ÿฅณ๐ŸŽŠ
const registry = new RegKeys('HKLM/Software')

๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป

get(forceRefresh: boolean = false): string[]

Gets the keys for the given root key.

  • forceRefresh: boolean, indicates whether the registry should be queryied again since the result of this method is cached internally, for performance,

returns a string[].

Example
const RegKeys = require('@igor.dvlpr/regkeys')

const registry = new RegKeys('HKCR')
const keys = registry.get()

// do something with the keys,
// it's your fate, unlock it ๐Ÿ˜›
keys.forEach((key) => {
  console.log(key)
})

๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป

hasKey(searchFor: string, caseSensitive: boolean = false): boolean

Checks whether the given key is a direct child of the currently selected key.

  • searchFor: string, the key to search for,
  • caseSensitive: boolean, indicates whether the search should be case-sensitive or not. Defaults to false,

returns a boolean.

NOTE: it will auto-fetch the keys if the internal cache is empty = you didn't call get() before calling this method.

Example
const RegKeys = require('@igor.dvlpr/regkeys')

const registry = new RegKeys('HKLM/Software')

// let's see if we have any
// Microsoft software on our Windows PC
// ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚
console.log(registry.hasKey('Microsoft'))

๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป

hasKeys(searchFor: string[], caseSensitive: boolean = false): boolean

Checks whether the given keys are a direct child of the currently selected key.

  • list: string[], the keys to search for,
  • caseSensitive: boolean, indicates whether the search should be case-sensitive or not. Defaults to false,

returns a boolean[].

NOTE: it will auto-fetch the keys if the internal cache is empty = you didn't call get() before calling this method.

Example
const RegKeys = require('@igor.dvlpr/regkeys')

const registry = new RegKeys('HKLM/Software')

console.log(registry.hasKeys(['Microsoft', 'Macromedia', 'Google', 'Adobe']))

๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป

has(value: string|string[], caseSensitive: boolean = false): boolean|boolean[]

A generic method that checks whether the given key(s) is/are a direct child of the currently selected key. See both hasKey() and hasKeys(). You can use this method for own convenience, it will pick the suited method depending on the type of the value parameter,

  • value: string|string[], the key(s) to search for,
  • caseSensitive: boolean, indicates whether the search should be case-sensitive or not. Defaults to false,

returns a boolean|boolean[].

NOTE: it will auto-fetch the keys if the internal cache is empty = you didn't call get() before calling this method.

Example
const RegKeys = require('@igor.dvlpr/regkeys')

const registry = new RegKeys('HKLM/Software')

console.log(registry.has('Microsoft'))
console.log(registry.has(['Microsoft', 'Macromedia', 'Google', 'Adobe']))

๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป

searchFor(value: string, predicate: SearchPredicate): boolean

Provides a way to do keys-checking using a custom predicate function,

  • value: string, the key name to find,
  • predicate: SearchPredicate, the callback that will do the actual querying, see the code example below,

returns a boolean, i.e. true upon finding the first match or false if no match is found or any of the both required parameters aren't set.

NOTE: it will auto-fetch the keys if the internal cache is empty = you didn't call get() before calling this method.

Example
const RegKeys = require('@igor.dvlpr/regkeys')

const registry = new RegKeys('HKLM/Software')

// useful for custom search algorithms/behavior,
// like demonstrated here, case-insensitive partial search
console.log(
  registry.searchFor('micro', (key, searchFor, i) => {
    return key.toLowerCase().indexOf(searchFor.toLowerCase()) > -1
  })
)

๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป

clear(): void

Clears the cached result, if any,

returns a void.

Example
const RegKeys = require('@igor.dvlpr/regkeys')

const registry = new RegKeys('HKLM/Software')

// fetch keys and cache them
let keys = registry.get()

// ๐Ÿ”ฎ do something with the registry โญ

// clear the cached result
registry.clear()

// refetch (new) keys
keys = registry.get()

console.log(keys)

๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป ๐Ÿ’ป

Don't forget to go through the tests too, they offer additional insight. ๐Ÿ“š