-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* prettier changes * upgrade code style * add .node-version * remove node v6 and v8 * code clean up
- Loading branch information
1 parent
7c57f9b
commit 66f5ddf
Showing
9 changed files
with
287 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
>=10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '8' | ||
- '6' | ||
- '10' | ||
- stable | ||
sudo: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,43 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
var gulp = require( 'gulp' ); | ||
var jshint = require( 'gulp-jshint' ); | ||
var complexity = require( 'gulp-complexity' ); | ||
var mocha = require( 'gulp-mocha' ); | ||
var gulp = require("gulp"); | ||
var jshint = require("gulp-jshint"); | ||
var complexity = require("gulp-complexity"); | ||
var mocha = require("gulp-mocha"); | ||
|
||
var paths = { | ||
lib: './lib/**/*.js' | ||
, gulp: './gulpfile.js' | ||
, testSpec: './test/spec.js' | ||
, tests: './test/**/*.js' | ||
lib: "./lib/**/*.js", | ||
gulp: "./gulpfile.js", | ||
testSpec: "./test/spec.js", | ||
tests: "./test/**/*.js" | ||
}; | ||
|
||
gulp.task( 'default', [ 'test' ] ); | ||
gulp.task( 'test', [ 'jshint', 'complexity', 'mocha' ] ); | ||
gulp.task("default", ["test"]); | ||
gulp.task("test", ["jshint", "complexity", "mocha"]); | ||
|
||
gulp.task( 'jshint', function() { | ||
return gulp.src([ paths.lib, paths.gulp, paths.tests ]) | ||
.pipe( jshint() ) | ||
.pipe( jshint.reporter( 'jshint-stylish' ) ) | ||
.pipe( jshint.reporter( 'fail' ) ); | ||
gulp.task("jshint", function() { | ||
return gulp | ||
.src([paths.lib, paths.gulp, paths.tests]) | ||
.pipe(jshint()) | ||
.pipe(jshint.reporter("jshint-stylish")) | ||
.pipe(jshint.reporter("fail")); | ||
}); | ||
|
||
gulp.task( 'complexity', function() { | ||
return gulp.src( [ paths.lib, '!./lib/helpers.js' ] ) | ||
.pipe( complexity({ | ||
cyclomatic: 10 // recommendation 10 | ||
, halstead: 12 // no recommendation | ||
, maintainability: 100 // recommendation 65 | ||
}) ); | ||
gulp.task("complexity", function() { | ||
return gulp.src([paths.lib, "!./lib/helpers.js"]).pipe( | ||
complexity({ | ||
cyclomatic: 10, // recommendation 10 | ||
halstead: 12, // no recommendation | ||
maintainability: 100 // recommendation 65 | ||
}) | ||
); | ||
}); | ||
|
||
// only doing this at the end so the logs don't get messed up by other tasks going | ||
gulp.task( 'mocha', [ 'jshint', 'complexity' ], function() { | ||
return gulp.src( paths.testSpec, { read: false }) | ||
.pipe( mocha({ | ||
reporter: 'spec' | ||
}) ); | ||
gulp.task("mocha", ["jshint", "complexity"], function() { | ||
return gulp.src(paths.testSpec, { read: false }).pipe( | ||
mocha({ | ||
reporter: "spec" | ||
}) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,62 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const url = require('url'); | ||
const path = require('path'); | ||
const isPlainObj = require('lodash.isplainobject'); | ||
const url = require("url"); | ||
const path = require("path"); | ||
const isPlainObj = require("lodash.isplainobject"); | ||
|
||
module.exports = { | ||
checkMethodAndFileType: (req, callback) => { | ||
if (req.method.toLowerCase() === 'get' && Boolean(req.url.match(/\/[a-zA-Z_0-9\-\.]+\.css/))) { | ||
callback(null, url.parse(req.url).pathname); | ||
} else { | ||
callback(new Error('File requested was not a CSS file or request method was not GET'), null); | ||
} | ||
}, | ||
pushPath: (array, string) => { | ||
if (array.indexOf(string) === -1) { | ||
array.push(string); | ||
} | ||
}, | ||
processOptions: (config) => { | ||
var cwd = process.cwd(); | ||
var publicDir = null; | ||
var options = {}; | ||
|
||
config = config || './public'; | ||
|
||
if (isPlainObj(config)) { | ||
publicDir = config.publicDir || './public'; | ||
options = config; | ||
} | ||
|
||
if (typeof config === 'string') { | ||
publicDir = config; | ||
} | ||
|
||
if (config && config.publicDir) { | ||
publicDir = config.publicDir; | ||
} | ||
|
||
publicDir = path.join(cwd, publicDir); | ||
|
||
if (Array.isArray(options.paths)) { | ||
options.paths.forEach(function(importPath, index) { | ||
options.paths[index] = path.join(cwd, importPath); | ||
}); | ||
} else { | ||
options.paths = []; | ||
} | ||
|
||
delete options.publicDir; | ||
|
||
return { | ||
options: options, | ||
publicDir: publicDir | ||
}; | ||
} | ||
checkMethodAndFileType: (req, callback) => { | ||
if ( | ||
req.method.toLowerCase() === "get" && | ||
Boolean(req.url.match(/\/[a-zA-Z_0-9\-\.]+\.css/)) | ||
) { | ||
callback(null, url.parse(req.url).pathname); | ||
} else { | ||
callback( | ||
new Error( | ||
"File requested was not a CSS file or request method was not GET" | ||
), | ||
null | ||
); | ||
} | ||
}, | ||
pushPath: (array, string) => { | ||
if (array.indexOf(string) === -1) { | ||
array.push(string); | ||
} | ||
}, | ||
processOptions: config => { | ||
const cwd = process.cwd(); | ||
let publicDir = null; | ||
let options = {}; | ||
|
||
config = config || "./public"; | ||
|
||
if (isPlainObj(config)) { | ||
publicDir = config.publicDir || "./public"; | ||
options = config; | ||
} | ||
|
||
if (typeof config === "string") { | ||
publicDir = config; | ||
} | ||
|
||
if (config && config.publicDir) { | ||
publicDir = config.publicDir; | ||
} | ||
|
||
publicDir = path.join(cwd, publicDir); | ||
|
||
if (Array.isArray(options.paths)) { | ||
options.paths.forEach(function(importPath, index) { | ||
options.paths[index] = path.join(cwd, importPath); | ||
}); | ||
} else { | ||
options.paths = []; | ||
} | ||
|
||
delete options.publicDir; | ||
|
||
return { options, publicDir }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,86 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const async = require('async'); | ||
const lessParser = require('./less-streaming-parser'); | ||
const helpers = require('./helpers'); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const async = require("async"); | ||
const lessParser = require("./less-streaming-parser"); | ||
const helpers = require("./helpers"); | ||
|
||
module.exports = config => { | ||
const newConfig = helpers.processOptions(config); | ||
const publicDir = newConfig.publicDir; | ||
const options = newConfig.options; | ||
const { publicDir, options } = helpers.processOptions(config); | ||
|
||
return (req, res, next) => { | ||
async.waterfall( | ||
[ | ||
// checking the request method and file type of that request | ||
cb => { | ||
helpers.checkMethodAndFileType(req, (err, filePath) => { | ||
if (err) { | ||
cb(err, null); | ||
return; | ||
} | ||
cb(null, filePath); | ||
}); | ||
}, | ||
// checking the request matches an existing CSS file | ||
(filePath, cb) => { | ||
fs.exists(path.join(publicDir, filePath), exists => { | ||
var lessFilePath; | ||
return (req, res, next) => { | ||
async.waterfall( | ||
[ | ||
// checking the request method and file type of that request | ||
cb => { | ||
helpers.checkMethodAndFileType(req, (err, filePath) => { | ||
if (err) { | ||
cb(err, null); | ||
return; | ||
} | ||
cb(null, filePath); | ||
}); | ||
}, | ||
// checking the request matches an existing CSS file | ||
(filePath, cb) => { | ||
fs.exists(path.join(publicDir, filePath), exists => { | ||
// serve CSS file if it exists (let express do the serving) | ||
if (exists) { | ||
cb({ err: "css file exists" }, null); | ||
return; | ||
} | ||
|
||
// serve CSS file if it exists (let express do the serving) | ||
if (exists) { | ||
cb({ err: 'css file exists' }, null); | ||
return; | ||
} | ||
// mimicing a less file at the same path as requested asset | ||
const lessFilePath = path | ||
.join(publicDir, filePath) | ||
.replace(/\.css$/, ".less"); | ||
|
||
// mimicing a less file at the same path as requested asset | ||
lessFilePath = path.join(publicDir, filePath).replace(/\.css$/, '.less'); | ||
cb(null, lessFilePath); | ||
}); | ||
}, | ||
// checking for LESS file with same path as requested assest (with .less extension) | ||
(lessFilePath, cb) => { | ||
fs.exists(lessFilePath, exists => { | ||
if (!exists) { | ||
cb({ err: "LESS file does not exist" }, null); | ||
return; | ||
} | ||
|
||
cb(null, lessFilePath); | ||
}); | ||
}, | ||
// checking for LESS file with same path as requested assest (with .less extension) | ||
(lessFilePath, cb) => { | ||
fs.exists(lessFilePath, (exists) => { | ||
if (!exists) { | ||
cb({ err: 'LESS file does not exist' }, null); | ||
return; | ||
} | ||
cb(null, lessFilePath); | ||
}); | ||
} | ||
// deciding what to do based on functions before. Either return next() and | ||
// pass off to Express, or deliever parsed LESS content | ||
], | ||
(err, lessFilePath) => { | ||
if (err) { | ||
return next(); // pass back to express | ||
} | ||
|
||
cb(null, lessFilePath); | ||
}); | ||
} | ||
// deciding what to do based on functions before. Either return next() and | ||
// pass off to Express, or deliever parsed LESS content | ||
], | ||
(err, lessFilePath) => { | ||
var lessStream, filePathArrayLength; | ||
const lessStream = fs.createReadStream(lessFilePath); | ||
const filePathArrayLength = lessFilePath.split(path.sep).length; | ||
|
||
if (err) { | ||
return next(); // pass back to express | ||
} | ||
lessStream.setEncoding("utf8"); | ||
|
||
lessStream = fs.createReadStream(lessFilePath); | ||
filePathArrayLength = lessFilePath.split(path.sep).length; | ||
helpers.pushPath( | ||
options.paths, | ||
lessFilePath | ||
.split(path.sep) | ||
.slice(0, filePathArrayLength - 1) | ||
.join(path.sep) + path.sep | ||
); | ||
|
||
lessStream.setEncoding('utf8'); | ||
res.setHeader("Content-Type", "text/css; charset=utf-8"); | ||
|
||
helpers.pushPath( | ||
options.paths, | ||
lessFilePath | ||
.split(path.sep) | ||
.slice(0, filePathArrayLength - 1) | ||
.join(path.sep) + path.sep | ||
); | ||
|
||
res.setHeader('Content-Type', 'text/css; charset=utf-8'); | ||
|
||
// pipe parsed content to response | ||
lessStream | ||
.pipe( | ||
lessParser({ | ||
parserOptions: options, | ||
fileToParse: lessFilePath | ||
}) | ||
) | ||
.pipe(res); | ||
} | ||
); | ||
}; | ||
// pipe parsed content to response | ||
lessStream | ||
.pipe( | ||
lessParser({ | ||
parserOptions: options, | ||
fileToParse: lessFilePath | ||
}) | ||
) | ||
.pipe(res); | ||
} | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const through2 = require('through2'); | ||
const less = require('less'); | ||
const through2 = require("through2"); | ||
const less = require("less"); | ||
|
||
module.exports = options => { | ||
return through2.obj((file, enc, cb) => { | ||
less | ||
.render(file, options.parserOptions) | ||
.then(res => cb(null, res.css)) | ||
.catch(err => { | ||
err.lineNumber = err.line; | ||
err.filename = err.filename === 'input' ? options.fileToParse : err.filename; | ||
err.message = 'express-less-middleware:\n\n' + err.toString(); | ||
module.exports = ({ parserOptions, fileToParse }) => { | ||
return through2.obj((file, enc, cb) => { | ||
less | ||
.render(file, parserOptions) | ||
.then(res => cb(null, res.css)) | ||
.catch(err => { | ||
err.lineNumber = err.line; | ||
err.filename = err.filename === "input" ? fileToParse : err.filename; | ||
err.message = "express-less-middleware:\n\n" + err.toString(); | ||
|
||
cb(null, err.message); | ||
}) | ||
.then(null, cb); | ||
}); | ||
cb(null, err.message); | ||
}) | ||
.then(null, cb); | ||
}); | ||
}; |
Oops, something went wrong.