forked from jmjuanes/electron-ejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (130 loc) · 3.71 KB
/
index.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
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
//Import dependencies
var ejs = require('ejs');
var electron = require('electron');
var fs = require('fs');
var mime = require('mime');
var path = require('path');
var pStat = require('pstat');
var url = require('url');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
//Import app
var app = electron.app;
//Main function
var ElectronEjs = function(data, options)
{
//Get this
var self = this;
//Check for undefined emit
if(typeof self.emit === 'undefined')
{
//Throw error
throw Error("This initialization is obsolete! Please look documentation for more informations.");
//Exit
return;
}
//Check data
if(typeof data === 'undefined'){ var data = {}; }
//Check options
if(typeof options === 'undefined'){ var options = {}; }
//App ready event
app.on('ready', function()
{
//Import protocol
var protocol = electron.protocol;
//Intercept the file protocol
protocol.interceptBufferProtocol('file', function(request, callback)
{
//Get the file
var file = ParsePath(request.url);
//Get the file extension
var extension = path.extname(file);
//Check if path exists and is a file
pStat.isFile(file, function(exists)
{
//Check if file doesn't exists
if(exists === false)
{
//Emit error
// self.emit("error", "File not found!");
//Return file not found
return callback(-6);
}
//Check the extension
if(extension === '.ejs')
{
//Add the path to options
options.filename = file;
//Render template function
var renderTemplate = function()
{
//Render the full file
ejs.renderFile(file, data, options, function(err, content)
{
//Check for error
if(err)
{
//Emit error
self.emit('error', err);
//Unexpected error
return callback(-9);
}
//Return the callback
return callback({ data: new Buffer(content), mimeType: 'text/html' });
});
};
//Check for event before render
if(!self.emit('before-render', file, data, options, renderTemplate))
{
//Render the template
renderTemplate();
}
}
else
{
//Read the file content
fs.readFile(file, function(err, content)
{
//Check for error
if(err)
{
//Emit error
self.emit('error', err);
//Generic failure
return callback(-2);
}
//Return the callback with the file content
return callback({ data: content, mimeType: mime.lookup(extension) });
});
}
});
});
});
//End ElectronEjs function
}
//Function to parse the path
function ParsePath(u)
{
//Parse the url
var p = url.parse(u);
//Get the path name
var pname = p.pathname;
//Check for Windows
if(process.platform === 'win32')
{
//Remove the first / from the path
//https://github.com/jmjuanes/electron-ejs/pull/4#issuecomment-219254028
pname = pname.substr(1);
}
//Sanitize URL. Spaces turn into `%20` symbols in the path and
//throws a `File Not Found Event`. This fix allows folder paths to have
//spaces in the folder name.
//https://github.com/jmjuanes/electron-ejs/pull/9
pname = pname.replace(/\s/g, ' ').replace(/%20/g, ' ');
//Return the path name
return pname;
}
//Inherit EventEmmiter to ElectronEjs
util.inherits(ElectronEjs, EventEmitter);
//Exports to node
module.exports = ElectronEjs;