forked from jadonk/bonescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autorun.js
124 lines (109 loc) · 3.51 KB
/
autorun.js
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
// Copyright (C) 2013 - Texas Instruments, Jason Kridner
//
//
var fs = require('fs');
var child_process = require('child_process');
var winston = require('winston');
var ar = '/var/lib/cloud9/autorun';
var apps = {};
winston.info('Starting bonescript autorun service');
fs.exists(ar, arExists);
function arExists(exists) {
if(!exists) fs.mkdir(ar, arWatch);
else fs.readdir(ar, arFound);
}
function arFound(err, files) {
if(err) {
winston.err('Error reading directory: ' + err);
arWatch();
return;
}
var i = 0;
arTestNext();
function arTestNext() {
if(i == files.length) {
arWatch();
return;
}
appStart(files[i]);
i++;
arTestNext();
}
}
function appStart(file) {
if(apps[file]) return;
appTest();
function appTest() {
fs.exists(ar + '/' + file, appExists);
}
function appExists(exists) {
function onStdout(data) {
winston.info('stdout (' + file + '): ' + data);
}
function onStderr(data) {
winston.info('stderr (' + file + '):' + data);
}
if(exists) {
if(typeof apps[file] != 'undefined') {
winston.info('already running: ' + file);
return;
}
if(file.match(/\.js$/)) {
winston.info('start: ' + file);
apps[file] = child_process.spawn(process.argv[0], [ar + '/' + file]);
apps[file].on('close', appClosed);
apps[file].stdout.on('data', onStdout);
apps[file].stderr.on('data', onStderr);
} else if(file.match(/\.py$/)) {
winston.info('start: ' + file);
apps[file] = child_process.spawn('/usr/bin/python', [ar + '/' + file]);
apps[file].on('close', appClosed);
apps[file].stdout.on('data', onStdout);
apps[file].stderr.on('data', onStderr);
} else if(file.match(/\.ino$/)) {
winston.info('start: ' + file);
apps[file] = child_process.spawn('/usr/bin/make',
[
"-f",
"/var/lib/cloud9/extras/Userspace-Arduino/Makefile",
"TARGET=" + file.replace(/\.ino$/, ''),
"LOCAL_INO_SRCS=" + file,
"LOCAL_C_SRCS=",
"LOCAL_CPP_SRCS=",
"LOCAL_PDE_SRCS=",
"LOCAL_AS_SRCS=",
"COMMON_DEPS="
],
{ 'cwd': ar }
);
apps[file].on('close', appClosed);
apps[file].stdout.on('data', onStdout);
apps[file].stderr.on('data', onStderr);
}
}
}
function appClosed(code, signal) {
delete apps[file];
if(signal == 'SIGTERM') setTimeout(appTest, 1000);
}
}
function arWatch() {
fs.watch(ar, arWatcher);
}
function arWatcher(event, filename) {
if(event == 'change') {
winston.info('change: ' + filename);
appStop(filename);
appStart(filename);
} else if(event == 'rename') {
winston.info('rename: ' + filename);
appStop(filename);
appStart(filename);
}
}
function appStop(file) {
if(typeof apps[file] != 'undefined') {
winston.info('stop: ' + file + ' (pid: ' + apps[file].pid + ')');
apps[file].kill('SIGTERM');
}
}