@@ -2,13 +2,11 @@ var crypto = require('crypto');
2
2
var padManager = require ( "ep_etherpad-lite/node/db/PadManager" ) ;
3
3
var fs = require ( 'fs' ) ;
4
4
5
-
6
-
7
- // read base project folder from settings.json
8
5
var settings = require ( 'ep_etherpad-lite/node/utils/Settings' ) ;
9
6
var authorManager = require ( 'ep_etherpad-lite/node/db/AuthorManager' ) ;
10
7
var sessionManager = require ( 'ep_etherpad-lite/node/db/SessionManager' ) ;
11
8
9
+ // this should be overridden in codepad conf
12
10
var project_path = '/tmp/' ;
13
11
14
12
if ( ! settings . ep_codepad ) {
@@ -35,16 +33,29 @@ var getBrush = require('./extensions.js').getBrush;
35
33
36
34
// code syntax checker
37
35
var jshint = require ( 'jshint' ) . JSHINT ;
38
-
36
+ var cb = function ( ) { } ;
39
37
var padMessageHandler = require ( "ep_etherpad-lite/node/handler/PadMessageHandler" ) ;
40
38
39
+ var send_ok = function ( padid )
40
+ {
41
+ var ok_msg = {
42
+ type : 'COLLABROOM' ,
43
+ data : {
44
+ type : "CUSTOM" ,
45
+ payload : {
46
+ padId : padid ,
47
+ from : "codepad" ,
48
+ errors : null
49
+ }
50
+ }
51
+ } ;
52
+ padMessageHandler . handleCustomObjectMessage ( ok_msg , undefined , cb ) ;
53
+ } ;
41
54
42
55
exports . handleMessage = function ( hook_name , context , callback ) {
43
56
44
57
if ( context . message && context . message . data ) {
45
58
46
- var cb = function ( ) { } ;
47
-
48
59
var msg = context . message . data . type ;
49
60
console . log ( "MSG: " + msg ) ;
50
61
@@ -58,13 +69,28 @@ exports.handleMessage = function(hook_name, context, callback) {
58
69
59
70
fs . readFile ( path , function ( err , data ) {
60
71
if ( err ) {
72
+ // notify user about the read error
61
73
console . log ( "codepad-read-error " + path ) ;
62
- }
63
-
64
- var adat = data . toString ( ) ;
65
- if ( adat !== text ) {
66
- value . setText ( adat ) ;
67
- padMessageHandler . updatePadClients ( value , cb ) ;
74
+ var err_msg = {
75
+ type : 'COLLABROOM' ,
76
+ data : {
77
+ type : "CUSTOM" ,
78
+ payload : {
79
+ padId : padid ,
80
+ from : "fs" ,
81
+ errors : err
82
+ }
83
+ }
84
+ } ;
85
+ padMessageHandler . handleCustomObjectMessage ( err_msg , undefined , cb ) ;
86
+ callback ( null ) ;
87
+ } else {
88
+ var adat = data . toString ( ) ;
89
+ if ( adat !== text ) {
90
+ value . setText ( adat ) ;
91
+ padMessageHandler . updatePadClients ( value , cb ) ;
92
+ }
93
+ send_ok ( padid ) ;
68
94
}
69
95
} ) ;
70
96
} ) ;
@@ -79,7 +105,7 @@ exports.handleMessage = function(hook_name, context, callback) {
79
105
var folder = '' ;
80
106
81
107
// since EEXISTS is an error, we skip that error
82
- // TODO, use mkdirP ?
108
+ // there will be a message if the write failes anyway
83
109
mkdir_err = function ( err ) { } ;
84
110
85
111
// create subfolders
@@ -94,25 +120,21 @@ exports.handleMessage = function(hook_name, context, callback) {
94
120
// get the file extension/brush
95
121
var ext = getBrush ( padid ) ;
96
122
97
- // the beutified text of the pad
123
+ // if all ok, send only one OK message.
124
+ var all_is_ok = true ;
98
125
126
+ // the beutified or the raw text of the pad
99
127
// remove newline character from the end of the string.
100
- var beat = value . atext . text . slice ( 0 , - 1 ) ;
101
-
102
-
128
+ var text = value . atext . text . slice ( 0 , - 1 ) ;
129
+ var beat = text ;
130
+
103
131
// if .js file beautify
104
132
if ( ext == 'js' ) {
105
- if ( msg == msg_push ) beat = beautify ( beat , {
106
- indent_size : 4
107
- } ) ;
108
-
109
-
110
- //padMessageHandler.updatePadClients(value, cb);
111
-
112
- if ( ! jshint ( beat ) ) {
133
+ //check for missing semiciolons forst
134
+ if ( msg == msg_push && ! jshint ( text ) ) {
113
135
114
136
// determine if semicolons should be added at the end of line
115
- check = function ( errors , line , length ) {
137
+ var check = function ( errors , line , length ) {
116
138
var count = 0 ;
117
139
var chpos = 0 ;
118
140
errors . forEach ( function ( err ) {
@@ -121,18 +143,18 @@ exports.handleMessage = function(hook_name, context, callback) {
121
143
if ( err . line == line + 1 ) {
122
144
count ++ ;
123
145
if ( err . code == 'W033' ) chpos = err . character ;
146
+ // TODO ... maybe it should abort the whole process if there was another error
124
147
}
125
148
}
126
149
} ) ;
127
150
128
-
129
- if ( count == 1 && chpos !== 0 && length < chpos ) return true ;
151
+ if ( count === 1 && chpos !== 0 && length < chpos ) return true ;
130
152
else return false ;
131
153
} ;
132
154
133
155
134
156
// Add the semicolons
135
- var lines = beat . split ( '\n' ) ;
157
+ var lines = text . split ( '\n' ) ;
136
158
beat = '' ;
137
159
138
160
for ( var i = 0 ; i < lines . length ; i ++ ) {
@@ -142,9 +164,16 @@ exports.handleMessage = function(hook_name, context, callback) {
142
164
else beat += lines [ i ] + '\n' ;
143
165
}
144
166
}
167
+
168
+ // if push and jshint said ok, then beautify text for real
169
+ if ( msg == msg_push && jshint ( text ) ) beat = beautify ( text , {
170
+ indent_size : 4
171
+ } ) ;
172
+
145
173
// re-check the new beautified code
146
174
if ( ! jshint ( beat ) ) {
147
175
176
+ // still has errors, we put them to the console log on the server
148
177
jshint . errors . forEach ( function ( err ) {
149
178
if ( err ) {
150
179
console . info ( " ! " + padid + ":" + err . line + ":" + err . character + " " + err . reason + "|" + err . evidence ) ;
@@ -153,6 +182,7 @@ exports.handleMessage = function(hook_name, context, callback) {
153
182
}
154
183
} ) ;
155
184
185
+ all_is_ok = false ;
156
186
var err_msg = {
157
187
type : 'COLLABROOM' ,
158
188
data : {
@@ -166,46 +196,33 @@ exports.handleMessage = function(hook_name, context, callback) {
166
196
} ;
167
197
padMessageHandler . handleCustomObjectMessage ( err_msg , undefined , cb ) ;
168
198
169
- } else {
170
- var ok_msg = {
171
- type : 'COLLABROOM' ,
172
- data : {
173
- type : "CUSTOM" ,
174
- payload : {
175
- padId : padid ,
176
- from : "jshint" ,
177
- errors : null
178
- }
179
- }
180
- } ;
181
- padMessageHandler . handleCustomObjectMessage ( ok_msg , undefined , cb ) ;
182
- }
183
-
184
- if ( msg == msg_push ) value . setText ( beat ) ;
185
- padMessageHandler . updatePadClients ( value , cb ) ;
199
+ }
186
200
}
187
201
188
202
// if .css file beautify
189
203
if ( ext == 'css' && msg == msg_push ) {
190
- beat = beautify_css ( beat , {
204
+ beat = beautify_css ( text , {
191
205
indent_size : 4
192
206
} ) ;
193
- value . setText ( beat ) ;
194
- padMessageHandler . updatePadClients ( value , cb ) ;
195
207
}
196
208
197
209
// if .html file beautify
198
210
if ( ext == 'xml' && msg == msg_push ) {
199
- beat = beautify_html ( beat , {
211
+ beat = beautify_html ( text , {
200
212
indent_size : 4
201
213
} ) ;
214
+ }
215
+
216
+ // text and beat may be different, depending on jsHint and the beutifier
217
+ if ( text !== beat ) {
202
218
value . setText ( beat ) ;
203
219
padMessageHandler . updatePadClients ( value , cb ) ;
204
220
}
205
221
206
222
// WRITE to the FILE
207
223
fs . writeFile ( path , beat , function ( err ) {
208
224
if ( err ) {
225
+ all_is_ok = false ;
209
226
console . log ( "Failed to write text to " + path ) ;
210
227
var err_msg = {
211
228
type : 'COLLABROOM' ,
@@ -221,42 +238,31 @@ exports.handleMessage = function(hook_name, context, callback) {
221
238
padMessageHandler . handleCustomObjectMessage ( err_msg , undefined , cb ) ;
222
239
} else {
223
240
console . log ( "Wrote pad contents to " + path ) ;
224
- var ok_msg = {
225
- type : 'COLLABROOM' ,
226
- data : {
227
- type : "CUSTOM" ,
228
- payload : {
229
- padId : padid ,
230
- from : "fs" ,
231
- errors : null
232
- }
233
- }
234
- } ;
235
- padMessageHandler . handleCustomObjectMessage ( ok_msg , undefined , cb ) ;
241
+
236
242
// if push_action is defined in settings.json, it will run here, use it for git/svn/hg ... or whatever.
237
243
if ( settings . ep_codepad && msg == msg_push ) {
238
244
if ( settings . ep_codepad . push_action ) {
239
245
exec ( settings . ep_codepad . push_action , function ( exec_err , stdout , stderr ) {
246
+ if ( stdout ) console . log ( "[codepad] push_action stdout: " + stdout ) ;
247
+ if ( stderr ) console . log ( "[codepad] push_action stderr: " + stderr ) ;
240
248
if ( exec_err ) {
241
- console . log ( 'codepad-push- error: ' , exec_err ) ;
249
+ console . log ( '[ codepad] push_action ! error: ' , exec_err ) ;
242
250
var err_msg = {
243
251
type : 'COLLABROOM' ,
244
252
data : {
245
253
type : "CUSTOM" ,
246
254
payload : {
247
255
padId : padid ,
248
- from : "fs " ,
256
+ from : "exec " ,
249
257
errors : exec_err
250
258
}
251
259
}
252
260
} ;
253
261
padMessageHandler . handleCustomObjectMessage ( err_msg , undefined , cb ) ;
254
- }
255
- if ( stdout ) console . log ( "codepad-push-stdout: " + stdout ) ;
256
- if ( stderr ) console . log ( "codepad-push-stderr: " + stderr ) ;
262
+ } else if ( all_is_ok ) send_ok ( padid ) ;
257
263
} ) ;
258
- }
259
- }
264
+ } else if ( all_is_ok ) send_ok ( padid ) ;
265
+ } else if ( all_is_ok ) send_ok ( padid ) ;
260
266
}
261
267
} ) ;
262
268
} ) ;
@@ -278,4 +284,5 @@ exports.handleMessage = function(hook_name, context, callback) {
278
284
"scope": "(main)",
279
285
"reason": "Missing semicolon."
280
286
281
- *****/
287
+ *****/
288
+
0 commit comments