1 | var path = require('path')
|
2 | var fs = require('fs-extra')
|
3 | var terraform = require('terraform')
|
4 | var async = require('async')
|
5 | var connect = require('connect')
|
6 | var mime = require('mime-types')
|
7 | var helpers = require('./helpers')
|
8 | var middleware = require('./middleware')
|
9 | var pkg = require('../package.json')
|
10 | var url = require("url")
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | exports.server = function(dirPath, options, callback){
|
22 | var app = connect()
|
23 | app.use(middleware.regProjectFinder(dirPath))
|
24 | app.use(middleware.setup)
|
25 | app.use(middleware.basicAuth)
|
26 | app.use(middleware.underscore)
|
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 | app.use(middleware.mwl)
|
61 | app.use(middleware.static)
|
62 | app.use(middleware.poly)
|
63 | app.use(middleware.process)
|
64 | app.use(middleware.fallback2)
|
65 |
|
66 | return app
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | exports.multihost = function(dirPath, options, callback){
|
83 | var app = connect()
|
84 | app.use(middleware.notMultihostURL)
|
85 | app.use(middleware.index(dirPath))
|
86 | app.use(middleware.hostProjectFinder(dirPath))
|
87 | app.use(middleware.setup)
|
88 | app.use(middleware.basicAuth)
|
89 | app.use(middleware.underscore)
|
90 | app.use(middleware.mwl)
|
91 | app.use(middleware.static)
|
92 | app.use(middleware.poly)
|
93 | app.use(middleware.process)
|
94 | app.use(middleware.fallback2)
|
95 | app.listen(options.port || 9000, callback)
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | exports.mount = function(mountPoint, root){
|
106 |
|
107 | if(!root){
|
108 | root = mountPoint
|
109 | mountPoint = null
|
110 | }else{
|
111 | var rx = new RegExp("^" + mountPoint)
|
112 | }
|
113 |
|
114 | var finder = middleware.regProjectFinder(root)
|
115 |
|
116 | return function(req, rsp, next){
|
117 |
|
118 | if(rx){
|
119 | if(!req.url.match(rx)) return next()
|
120 | var originalUrl = req.url
|
121 | req.url = req.url.replace(rx, "/")
|
122 | }
|
123 |
|
124 | finder(req, rsp, function(){
|
125 | middleware.setup(req, rsp, function(){
|
126 | middleware.static(req, rsp, function(){
|
127 | middleware.poly(req, rsp, function(){
|
128 | middleware.process(req, rsp, function(){
|
129 | if(originalUrl) req.url = originalUrl
|
130 | next()
|
131 | })
|
132 | })
|
133 | })
|
134 | })
|
135 | })
|
136 | }
|
137 | }
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | exports.pipeline = function(root){
|
148 | console.log("Deprecated, please use MOUNT instead, this will be removed in a future version.");
|
149 | var publicPath = path.resolve(root)
|
150 | var terra = terraform.root(publicPath)
|
151 |
|
152 | return function(req, rsp, next){
|
153 | var normalizedPath = helpers.normalizeUrl(req.url)
|
154 | var priorityList = terraform.helpers.buildPriorityList(normalizedPath)
|
155 | var sourceFile = terraform.helpers.findFirstFile(publicPath, priorityList)
|
156 |
|
157 | if(!sourceFile) return next()
|
158 |
|
159 | terra.render(sourceFile, function(error, body){
|
160 | if(error) return next(error)
|
161 | if(!body) return next()
|
162 |
|
163 | var outputType = terraform.helpers.outputType(sourceFile)
|
164 | var mimeType = helpers.mimeType(outputType)
|
165 | var charset = mime.charsets.lookup(mimeType)
|
166 | rsp.statusCode = 200
|
167 | rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''))
|
168 | rsp.setHeader('Content-Length', Buffer.byteLength(body, charset));
|
169 | rsp.end(body)
|
170 | })
|
171 |
|
172 | }
|
173 |
|
174 | }
|
175 |
|
176 | exports.pkg = pkg
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | exports.middleware = middleware;
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | exports.compile = function(projectPath, outputPath, callback){
|
195 |
|
196 | |
197 |
|
198 |
|
199 |
|
200 | if(!callback && typeof outputPath === "function"){
|
201 | callback = outputPath
|
202 | outputPath = "www"
|
203 | }
|
204 |
|
205 | if(!outputPath){
|
206 | outputPath = "www"
|
207 | }
|
208 |
|
209 |
|
210 | |
211 |
|
212 |
|
213 |
|
214 | try{
|
215 | outputPath = path.resolve(projectPath, outputPath)
|
216 | var setup = helpers.setup(projectPath, "production")
|
217 | var terra = terraform.root(setup.publicPath, setup.config.globals)
|
218 | }catch(err){
|
219 | return callback(err)
|
220 | }
|
221 |
|
222 |
|
223 | |
224 |
|
225 |
|
226 |
|
227 |
|
228 | if(!helpers.willAllow(projectPath, outputPath)){
|
229 | return callback({
|
230 | type: "Invalid Output Path",
|
231 | message: "Output path cannot be greater then one level up from project path and must be in directory starting with `_` (underscore).",
|
232 | projectPath: projectPath,
|
233 | outputPath: outputPath
|
234 | })
|
235 | }
|
236 |
|
237 |
|
238 | |
239 |
|
240 |
|
241 |
|
242 | var compileFile = function(file, done){
|
243 | process.nextTick(function () {
|
244 | terra.render(file, function(error, body){
|
245 | if(error){
|
246 | done(error)
|
247 | }else{
|
248 | if(body){
|
249 | var dest = path.resolve(outputPath, terraform.helpers.outputPath(file))
|
250 | fs.mkdirp(path.dirname(dest), function(err){
|
251 | fs.writeFile(dest, body, done)
|
252 | })
|
253 | }else{
|
254 | if (file === "app.jsx"){
|
255 | var results = esbuild.buildSync({
|
256 | absWorkingDir: setup.publicPath,
|
257 | entryPoints: [setup.publicPath + '/app.jsx'],
|
258 | outfile: 'app.js',
|
259 | bundle: true,
|
260 | write: false,
|
261 | plugins: [],
|
262 | })
|
263 | fs.writeFile([outputPath, "app.js"].join(path.sep), results.outputFiles[0]["text"], done)
|
264 | }else if (file === "bundle.cjs"){
|
265 | var results = esbuild.buildSync({
|
266 | absWorkingDir: setup.publicPath,
|
267 | entryPoints: [setup.publicPath + '/bundle.cjs'],
|
268 | outfile: 'bundle.js',
|
269 | bundle: true,
|
270 | write: false,
|
271 | plugins: [],
|
272 | })
|
273 | fs.writeFile([outputPath, "bundle.js"].join(path.sep), results.outputFiles[0]["text"], done)
|
274 | } else {
|
275 | done()
|
276 | }
|
277 | }
|
278 | }
|
279 | })
|
280 | })
|
281 | }
|
282 |
|
283 |
|
284 | |
285 |
|
286 |
|
287 |
|
288 |
|
289 | var copyFile = function(file, done){
|
290 | var ext = path.extname(file)
|
291 | if(!terraform.helpers.shouldIgnore(file) && [".jsx", ".jade", ".ejs", ".md", ".styl", ".less", ".scss", ".sass", ".coffee", ".cjs"].indexOf(ext) === -1){
|
292 | var localPath = path.resolve(outputPath, file)
|
293 | fs.mkdirp(path.dirname(localPath), function(err){
|
294 | fs.copy(path.resolve(setup.publicPath, file), localPath, done)
|
295 | })
|
296 | }else{
|
297 | done()
|
298 | }
|
299 | }
|
300 |
|
301 | |
302 |
|
303 |
|
304 |
|
305 | helpers.prime(outputPath, { ignore: projectPath }, function(err){
|
306 | if(err) console.log(err)
|
307 |
|
308 | helpers.ls(setup.publicPath, function(err, results){
|
309 | async.each(results, compileFile, function(err){
|
310 | if(err){
|
311 | callback(err)
|
312 | }else{
|
313 | async.each(results, copyFile, function(err){
|
314 | setup.config['harp_version'] = pkg.version
|
315 | delete setup.config.globals
|
316 | callback(null, setup.config)
|
317 | })
|
318 | }
|
319 | })
|
320 | })
|
321 | })
|
322 |
|
323 | }
|