/
index.js
78 lines (65 loc) · 1.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const runApplescript = require('run-applescript');
const getBrowserLauncher = function(browserName) {
return function(url) {
this._url = url;
// record if safari was already open via `wasopen`
// then open safari with a tab at `karma url`
// safari was open a
const script = `
set wasopen to false
if application "${browserName}" is running then set wasopen to true
tell application "${browserName}"
make new document with properties {URL:"${url}"}
end tell
return wasopen
`;
runApplescript(script).then((result) => {
this._wasOpen = (result === 'true');
}).catch((err) => {
throw err;
});
};
};
const getBrowserKiller = function(browserName) {
return function(done) {
// code to close blank favorites tab, we don't need
// it now, but if we ever do:
// close (documents where name is "favorites")
// close the karma url and safari if it wasn't open
// before we started
const script = `
tell application "${browserName}"
close documents where URL = "${this._url}"
${this._wasOpen ? '' : 'quit'}
end tell
`;
runApplescript(script).then((result) => {
done();
}).catch((err) => {
done();
throw err;
});
};
};
const SafariBrowser = function(baseBrowserDecorator) {
baseBrowserDecorator(this);
this._start = getBrowserLauncher('Safari');
this.on('kill', getBrowserKiller('Safari'));
};
SafariBrowser.prototype = {
name: 'Safari'
};
SafariBrowser.$inject = ['baseBrowserDecorator'];
const SafariTechBrowser = function(baseBrowserDecorator) {
baseBrowserDecorator(this);
this._start = getBrowserLauncher('Safari Technology Preview');
this.on('kill', getBrowserKiller('Safari Technology Preview'));
};
SafariTechBrowser.prototype = {
name: 'SafariTechPreview'
};
SafariBrowser.$inject = ['baseBrowserDecorator'];
module.exports = {
'launcher:Safari': ['type', SafariBrowser],
'launcher:SafariTechPreview': ['type', SafariTechBrowser]
};