Skip to content
This repository was archived by the owner on Jul 18, 2018. It is now read-only.

Commit e5d7714

Browse files
author
Jens Lind
committed
Implemented Finder support #9
1 parent ee4ab47 commit e5d7714

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

lib/Grunt.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@ exports.getTasks = function () {
2121
}
2222

2323
exports.runTask = function (task, cb) {
24+
let current = window.localStorage.getItem('current')
25+
2426
// Stop task if running
25-
if (global.processes[task]) {
26-
global.processes[task].kill()
27+
if (global.processes[current] && global.processes[current][task]) {
28+
global.processes[current][task].kill()
2729
return
2830
}
2931

3032
let dir = window.localStorage.getItem('current')
3133
var command = spawn('grunt', [task], {cwd: dir})
3234

3335
// Save
34-
global.processes[task] = command
36+
if (!global.processes[current]) {
37+
global.processes[current] = {}
38+
}
39+
global.processes[current][task] = command
3540

3641
cb()
3742

@@ -42,14 +47,16 @@ exports.runTask = function (task, cb) {
4247
message: (!code) ? 'Done' : 'Error'
4348
})
4449

45-
delete global.processes[task]
50+
delete global.processes[current][task]
4651
cb()
4752
})
4853
}
4954

5055
exports.stopAll = function () {
51-
for (let p in global.processes) {
52-
global.processes[p].kill()
56+
for (let project in global.processes) {
57+
for (let p in global.processes[project]) {
58+
global.processes[project][p].kill()
59+
}
5360
}
5461

5562
global.processes = {}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"standard": "^4.0.1"
2525
},
2626
"dependencies": {
27+
"current-path": "^1.0.1",
2728
"fix-path": "^1.1.0",
2829
"get-grunt-tasks": "^1.0.0",
2930
"node-notifier": "^4.2.1",

src/tray.js

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

33
void (function () {
4+
const FINDER_UPDATE = 1000
5+
46
const remote = require('remote')
57
const Tray = remote.require('tray')
68
const Menu = remote.require('menu')
@@ -9,6 +11,9 @@ void (function () {
911
const ipc = require('ipc')
1012
const grunt = require('./lib/Grunt')
1113
const tildify = require('tildify')
14+
const currentPath = require('current-path')
15+
16+
var finderInterval
1217

1318
// Set up tray menu.
1419
let tray = new Tray(__dirname + '/gruntTemplate.png')
@@ -17,25 +22,48 @@ void (function () {
1722
build()
1823

1924
function build () {
20-
let current = window.localStorage.getItem('current') || 'Choose folder...'
21-
trayMenu.append(new MenuItem({
22-
label: tildify(current),
25+
// Current menu
26+
let currentMenu = new Menu()
27+
currentMenu.append(new MenuItem({
28+
label: 'Follow Finder',
29+
type: 'checkbox',
30+
checked: window.localStorage.getItem('followFinder') === 'true',
31+
click: function (item) {
32+
window.localStorage.setItem('followFinder', item.checked)
33+
rebuild()
34+
}
35+
}))
36+
37+
currentMenu.append(new MenuItem({type: 'separator'}))
38+
39+
currentMenu.append(new MenuItem({
40+
label: 'Choose folder...',
2341
click: function () {
2442
dialog.showOpenDialog({ properties: ['openDirectory']}, function (dir) {
2543
if (dir !== undefined) {
2644
window.localStorage.setItem('current', dir)
45+
46+
// Do not follow finder
47+
window.localStorage.setItem('followFinder', 'false')
48+
clearInterval(finderInterval)
49+
2750
if (Object.keys(global.processes).length > 0) {
2851
// Stop all tasks
2952
grunt.stopAll()
3053
} else {
31-
trayMenu = new Menu()
32-
build()
54+
rebuild()
3355
}
3456
}
3557
})
3658
}
3759
}))
3860

61+
let current = window.localStorage.getItem('current') || 'Choose folder...'
62+
trayMenu.append(new MenuItem({
63+
label: tildify(current),
64+
submenu: currentMenu
65+
}))
66+
3967
trayMenu.append(new MenuItem({type: 'separator'}))
4068

4169
grunt.getTasks()
@@ -47,14 +75,15 @@ void (function () {
4775
click: function () {
4876
grunt.runTask(task, function () {
4977
// Rebuild menu
50-
trayMenu = new Menu()
51-
build()
78+
rebuild()
5279
})
5380
}
5481
}
5582

56-
if (global.processes[task]) {
57-
item.checked = true
83+
if (global.processes[current]) {
84+
if (global.processes[current][task]) {
85+
item.checked = true
86+
}
5887
}
5988

6089
trayMenu.append(new MenuItem(item))
@@ -71,5 +100,25 @@ void (function () {
71100

72101
tray.setContextMenu(trayMenu)
73102
})
103+
104+
// Follow Finder
105+
if (window.localStorage.getItem('followFinder') === 'true') {
106+
finderInterval = setInterval(function () {
107+
currentPath(function (err, path) {
108+
if (!err && window.localStorage.getItem('current') !== path) {
109+
window.localStorage.setItem('current', path)
110+
111+
rebuild()
112+
}
113+
})
114+
}, FINDER_UPDATE)
115+
}
116+
}
117+
118+
// Rebuild the tray
119+
function rebuild () {
120+
clearInterval(finderInterval)
121+
trayMenu = new Menu()
122+
build()
74123
}
75124
})()

0 commit comments

Comments
 (0)