Skip to content

Feat/notify level #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.vagrant
node_modules
*.log
.ts-node
.ts-node
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ tsnd --respawn server.ts
- `--clear` (`--cls`) Will clear screen on restart
- `--watch` - Explicitly add files or folders to watch and restart on change (list separated by commas)
- `--exit-child` - Adds 'SIGTERM' exit handler in a child process.
- `--notifyLevel` - `error` or `info` (default). The level for which OS level notifications should be sent

**Caveats and points of notice:**

Expand Down
1 change: 1 addition & 0 deletions bin/ts-node-dev
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var opts = minimist(devArgs, {
'ignore-watch',
'interval',
'debounce',
'notifyLevel',
'watch'
],
alias: {
Expand Down
2 changes: 2 additions & 0 deletions lib/cfg.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = function (main, opts) {
if (opts.respawn) c.respawn = true;
if (opts.notify === false) c.notify = false;
if (opts.clear || opts.cls) c.clear = true;
if (opts.notifyLevel) c.notifyLevel = opts.notifyLevel
}

var ignoreWatch = ([]).concat(opts && opts['ignore-watch'] || []).concat(c.ignore || []);
Expand All @@ -37,6 +38,7 @@ module.exports = function (main, opts) {
vm: c.vm !== false,
fork: c.fork !== false,
notify: c.notify !== false,
notifyLevel: c.notifyLevel || 'DEBUG',
deps: c.deps,
timestamp: c.timestamp || (c.timestamp !== false && 'HH:MM:ss'),
clear: !!(c.clear),
Expand Down
8 changes: 8 additions & 0 deletions lib/child-require-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var fs = require('fs')
var getCompiledPath = require('./get-compiled-path')
var sep = require('path').sep
var join = require('path').join
var notify = require(join(__dirname, './notify'))
var execSync = require('child_process').execSync
var compilationId
var timeThreshold = 0
Expand All @@ -13,6 +14,7 @@ var readyFile
var execCheck = false
var exitChild = false
var sourceMapSupportPath
var cfg = {}

var checkFileScript = join(__dirname, 'check-file-exists.js')

Expand Down Expand Up @@ -113,6 +115,12 @@ if (readyFile) {
}
}

process.on('uncaughtException', err => {
notify(cfg, null, err)('Error', err.message || err.stack, 'error')
process.exitCode = 1
process.kill(process.pid, 'SIGTERM')
})

if (exitChild) {
process.on('SIGTERM', function() {
console.log('Child got SIGTERM, exiting.')
Expand Down
8 changes: 8 additions & 0 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ var compilationInstanceStamp = Math.random()
var compiler = {
allowJs: false,
tsConfigPath: '',
cfg: {},
setConfig(cfg) {
this.cfg = cfg
},
getCompilationId: function() {
return compilationInstanceStamp
},
Expand Down Expand Up @@ -117,6 +121,10 @@ var compiler = {
/__dirname/,
'"' + __dirname.replace(/\\/g, '/') + '"'
)
fileData = fileData.replace(
'var cfg = {}',
'var cfg = ' + JSON.stringify(this.cfg)
)
fs.writeFileSync(compiler.getChildHookPath(), fileData)
},
init: function(options) {
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
var log = require('./log')(cfg)
var notify = require('./notify')(cfg, log)
opts.log = log
compiler.setConfig(cfg)
compiler.init(opts)

compiler.notify = notify
Expand Down
15 changes: 13 additions & 2 deletions lib/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@ function icon(level) {
return path.resolve(__dirname, '../icons/node_' + level + '.png');
}

function levelToInt(strLevel) {
if (!strLevel) return 0;
switch (strLevel.toLowerCase()) {
case 'info': return 1;
case 'error': return 3;
default: return 0;
}
}

/**
* Displays a desktop notification and writes a message to the console.
*/
module.exports = function (cfg, log) {
return function (title, msg, level) {
level = level || 'info';
log([title, msg].filter(_ => _).join(': '), level);
if (cfg.notify) {
if (log) {
log([title, msg].filter(_ => _).join(': '), level);
}
if (cfg.notify && levelToInt(cfg.notifyLevel) <= levelToInt(level)) {
notifier.notify({
title: title || 'node.js',
icon: icon(level),
Expand Down