From 5efb4fe1e0f8dcd8dbb4ec77d044a8ecd722ea8e Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Tue, 5 Jun 2018 10:45:06 +0200 Subject: [PATCH] chore(modeler): drop in-app file association behavior Associating files is now provided by installer. Related to #787 --- app/lib/platform/windows/file-associations.js | 92 ------------------- app/lib/platform/windows/index.js | 79 +--------------- .../windows/file-associations-spec.js | 50 ---------- 3 files changed, 1 insertion(+), 220 deletions(-) delete mode 100644 app/lib/platform/windows/file-associations.js delete mode 100644 app/test/spec/platform/windows/file-associations-spec.js diff --git a/app/lib/platform/windows/file-associations.js b/app/lib/platform/windows/file-associations.js deleted file mode 100644 index 3f39a1831d..0000000000 --- a/app/lib/platform/windows/file-associations.js +++ /dev/null @@ -1,92 +0,0 @@ -'use strict'; - -var execSync = require('child_process').execSync; - -var { - forEach, - uniqueBy -} = require('min-dash'); - - -var EXTENSIONS = [ 'bpmn', 'dmn', 'cmmn' ]; - -var REG_KEY_NOT_FOUND_PATTERN_EN = /The system was unable to find the specified registry key or value/; -var REG_KEY_NOT_FOUND_PATTERN_DE = /Wert wurde nicht gefunden/; - -var REG_COMMAND_RESULT_PATTERN = /.*REG_SZ\s+"(.*)" -- "%1"$/m; - -function exec(cmd) { - var result = execSync(cmd, { encoding: 'utf-8', stdio: [ 'pipe', 'pipe', 'pipe' ] }); - - console.log('[file associations] exec\n> %s\n%s', cmd, result); - - return result; -} - -function createKey(extension) { - var regBaseKey = 'HKEY_CURRENT_USER\\Software\\Classes\\.' + extension, - regIconKey = regBaseKey + '\\DefaultIcon', - regCommandKey = regBaseKey + '\\shell\\open\\command'; - - return { - base: regBaseKey, - icon: regIconKey, - command: regCommandKey - }; -} - -function ignoreNotFound(fn) { - try { - return fn(); - } catch (err) { - if (REG_KEY_NOT_FOUND_PATTERN_EN.test(err.message) || - REG_KEY_NOT_FOUND_PATTERN_DE.test(err.message)) { - return null; - } else { - throw err; - } - } -} - -module.exports.register = function(exePath) { - forEach(EXTENSIONS, function(extension) { - var regKey = createKey(extension); - - exec('reg ADD ' + regKey.command + ' /d "\\"' + exePath + '\\" -- \\"%1\\"" /f'); - exec('reg ADD ' + regKey.icon + ' /d "' + exePath + ',0" /f'); - }); - - return true; -}; - -module.exports.query = function() { - return ignoreNotFound(function() { - var keys = []; - - forEach(EXTENSIONS, function(extension) { - var regKey = createKey(extension); - - var result = exec('reg query ' + regKey.command + ' /ve'); - - var match = REG_COMMAND_RESULT_PATTERN.exec(result); - - if (match) { - keys.push(match[1]); - } else { - throw new Error('query: unparsable result:\n', result); - } - }); - - return uniqueBy((e) => e, keys); - }); -}; - -module.exports.deregister = function() { - ignoreNotFound(function() { - forEach(EXTENSIONS, function(extension) { - var regKey = createKey(extension); - - exec('reg DELETE ' + regKey.base + ' /f'); - }); - }); -}; diff --git a/app/lib/platform/windows/index.js b/app/lib/platform/windows/index.js index acd61041f6..19f01fb29c 100644 --- a/app/lib/platform/windows/index.js +++ b/app/lib/platform/windows/index.js @@ -1,19 +1,8 @@ 'use strict'; -var dialog = require('electron').dialog; - -var FileAssociations = require('./file-associations'); - -var FILE_ASSOCIATION_KEY = 'fileAssociation'; function WindowsPlatform(app, config) { - // setup file associations + deferred open file - // specified via command-line - app.on('app:client-ready', function() { - checkFileAssociations(app, config); - }); - /** * Adding recent open files. */ @@ -26,70 +15,4 @@ function WindowsPlatform(app, config) { }); } -module.exports = WindowsPlatform; - - -/** - * Check application file associations and - * initialize/update as needed. - * - * @param {ElectronApp} app - * @param {Config} config - */ -function checkFileAssociations(app, config) { - - var executablePath = app.getPath('exe'); - - var userChoice = config.get(FILE_ASSOCIATION_KEY); - - needsAssociation(userChoice, function(err, associate) { - - if (err) { - return console.error('[file association] failed to check', err); - } - - if (associate) { - - associateEditor(executablePath, function(err) { - if (err) { - return console.error('[file association] failed to associate', err); - } - }); - } - - config.set(FILE_ASSOCIATION_KEY, associate); - }); -} - - -function needsAssociation(existingChoice, done) { - - if (existingChoice !== undefined) { - return done(null, existingChoice); - } else { - suggestFileAssociation(done); - } -} - -function associateEditor(executablePath, done) { - - try { - FileAssociations.register(executablePath); - } catch (e) { - return done(e); - } - - done(null); -} - -function suggestFileAssociation(done) { - dialog.showMessageBox({ - type: 'question', - buttons: [ 'Yes', 'No' ], - title: 'Camunda Modeler', - message: 'Do you want to associate your .bpmn, .dmn and .cmmn files to the Camunda Modeler?' - }, function(answer) { - // return true, if the user agreed - done(null, answer === 0); - }); -} +module.exports = WindowsPlatform; \ No newline at end of file diff --git a/app/test/spec/platform/windows/file-associations-spec.js b/app/test/spec/platform/windows/file-associations-spec.js deleted file mode 100644 index 750672b729..0000000000 --- a/app/test/spec/platform/windows/file-associations-spec.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict'; - -var FileAssociations = require('../../../../lib/platform/windows/file-associations'); - -// only run these tests on windows -if (process.platform === 'win32') { - - var TEST_EXECUTABLE = 'C:\\bar\\foo.exe'; - - describe('app/platform/windows - FileAssociations', function() { - - afterEach(function() { - FileAssociations.deregister(); - }); - - - it('should not exist, initially', function() { - // assume - expect(FileAssociations.query()).not.to.exist; - }); - - - it('should add to registry', function() { - var query; - - // when - FileAssociations.register(TEST_EXECUTABLE); - - query = FileAssociations.query(); - - // then - expect(query).to.have.length(1); - expect(query[0]).to.equal(TEST_EXECUTABLE); - }); - - - it('should remove from registry', function() { - - // given - FileAssociations.register(TEST_EXECUTABLE); - - // when - FileAssociations.deregister(); - - // then - expect(FileAssociations.query()).to.not.exist; - }); - - }); -} \ No newline at end of file