Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor; use LaunchAgent service rather than Login Items + .app shim
- Loading branch information
Showing
15 changed files
with
230 additions
and
270 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/*jslint node: true */ | ||
var util = require('util-enhanced'); | ||
var fs = require('fs'); | ||
var logger = require('winston'); | ||
|
||
/** new FileWatcher(filepath: string, delay: number, onchange: ()) | ||
Watch a filepath for changes, calling `onchange` when the file changes. | ||
Mostly a wrapper around fs.watch / fs.FSWatcher, but with some debounce. | ||
delay | ||
Milliseconds to wait before calling onchange again. A delay of 0 means | ||
onchange will be called immediately every time the file changes (no debounce). | ||
*/ | ||
function FileWatcher(filepath, delay, onchange) { | ||
this.filepath = filepath; | ||
this.onchange = onchange; | ||
this.delay = delay; | ||
} | ||
|
||
/** FileWatcher#start() | ||
Start the underlying watcher. | ||
Debounces for a period of `delay` seconds; executes on the immediate end. | ||
*/ | ||
FileWatcher.prototype.start = function() { | ||
var self = this; | ||
var last_called = -1; | ||
|
||
this.fs_watcher = fs.watch(this.filepath, {persistent: true}) | ||
.on('change', function(event, filename) { | ||
// filename may not actually be supplied | ||
// TODO: check last modified stats? | ||
// if (curr.mtime.valueOf() != prev.mtime.valueOf() || | ||
// curr.ctime.valueOf() != prev.ctime.valueOf()) { | ||
var since_last = Date.now() - last_called; | ||
logger.debug('Saw %s on %s (%dms since last call)', event, self.filepath, since_last); | ||
if (since_last >= self.delay) { | ||
self.onchange(); | ||
last_called = Date.now(); | ||
} | ||
else { | ||
logger.debug('Not firing onchange due to debounce (%d < %d)', since_last, self.delay); | ||
} | ||
}) | ||
.on('error', function(err) { | ||
logger.error('Error in fs.watch: %s', err); | ||
}); | ||
}; | ||
|
||
/** FileWatcher#stop() | ||
Close the underlying watcher. | ||
*/ | ||
FileWatcher.prototype.stop = function() { | ||
this.fs_watcher.close(); | ||
}; | ||
|
||
module.exports = FileWatcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env node | ||
/*jslint node: true */ | ||
var command = process.argv[2]; | ||
|
||
if (command == 'print-launch-agent') { | ||
require('../commands/print-launch-agent'); | ||
} | ||
else if (command == 'service') { | ||
require('../commands/service'); | ||
} | ||
else { | ||
console.error('Unrecognized command: %s', command); | ||
console.error(' Available commands: print-launch-agent, service'); | ||
process.exit(1); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env node | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
var filepath = path.join(__dirname, '..', 'launch-agent.plist'); | ||
var template = fs.readFileSync(filepath, {encoding: 'utf8'}); | ||
|
||
var string = template.replace(/\$\{(\w+)\}/g, function(match, group, index) { | ||
return process.env[group]; | ||
}); | ||
|
||
process.stdout.write(string); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env node | ||
var path = require('path'); | ||
var child_process = require('child_process'); | ||
var _ = require('underscore'); | ||
var logger = require('winston'); | ||
|
||
var config = require('../config'); | ||
var FileWatcher = require('../FileWatcher'); | ||
|
||
if (process.env.OSX) { | ||
var NotificationCenterTransport = require('winston-notification-center'); | ||
logger.add(NotificationCenterTransport, {title: 'File system watcher'}); | ||
} | ||
|
||
logger.level = process.env.DEBUG ? 'debug' : 'info'; | ||
|
||
var config_filepath = (process.env.CONFIG || '~/.fs-change').replace(/^~/, process.env.HOME); | ||
|
||
var file_watchers = []; | ||
var restart = function() { | ||
logger.debug('Stopping %d FileWatchers', file_watchers.length); | ||
_.invoke(file_watchers, 'stop'); | ||
file_watchers.length = 0; | ||
config.loadFileWatchers(config_filepath, function(err, new_file_watchers) { | ||
if (err) { | ||
logger.error('Error reading config file: %s', err); | ||
process.exit(1); | ||
} | ||
|
||
Array.prototype.push.apply(file_watchers, new_file_watchers); | ||
|
||
logger.debug('Starting %d FileWatchers', file_watchers.length); | ||
_.invoke(file_watchers, 'start'); | ||
}); | ||
}; | ||
|
||
var config_watcher = new FileWatcher(config_filepath, 2000, restart); | ||
config_watcher.start(); | ||
restart(); |
Oops, something went wrong.