Skip to content

Commit 7d03c33

Browse files
committed
use execSync to watch compiled file, issue #6
1 parent d28d299 commit 7d03c33

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

lib/check-file-exists.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var fs = require('fs')
2+
var filePath = process.argv[2]
3+
4+
const handler = function (stat) {
5+
if (stat && stat.birthtime.getTime() > 0) {
6+
process.exit(0)
7+
}
8+
}
9+
10+
fs.watchFile(filePath, { interval: 100 }, handler)
11+
fs.stat(filePath, function (err, stat) { handler(stat) })

lib/child-require-hook.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var fs = require('fs')
22
var getCompiledPath = require('./get-compiled-path')
33
var sep = require('path').sep
4-
4+
var join = require('path').join
5+
var execSync = require('child_process').execSync
56
var compilationId
67
var timeThreshold = 10000
78
var allowJs = false
@@ -10,6 +11,8 @@ var preferTs = false
1011
var ignore = [/node_modules/]
1112
var readyFile
1213

14+
var checkFileScript = join(__dirname, 'check-file-exists.js')
15+
1316
var compile = (code, fileName) => {
1417
var compiledPath = getCompiledPath(code, fileName, compiledDir)
1518
process.send({
@@ -18,20 +21,8 @@ var compile = (code, fileName) => {
1821
})
1922
var compileRequestFile = [compiledDir, compilationId + '.req'].join(sep)
2023
fs.writeFileSync(compileRequestFile, [fileName, compiledPath].join('\n'))
21-
var compiled
22-
var start = new Date().getTime()
23-
var passed
24-
while (compiled === undefined) {
25-
if (fs.existsSync(compiledPath + '.done')) {
26-
compiled = fs.readFileSync(compiledPath, 'utf-8')
27-
}
28-
passed = (new Date().getTime() - start)
29-
if (passed > timeThreshold) {
30-
throw new Error(
31-
'Could not require ' + fileName + ', compiled path:' + compiledPath
32-
)
33-
}
34-
}
24+
execSync(['node', checkFileScript, '"' + compiledPath + '.done' + '"'].join(' '), { stdio: 'inherit' })
25+
var compiled = fs.readFileSync(compiledPath, 'utf-8')
3526
return compiled
3627
}
3728

lib/compiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var compiler = {
4242
},
4343
writeChildHookFile: function (options) {
4444
var fileData = fs.readFileSync(path.join(__dirname, 'child-require-hook.js'), 'utf-8')
45-
var compileTimeout = parseInt(options['compile-timeout'])
45+
var compileTimeout = parseInt(options['compile-timeout'])
4646
if (compileTimeout) {
4747
fileData = fileData.replace('10000', compileTimeout.toString())
4848
}
@@ -65,6 +65,7 @@ var compiler = {
6565
fileData = fileData.replace('./get-compiled-path', path.join(__dirname, 'get-compiled-path').replace(/\\/g, '/'))
6666
fileData = fileData.replace('var readyFile',
6767
'var readyFile = "' + compiler.getCompilerReadyFilePath() + '"')
68+
fileData = fileData.replace(/__dirname/, '"' + __dirname.replace(/\\/g, '/') + '"')
6869
fs.writeFileSync(compiler.getChildHookPath(), fileData)
6970
},
7071
init: function (options) {

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ module.exports = function (script, scriptArgs, nodeArgs, opts) {
135135
}
136136

137137
function stop(willTerminate) {
138-
child.respawn = true;
138+
if (!child || child.stopping) return;
139+
child.stopping = true
140+
child.respawn = true;
139141
child.disconnect();
140142
if (!willTerminate) child.kill('SIGTERM');
141143
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-node-dev",
3-
"version": "1.0.0-pre.23",
3+
"version": "1.0.0-pre.24",
44
"description": "Compiles your TS app and restarts when files are modified.",
55
"keywords": [
66
"restart",

0 commit comments

Comments
 (0)