From efbe2cee7672dfa5cb4d2ef44e506065ebbad1c6 Mon Sep 17 00:00:00 2001 From: Damien Guard Date: Tue, 27 Dec 2016 16:24:06 -0800 Subject: [PATCH] Convert win-shell.coffee to win-shell.js --- src/main-process/win-shell.coffee | 63 ------------------------- src/main-process/win-shell.js | 77 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 63 deletions(-) delete mode 100644 src/main-process/win-shell.coffee create mode 100644 src/main-process/win-shell.js diff --git a/src/main-process/win-shell.coffee b/src/main-process/win-shell.coffee deleted file mode 100644 index baf02a9fc..000000000 --- a/src/main-process/win-shell.coffee +++ /dev/null @@ -1,63 +0,0 @@ -Registry = require 'winreg' -Path = require 'path' - -exeName = Path.basename(process.execPath) -appPath = "\"#{process.execPath}\"" -fileIconPath = "\"#{Path.join(process.execPath, '..', 'resources', 'cli', 'file.ico')}\"" -isBeta = appPath.includes(' Beta') -appName = exeName.replace('atom', (if isBeta then 'Atom Beta' else 'Atom' )).replace('.exe', '') - -class ShellOption - constructor: (key, parts) -> - @key = key - @parts = parts - - isRegistered: (callback) => - new Registry({hive: 'HKCU', key: "#{@key}\\#{@parts[0].key}"}) - .get @parts[0].name, (err, val) => - callback(not err? and val? and val.value is @parts[0].value) - - register: (callback) => - doneCount = @parts.length - @parts.forEach (part) => - reg = new Registry({hive: 'HKCU', key: if part.key? then "#{@key}\\#{part.key}" else @key}) - reg.create( -> reg.set part.name, Registry.REG_SZ, part.value, -> callback() if --doneCount is 0) - - deregister: (callback) => - @isRegistered (isRegistered) => - if isRegistered - new Registry({hive: 'HKCU', key: @key}).destroy -> callback null, true - else - callback null, false - - update: (callback) => - new Registry({hive: 'HKCU', key: "#{@key}\\#{@parts[0].key}"}) - .get @parts[0].name, (err, val) => - if err? or not val? - callback(err) - else - @register callback - -exports.appName = appName - -exports.fileHandler = new ShellOption("\\Software\\Classes\\Applications\\#{exeName}", - [ - {key: 'shell\\open\\command', name: '', value: "#{appPath} \"%1\""}, - {key: 'shell\\open', name: 'FriendlyAppName', value: "#{appName}"}, - {key: 'DefaultIcon', name: '', value: "#{fileIconPath}"} - ] -) - -contextParts = [ - {key: 'command', name: '', value: "#{appPath} \"%1\""}, - {name: '', value: "Open with #{appName}"}, - {name: 'Icon', value: "#{appPath}"} -] - -exports.fileContextMenu = new ShellOption("\\Software\\Classes\\*\\shell\\#{appName}", contextParts) - -exports.folderContextMenu = new ShellOption("\\Software\\Classes\\Directory\\shell\\#{appName}", contextParts) - -exports.folderBackgroundContextMenu = new ShellOption("\\Software\\Classes\\Directory\\background\\shell\\#{appName}", - JSON.parse(JSON.stringify(contextParts).replace('%1', '%V')) -) diff --git a/src/main-process/win-shell.js b/src/main-process/win-shell.js new file mode 100644 index 000000000..9670936c7 --- /dev/null +++ b/src/main-process/win-shell.js @@ -0,0 +1,77 @@ +'use babel' + +import Registry from 'winreg' +import Path from 'path' + +let exeName = Path.basename(process.execPath) +let appPath = `\"${process.execPath}\"` +let fileIconPath = `\"${Path.join(process.execPath, '..', 'resources', 'cli', 'file.ico')}\"` +let isBeta = appPath.includes(' Beta') +let appName = exeName.replace('atom', isBeta ? 'Atom Beta' : 'Atom').replace('.exe', '') + +class ShellOption { + constructor (key, parts) { + this.isRegistered = this.isRegistered.bind(this) + this.register = this.register.bind(this) + this.deregister = this.deregister.bind(this) + this.update = this.update.bind(this) + this.key = key + this.parts = parts + } + + isRegistered (callback) { + new Registry({hive: 'HKCU', key: `${this.key}\\${this.parts[0].key}`}) + .get(this.parts[0].name, (err, val) => callback((err == null) && (val != null) && val.value === this.parts[0].value)) + } + + register (callback) { + let doneCount = this.parts.length + this.parts.forEach(part => { + let reg = new Registry({hive: 'HKCU', key: (part.key != null) ? `${this.key}\\${part.key}` : this.key}) + return reg.create(() => reg.set(part.name, Registry.REG_SZ, part.value, () => { if (--doneCount === 0) return callback() })) + }) + } + + deregister (callback) { + this.isRegistered(isRegistered => { + if (isRegistered) { + new Registry({hive: 'HKCU', key: this.key}).destroy(() => callback(null, true)) + } else { + callback(null, false) + } + }) + } + + update (callback) { + new Registry({hive: 'HKCU', key: `${this.key}\\${this.parts[0].key}`}) + .get(this.parts[0].name, (err, val) => { + if ((err != null) || (val == null)) { + callback(err) + } else { + this.register(callback) + } + }) + } +} + +exports.appName = appName + +exports.fileHandler = new ShellOption(`\\Software\\Classes\\Applications\\${exeName}`, + [ + {key: 'shell\\open\\command', name: '', value: `${appPath} \"%1\"`}, + {key: 'shell\\open', name: 'FriendlyAppName', value: `${appName}`}, + {key: 'DefaultIcon', name: '', value: `${fileIconPath}`} + ] +) + +let contextParts = [ + {key: 'command', name: '', value: `${appPath} \"%1\"`}, + {name: '', value: `Open with ${appName}`}, + {name: 'Icon', value: `${appPath}`} +] + +exports.fileContextMenu = new ShellOption(`\\Software\\Classes\\*\\shell\\${appName}`, contextParts) +exports.folderContextMenu = new ShellOption(`\\Software\\Classes\\Directory\\shell\\${appName}`, contextParts) +exports.folderBackgroundContextMenu = new ShellOption(`\\Software\\Classes\\Directory\\background\\shell\\${appName}`, + JSON.parse(JSON.stringify(contextParts).replace('%1', '%V')) +)