Skip to content

Commit

Permalink
Syntax changes (#36)
Browse files Browse the repository at this point in the history
* prettier changes
* upgrade code style
* add .node-version
* remove node v6 and v8
* code clean up
  • Loading branch information
vernak2539 authored Jul 24, 2019
1 parent 7c57f9b commit 66f5ddf
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 281 deletions.
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
>=10
2 changes: 0 additions & 2 deletions .travis.yml
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
Expand Down
59 changes: 31 additions & 28 deletions gulpfile.js
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"
})
);
});
111 changes: 58 additions & 53 deletions lib/helpers.js
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 };
}
};
152 changes: 74 additions & 78 deletions lib/less-middleware.js
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);
}
);
};
};
32 changes: 16 additions & 16 deletions lib/less-streaming-parser.js
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);
});
};
Loading

0 comments on commit 66f5ddf

Please sign in to comment.