Repository navigation
Expand file tree
/
Copy pathautotest.js
More file actions
169 lines (145 loc) · 4.05 KB
/
Copy pathautotest.js
File metadata and controls
169 lines (145 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Watches the spec files for changes and reloads the Jasmine test runner
// Watches the source files for changes and auto-builds with Sprockets
// Rebuilds also trigger the tests
var SOURCE_DIR = "src";
var SOURCE_MAIN = SOURCE_DIR+'/application.js';
var BUILD_DIR = 'build';
var BUILD_MAIN = BUILD_DIR+'/application.js';
var SPEC_DIR = 'spec';
var WATCH_INTERVAL = 5000;
// Requires
var sys = require("sys"),
fs = require("fs")
spawn = require('child_process').spawn,
exec = require('child_process').exec;
var FileWatcher = {};
FileWatcher.create = function(pathToWatch, callback, options)
{
var _options = options || []
var _callOnChange = callback;
var _rootWatchedPaths = (pathToWatch instanceof Array) ? pathToWatch : [pathToWatch];
var _watchedFiles = [];
var _conditionallyCallOnChange = function(currStat, prevStat)
{
// Only execute if the file was actually modified
if(""+currStat.mtime == ""+prevStat.mtime)
{
return;
}
// Remove watchers, if necessary
_options["disableDuringCallback"] && _unwatchAllPaths();
// Call _callOnChange()
_callOnChange();
// Refresh watchers, if necessary
if(_options["disableDuringCallback"] || _options["refreshAfterCallback"])
{
_reprocessRootPaths();
}
};
var _unwatchAllPaths = function()
{
while(_watchedFiles.length > 0)
{
// Clear the filesystem watch and remove from the array
var filePath = _watchedFiles.pop();
fs.unwatchFile(filePath);
}
};
var _watchThisFilePath = function(filePath)
{
fs.watchFile(filePath,
{persistent:true, interval:WATCH_INTERVAL},
_conditionallyCallOnChange);
_watchedFiles.push(filePath);
};
var _processPath = function(filePath)
{
var fileStat = fs.statSync(filePath);
if(fileStat.isFile())
{
_watchThisFilePath(filePath);
}
else if(fileStat.isDirectory())
{
var files = fs.readdirSync(filePath);
files.forEach(function(child)
{
// Descend the filename
_processPath(filePath+'/'+child);
});
}
};
var _reprocessRootPaths = function()
{
_unwatchAllPaths();
_rootWatchedPaths.forEach(function(watchThisPath)
{
_processPath(watchThisPath);
});
};
// Check the pathToWatch (file or directory?) and do the appropriate operation
_reprocessRootPaths();
};
var Jasmine = {};
Jasmine.init = function()
{
var _jasmineProcess;
var _stop = function()
{
_jasmineProcess && _jasmineProcess.kill();
};
var _start = function()
{
sys.puts("Starting Jasmine Server...");
_stop();
_jasmineProcess = spawn('rake', ['jasmine']);
_jasmineProcess.stdout.addListener('data', function (data)
{
sys.puts('jasmine stdout: ' + data);
});
_jasmineProcess.stderr.addListener('data', function (data)
{
sys.puts('jasmine stderr: ' + data);
});
_jasmineProcess.addListener('exit', function (code)
{
sys.puts('Jasmine Server exited with code: ' + code);
});
// Watch the test files and output file
FileWatcher.create(SPEC_DIR, _reopenTestPage);
};
var _reopenTestPage = function()
{
sys.puts("Opening Jasmine Test Runner.");
exec("open -g -a 'Google Chrome' 'http://localhost:8888'", function(error, stdout, stderr)
{
if (error !== null)
{
sys.puts('testPage exec error: ' + error);
}
});
};
return {
start: _start,
stop: _stop,
reopenTestPage: _reopenTestPage
};
};
var jasmineManager = Jasmine.init();
jasmineManager.start();
// define a function for calling sprockets
var sprocketize = function()
{
sys.puts("Sprocketizing source.");
var sprocketizeCommand = 'sprocketize -I src '+SOURCE_MAIN+' > '+BUILD_MAIN;
var sprocket = exec(sprocketizeCommand,
function(error, stdout, stderr)
{
if (error !== null) {
sys.puts('exec error: ' + error);
}
jasmineManager.reopenTestPage();
}
);
};
FileWatcher.create(SOURCE_DIR, sprocketize, { 'disableDuringCallback':true });