-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.js
106 lines (86 loc) · 3.34 KB
/
middleware.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
/*
* cartero-express-hook v0.1.1
* https://github.com/rotundasoftware/cartero-express-hook
*
* Copyright (c) 2013 Rotunda Software, LLC
* Licensed under the MIT license.
*
* A Node.js / Express Hook for the Cartero asset manager, implemented as Express middleware.
*
*/
var fs = require( "fs" ),
_ = require( "underscore" ),
path = require( "path" ),
async = require( "async" ),
View = require('express/lib/view');
module.exports = function( projectDir ) {
var carteroJson;
// cache the cartero.json file, which lists the required assets for each view template.
try {
carteroJson = JSON.parse( fs.readFileSync( path.join( projectDir, "cartero.json" ) ).toString() );
}
catch( err ) {
throw new Error( "Error while reading the cartero.json file. Have you run the grunt cartero task yet?" + err.stack );
}
return function( req, res, next ) {
var oldRender = res.render;
// for each request, wrap the render function so that we can execute our own code
// first to populate the `cartero_js`, `cartero_css`, and `cartero_tmpl` variables.
res.render = function( name, options ) {
var _arguments = arguments;
var parcelName;
if( options && options.cartero_parcel ) parcelName = options.cartero_parcel;
else {
var app = req.app;
var absolutePath;
var existsSync = fs.existsSync ? fs.existsSync : path.existsSync;
// try to find the absolute path of the template by resolving it against the views folder
absolutePath = path.resolve( app.get( "views" ), name );
if( ! existsSync( absolutePath ) ) {
// if that doesn't work, resolve it using same method as app.render, which adds
// extensions based on the view engine being used, etc.
var view = new View( name, {
defaultEngine: app.get( "view engine" ),
root: app.get( "views" ),
engines: app.engines
} );
absolutePath = view.path;
}
parcelName = path.relative( projectDir, absolutePath );
}
var parcelMetadata = carteroJson.parcels[ parcelName ];
if( ! parcelMetadata ) return next( new Error( "Could not find parcel \"" + parcelName + "\" in parcel map." ) );
res.locals.cartero_js = _.map( parcelMetadata.js, function( fileName ) {
// don't change file path if its a CDN file
if ( ! /https?:\/\//.test( fileName ) )
fileName = carteroJson.publicUrl + fileName.replace( carteroJson.publicDir, "" );
return "<script type='text/javascript' src='" + fileName + "'></script>";
} ).join( "" );
res.locals.cartero_css = _.map( parcelMetadata.css, function( fileName ) {
// don't change file path if its a CDN file
if ( ! /https?:\/\//.test( fileName ) )
fileName = carteroJson.publicUrl + fileName.replace( carteroJson.publicDir, "" );
return "<link rel='stylesheet' href='" + fileName + "'></link>";
} ).join( "" );
var tmplContents = "";
async.each( parcelMetadata.tmpl, function( fileName, callback ) {
fs.readFile( path.join( projectDir, fileName ), function( err, data ) {
if( err ) {
callback( err );
return;
}
tmplContents += data.toString();
callback();
} );
},
function( err ) {
if( err ) {
console.log( "ERROR: Exception while reading tmpl files to inject into response: " + err );
}
res.locals.cartero_tmpl = tmplContents;
oldRender.apply( res, _arguments );
} );
};
next();
};
};