1'use strict' 2 3const log = require('npmlog') 4const execFile = require('child_process').execFile 5const path = require('path') 6 7function logWithPrefix (log, prefix) { 8 function setPrefix (logFunction) { 9 return (...args) => logFunction.apply(null, [ prefix, ...args ]) // eslint-disable-line 10 } 11 return { 12 silly: setPrefix(log.silly), 13 verbose: setPrefix(log.verbose), 14 info: setPrefix(log.info), 15 warn: setPrefix(log.warn), 16 error: setPrefix(log.error) 17 } 18} 19 20function regGetValue (key, value, addOpts, cb) { 21 const outReValue = value.replace(/\W/g, '.') 22 const outRe = new RegExp(`^\\s+${outReValue}\\s+REG_\\w+\\s+(\\S.*)$`, 'im') 23 const reg = path.join(process.env.SystemRoot, 'System32', 'reg.exe') 24 const regArgs = ['query', key, '/v', value].concat(addOpts) 25 26 log.silly('reg', 'running', reg, regArgs) 27 const child = execFile(reg, regArgs, { encoding: 'utf8' }, 28 function (err, stdout, stderr) { 29 log.silly('reg', 'reg.exe stdout = %j', stdout) 30 if (err || stderr.trim() !== '') { 31 log.silly('reg', 'reg.exe err = %j', err && (err.stack || err)) 32 log.silly('reg', 'reg.exe stderr = %j', stderr) 33 return cb(err, stderr) 34 } 35 36 const result = outRe.exec(stdout) 37 if (!result) { 38 log.silly('reg', 'error parsing stdout') 39 return cb(new Error('Could not parse output of reg.exe')) 40 } 41 log.silly('reg', 'found: %j', result[1]) 42 cb(null, result[1]) 43 }) 44 child.stdin.end() 45} 46 47function regSearchKeys (keys, value, addOpts, cb) { 48 var i = 0 49 const search = () => { 50 log.silly('reg-search', 'looking for %j in %j', value, keys[i]) 51 regGetValue(keys[i], value, addOpts, (err, res) => { 52 ++i 53 if (err && i < keys.length) { return search() } 54 cb(err, res) 55 }) 56 } 57 search() 58} 59 60module.exports = { 61 logWithPrefix: logWithPrefix, 62 regGetValue: regGetValue, 63 regSearchKeys: regSearchKeys 64} 65