ts-node-dev
Advanced tools
Comparing version
@@ -25,3 +25,3 @@ var fs = require('fs') | ||
while (true) { | ||
const exists = execCheck | ||
var exists = execCheck | ||
? execSync(['node', checkFileScript, '"' + fileName + '"'].join(' '), { | ||
@@ -66,2 +66,14 @@ stdio: 'inherit' | ||
}) | ||
if (preferTs) { | ||
function reorderRequireExtension (ext) { | ||
var old = require.extensions[ext] | ||
delete require.extensions[ext] | ||
require.extensions[ext] = old | ||
} | ||
var order = ['.ts'].concat(Object.keys(require.extensions) | ||
.filter(_ => _ !== '.ts')) | ||
order.forEach(function (ext) { | ||
reorderRequireExtension(ext) | ||
}) | ||
} | ||
} | ||
@@ -74,3 +86,4 @@ | ||
function registerJsExtension() { | ||
var old = require.extensions['.js'] | ||
var old = require.extensions['.js'] | ||
// handling preferTs probably redundant after reordering | ||
if (allowJs || preferTs) { | ||
@@ -80,11 +93,11 @@ require.extensions['.js'] = function(m, fileName) { | ||
return old(m, fileName) | ||
} | ||
} | ||
var tsCode | ||
var tsFileName | ||
if (preferTs && !isFileInNodeModules(fileName)) { | ||
tsFileName = fileName.replace(/\.js$/, '.ts') | ||
if (fs.existsSync(tsFileName)) { | ||
tsCode = fs.readFileSync(tsFileName, 'utf-8') | ||
} | ||
} | ||
// if (preferTs && !isFileInNodeModules(fileName)) { | ||
// tsFileName = fileName.replace(/\.js$/, '.ts') | ||
// // if (fs.existsSync(tsFileName)) { | ||
// // tsCode = fs.readFileSync(tsFileName, 'utf-8') | ||
// // } | ||
// } | ||
var _compile = m._compile | ||
@@ -106,2 +119,3 @@ var isIgnored = | ||
} | ||
return old(m, fileName) | ||
@@ -133,3 +147,3 @@ } | ||
if (exitChild) { | ||
process.on('SIGTERM', function() { | ||
process.on('SIGTERM', function() { | ||
console.log('Child got SIGTERM, exiting.') | ||
@@ -136,0 +150,0 @@ process.exit() |
@@ -1,2 +0,2 @@ | ||
var register = require('ts-node').register | ||
var tsNode = require('ts-node') | ||
@@ -73,3 +73,3 @@ var fs = require('fs') | ||
} | ||
if (options['prefer-ts']) { | ||
if (options['prefer-ts'] || options['prefer-ts-exts']) { | ||
fileData = fileData.replace('preferTs = false', 'preferTs = true') | ||
@@ -164,2 +164,19 @@ } | ||
// var tsNodeOptions = { | ||
// //dir: should add | ||
// emit: options['emit'], | ||
// files: options['files'], | ||
// pretty: options['pretty'], | ||
// transpileOnly: options['transpile-only'], | ||
// ignore: [].concat(options['ignore']), | ||
// preferTsExts: options['prefer-ts-exts'] || options['prefer-ts'], | ||
// logError: options['log-error'], | ||
// project: options['project'], | ||
// skipProject: options['skip-project'], | ||
// skipIgnore: options['skip-ignore'], | ||
// compiler: options['compiler'], | ||
// ignoreDiagnostics: options['ignore-diagnostics'], | ||
// disableWarnings: options['disableWarnings'], | ||
// compilerOptions: options['compiler-options'], | ||
// } | ||
var compilerOptionsArg = | ||
@@ -184,29 +201,29 @@ options['compilerOptions'] || options['compiler-options'] | ||
var tsNodeOptions = { | ||
fast: options['fast'], | ||
cache: options['cache'] || !options['no-cache'], | ||
typeCheck: options['type-check'], | ||
transpileOnly: options['transpileOnly'] || options['transpile-only'], | ||
pretty: options['pretty'], | ||
cacheDirectory: options['cache-directory'] || path.join(tmpDir, 'cache'), | ||
compiler: options['compiler'], | ||
project: options['project'], | ||
skipProject: options['skip-project'], | ||
skipIgnore: options['skip-ignore'], | ||
ignore: ignore, | ||
ignoreWarnings: | ||
options['ignoreWarnings'] || | ||
options['ignoreDiagnostics'] || | ||
options['ignore-diagnostics'], | ||
ignoreDiagnostics: | ||
options['ignoreDiagnostics'] || options['ignore-diagnostics'], | ||
logError: options['log-error'], | ||
disableWarnings: options['disableWarnings'], | ||
preferTsExts: options['prefer-ts-exts'], | ||
compilerOptions: compilerOptions, | ||
files: options['files'] || true, | ||
} | ||
var DEFAULTS = tsNode.DEFAULTS | ||
try { | ||
compiler.service = register(tsNodeOptions) | ||
compiler.service = tsNode.register({ | ||
// should add --script-mode | ||
dir: options['dir'] || DEFAULTS.dir, | ||
scope: options['dir'] || DEFAULTS.scope, | ||
emit: options['emit'] || DEFAULTS.emit, | ||
files: options['files'] || DEFAULTS.files, | ||
pretty: options['pretty'] || DEFAULTS.pretty, | ||
transpileOnly: options['transpile-only'] || DEFAULTS.transpileOnly, | ||
ignore: options['ignore'] | ||
? tsNode.split(options['ignore']) || options['ignore'] | ||
: DEFAULTS.ignore, | ||
preferTsExts: | ||
options['prefer-ts-exts'] || | ||
options['prefer-ts'] || | ||
DEFAULTS.preferTsExts, | ||
logError: options['log-error'] || DEFAULTS.logError, | ||
project: options['project'], | ||
skipProject: options['skip-project'], | ||
skipIgnore: options['skip-ignore'], | ||
compiler: options['compiler'] || DEFAULTS.compiler, | ||
compilerHost: options['compiler-host'] || DEFAULTS.compilerHost, | ||
ignoreDiagnostics: options['ignore-diagnostics'], | ||
compilerOptions: tsNode.parse(options['compiler-options']), | ||
}) | ||
} catch (e) { | ||
@@ -213,0 +230,0 @@ console.log(e) |
@@ -12,3 +12,3 @@ var fork = require('child_process').fork | ||
module.exports = function(script, scriptArgs, nodeArgs, opts) { | ||
module.exports = function (script, scriptArgs, nodeArgs, opts) { | ||
if (typeof script !== 'string' || script.length === 0) { | ||
@@ -48,13 +48,15 @@ throw new TypeError('`script` must be a string') | ||
// }) | ||
function initWatcher () { | ||
function initWatcher() { | ||
var watcher = chokidar.watch([], { | ||
usePolling: opts.poll, | ||
interval: parseInt(opts.interval) || undefined, | ||
}) | ||
watcher.on('change', restart) | ||
watcher.on('fallback', function(limit) { | ||
log.warn('node-dev ran out of file handles after watching %s files.', limit) | ||
watcher.on('fallback', function (limit) { | ||
log.warn( | ||
'node-dev ran out of file handles after watching %s files.', | ||
limit | ||
) | ||
log.warn('Falling back to polling which uses more CPU.') | ||
@@ -67,5 +69,5 @@ log.info('Run ulimit -n 10000 to increase the file descriptor limit.') | ||
var watcher = initWatcher() | ||
var starting = false | ||
// Read for "rs" from command line | ||
@@ -76,5 +78,5 @@ if (opts.rs !== false) { | ||
output: process.stdout, | ||
terminal: false | ||
terminal: false, | ||
}) | ||
rl.on('line', function(line) { | ||
rl.on('line', function (line) { | ||
if (line.trim() === 'rs') { | ||
@@ -101,3 +103,2 @@ restart('', true) | ||
var childHookPath = compiler.getChildHookPath() | ||
cmd = (opts.priorNodeArgs || []).concat(['-r', childHookPath]).concat(cmd) | ||
@@ -107,3 +108,3 @@ log.debug('Starting child process %s', cmd.join(' ')) | ||
cwd: process.cwd(), | ||
env: process.env | ||
env: process.env, | ||
}) | ||
@@ -114,6 +115,6 @@ starting = false | ||
compileReqWatcher.close() | ||
} | ||
} | ||
compileReqWatcher = chokidar.watch([], { | ||
usePolling: opts.poll, | ||
interval: parseInt(opts.interval) || undefined | ||
interval: parseInt(opts.interval) || undefined, | ||
}) | ||
@@ -123,4 +124,4 @@ var currentCompilePath | ||
compileReqWatcher.add(compiler.getCompileReqFilePath()) | ||
compileReqWatcher.on('change', function(file) { | ||
fs.readFile(file, 'utf-8', function(err, data) { | ||
compileReqWatcher.on('change', function (file) { | ||
fs.readFile(file, 'utf-8', function (err, data) { | ||
if (err) { | ||
@@ -139,3 +140,3 @@ log.error('Error reading compile request file', err) | ||
compile: compile, | ||
compiledPath: compiledPath | ||
compiledPath: compiledPath, | ||
}) | ||
@@ -145,3 +146,3 @@ } | ||
}) | ||
child.on('message', function(message) { | ||
child.on('message', function (message) { | ||
if (!message.compiledPath || currentCompilePath === message.compiledPath) | ||
@@ -153,3 +154,3 @@ return | ||
child.on('exit', function(code) { | ||
child.on('exit', function (code) { | ||
log.debug('Child exited with code %s', code) | ||
@@ -170,3 +171,3 @@ if (!child) return | ||
// Listen for `required` messages and watch the required file. | ||
ipc.on(child, 'required', function(m) { | ||
ipc.on(child, 'required', function (m) { | ||
var isIgnored = | ||
@@ -177,2 +178,3 @@ cfg.ignore.some(isPrefixOf(m.required)) || | ||
if (!isIgnored && (cfg.deps === -1 || getLevel(m.required) <= cfg.deps)) { | ||
log.debug(m.required, 'added to watcher') | ||
watcher.add(m.required) | ||
@@ -183,3 +185,3 @@ } | ||
// Upon errors, display a notification and tell the child to exit. | ||
ipc.on(child, 'error', function(m) { | ||
ipc.on(child, 'error', function (m) { | ||
log.debug('Child error') | ||
@@ -211,3 +213,3 @@ notify(m.error, m.message, 'error') | ||
//if (!willTerminate) { | ||
killChild() | ||
killChild() | ||
//} | ||
@@ -237,3 +239,3 @@ } | ||
//watcher.removeAll()ya | ||
watcher.close() | ||
@@ -254,3 +256,3 @@ watcher = initWatcher() | ||
// Relay SIGTERM | ||
process.on('SIGTERM', function() { | ||
process.on('SIGTERM', function () { | ||
log.debug('Process got SIGTERM') | ||
@@ -261,3 +263,3 @@ killChild() | ||
log.info('Restarting terminated in ' + timeout + ' seconds') | ||
setTimeout(() => { | ||
setTimeout(() => { | ||
start() | ||
@@ -267,3 +269,3 @@ }, timeout) | ||
process.exit(0) | ||
} | ||
} | ||
}) | ||
@@ -295,3 +297,3 @@ | ||
function isPrefixOf(value) { | ||
return function(prefix) { | ||
return function (prefix) { | ||
return value.indexOf(prefix) === 0 | ||
@@ -302,5 +304,5 @@ } | ||
function isRegExpMatch(value) { | ||
return function(regExp) { | ||
return function (regExp) { | ||
return new RegExp(regExp).test(value) | ||
} | ||
} |
@@ -8,4 +8,5 @@ var path = require('path') | ||
var resolveMain = require('./resolveMain') | ||
// var Module = require('module') | ||
// Remove wrap.js from the argv array | ||
// Remove wrap.js from the argv array | ||
process.argv.splice(1, 1) | ||
@@ -40,2 +41,9 @@ | ||
// var lastRequired = null | ||
// var origRequire = Module.prototype.require | ||
// Module.prototype.require = function (requirePath) { | ||
// lastRequired = { path: requirePath, filename: this.filename } | ||
// return origRequire.apply(this, arguments) | ||
// } | ||
// Error handler that displays a notification and logs the stack to stderr: | ||
@@ -53,7 +61,9 @@ var caught = false | ||
if (!hasCustomHandler && !isTsError) { | ||
console.error((err && err.stack) || err) | ||
//console.error((err && err.stack) || err) | ||
} | ||
ipc.send({ | ||
error: isTsError ? '' : err && err.name || 'Error', | ||
error: isTsError ? '' : (err && err.name) || 'Error', | ||
// lastRequired: lastRequired, | ||
message: err ? err.message : '', | ||
code: err && err.code, | ||
willTerminate: hasCustomHandler, | ||
@@ -60,0 +70,0 @@ }) |
{ | ||
"name": "ts-node-dev", | ||
"version": "1.0.0-pre.50", | ||
"version": "1.0.0-pre.51", | ||
"description": "Compiles your TS app and restarts when files are modified.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -31,3 +31,3 @@ # ts-node-dev | ||
``` | ||
ts-node-dev --respawn --transpileOnly server.ts | ||
ts-node-dev --respawn --transpile-only server.ts | ||
``` | ||
@@ -43,4 +43,4 @@ | ||
- `--prefer-ts` (default: false) - for each `.js` file (that is not in `node_modules`) will try to check if corresponding `.ts` version exists and require it. | ||
- `--ignore-watch` (default: []) - files/folders to be [ignored by `node-dev`](https://github.com/fgnass/node-dev#ignore-paths). **But also this behaviour enhanced:** it will also make up `new RegExp` of passed ignore string and check absolute paths of required files for match. | ||
- `--prefer-ts` (`--prefer-ts-exts`) - (default: false) - For each `.js` file (that is not in `node_modules`) will try to check if corresponding `.ts` version exists and require it. | ||
- `--ignore-watch` - (default: []) - files/folders to be [ignored by `node-dev`](https://github.com/fgnass/node-dev#ignore-paths). **But also this behaviour enhanced:** it will also make up `new RegExp` of passed ignore string and check absolute paths of required files for match. | ||
So, to ignore everything in `node_modules`, just pass `--ignore-watch node_modules`, or us `--no-deps` for the same effect. | ||
@@ -47,0 +47,0 @@ |
Sorry, the diff of this file is not supported yet
43057
3.77%1013
4.43%