diff --git a/index.js b/index.js index 3eee8b7..5fc3b52 100755 --- a/index.js +++ b/index.js @@ -1,10 +1,10 @@ -var _ = require('lodash'); -var map = require('map-stream'); -var pp = require('preprocess'); -var path = require('path'); +var _ = require("lodash"); +var map = require("map-stream"); +var pp = require("preprocess"); +var path = require("path"); -module.exports = function (options) { - var opts = _.merge({}, options); +module.exports = function(options) { + var opts = _.merge({}, options); var context = _.merge({}, process.env, opts.context); function ppStream(file, callback) { @@ -12,15 +12,18 @@ module.exports = function (options) { // TODO: support streaming files if (file.isNull()) return callback(null, file); // pass along - if (file.isStream()) return callback(new Error("gulp-preprocess: Streaming not supported")); + if (file.isStream()) + return callback(new Error("gulp-preprocess: Streaming not supported")); context.src = file.path; context.srcDir = opts.includeBase || path.dirname(file.path); - context.NODE_ENV = context.NODE_ENV || 'development'; + context.NODE_ENV = context.NODE_ENV || "development"; - extension = _.isEmpty(opts.extension) ? getExtension(context.src) : opts.extension; + extension = _.isEmpty(opts.extension) + ? getExtension(context.src) + : opts.extension; - contents = file.contents.toString('utf8'); + contents = file.contents.toString("utf8"); contents = pp.preprocess(contents, context, extension); file.contents = new Buffer(contents); @@ -31,6 +34,6 @@ module.exports = function (options) { }; function getExtension(filename) { - var ext = path.extname(filename||'').split('.'); + var ext = path.extname(filename || "").split("."); return ext[ext.length - 1]; } diff --git a/package-lock.json b/package-lock.json index c28b078..deac846 100644 --- a/package-lock.json +++ b/package-lock.json @@ -524,6 +524,12 @@ "xregexp": "3.1.0" } }, + "prettier": { + "version": "1.13.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.13.5.tgz", + "integrity": "sha512-4M90mfvLz6yRf2Dhzd+xPIE6b4xkI8nHMJhsSm9IlfG17g6wujrrm7+H1X8x52tC4cSNm6HmuhCUSNe6Hd5wfw==", + "dev": true + }, "process-nextick-args": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", diff --git a/package.json b/package.json index af3e39b..71775c3 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "devDependencies": { "gulp-util": "3.0.x", "mocha": "*", + "prettier": "^1.13.5", "should": "*" }, "engines": { diff --git a/test/main.js b/test/main.js index 794d1bb..02c709f 100755 --- a/test/main.js +++ b/test/main.js @@ -1,140 +1,144 @@ -var should = require('should'); -var gutil = require('gulp-util'); -var preprocess = require('../'); -var fs = require('fs'); +var should = require("should"); +var gutil = require("gulp-util"); +var preprocess = require("../"); +var fs = require("fs"); -require('mocha'); +require("mocha"); function fixtureFile(fileName) { return new gutil.File({ - base: 'test/fixtures', - cwd: 'test/', - path: 'test/fixtures/' + fileName, - contents: fs.readFileSync('test/fixtures/' + fileName) + base: "test/fixtures", + cwd: "test/", + path: "test/fixtures/" + fileName, + contents: fs.readFileSync("test/fixtures/" + fileName) }); -}; +} -process.env['FAKEHOME'] = '/Users/jas'; +process.env["FAKEHOME"] = "/Users/jas"; -describe('gulp-preprocess', function(){ - describe('preprocess()', function(){ - - it('should preprocess html', function(done){ +describe("gulp-preprocess", function() { + describe("preprocess()", function() { + it("should preprocess html", function(done) { var stream = preprocess({ context: { - firstOption: 'bar', - secondOption: 'foo' + firstOption: "bar", + secondOption: "foo" } }); - var fakeFile = fixtureFile('test.html'); + var fakeFile = fixtureFile("test.html"); - stream.once('data', function(newFile){ + stream.once("data", function(newFile) { should.exist(newFile); should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/test.html', 'utf8')); + String(newFile.contents).should.equal( + fs.readFileSync("test/expected/test.html", "utf8") + ); done(); }); stream.write(fakeFile); - }); - it('should preprocess javascript', function(done){ + it("should preprocess javascript", function(done) { var stream = preprocess({ context: { - firstOption: 'bar', - secondOption: 'foo' + firstOption: "bar", + secondOption: "foo" } }); - var fakeFile = fixtureFile('test.js'); + var fakeFile = fixtureFile("test.js"); - stream.once('data', function(newFile){ + stream.once("data", function(newFile) { should.exist(newFile); should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/test.js', 'utf8')); + String(newFile.contents).should.equal( + fs.readFileSync("test/expected/test.js", "utf8") + ); done(); }); stream.write(fakeFile); - }); - it('should preprocess coffeescript', function(done){ + it("should preprocess coffeescript", function(done) { var stream = preprocess({ context: { - firstOption: 'bar', - secondOption: 'foo' + firstOption: "bar", + secondOption: "foo" } }); - var fakeFile = fixtureFile('test.coffee'); + var fakeFile = fixtureFile("test.coffee"); - stream.once('data', function(newFile){ + stream.once("data", function(newFile) { should.exist(newFile); should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/test.coffee', 'utf8')); + String(newFile.contents).should.equal( + fs.readFileSync("test/expected/test.coffee", "utf8") + ); done(); }); stream.write(fakeFile); - }); - it('should preprocess without options object', function(done){ + it("should preprocess without options object", function(done) { var stream = preprocess(); - var fakeFile = fixtureFile('test.coffee'); + var fakeFile = fixtureFile("test.coffee"); - stream.once('data', function(newFile){ + stream.once("data", function(newFile) { should.exist(newFile); should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/test.coffee', 'utf8')); + String(newFile.contents).should.equal( + fs.readFileSync("test/expected/test.coffee", "utf8") + ); done(); }); stream.write(fakeFile); - }); - it('should preprocess html with custom base', function(done){ + it("should preprocess html with custom base", function(done) { var stream = preprocess({ - includeBase: 'test/fixtures/base', + includeBase: "test/fixtures/base", context: { - firstOption: 'bar', - secondOption: 'foo' + firstOption: "bar", + secondOption: "foo" } }); - var fakeFile = fixtureFile('test.html'); + var fakeFile = fixtureFile("test.html"); - stream.once('data', function(newFile){ + stream.once("data", function(newFile) { should.exist(newFile); should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/test-base.html', 'utf8')); + String(newFile.contents).should.equal( + fs.readFileSync("test/expected/test-base.html", "utf8") + ); done(); }); stream.write(fakeFile); - }); - it('should allow custom extension', function(done){ + it("should allow custom extension", function(done) { var stream = preprocess({ - extension: 'html', + extension: "html", context: { - firstOption: 'bar', - secondOption: 'foo' + firstOption: "bar", + secondOption: "foo" } }); - var fakeFile = fixtureFile('test.php'); + var fakeFile = fixtureFile("test.php"); - stream.once('data', function(newFile){ + stream.once("data", function(newFile) { should.exist(newFile); should.exist(newFile.contents); - String(newFile.contents).should.equal(fs.readFileSync('test/expected/test.html', 'utf8')); + String(newFile.contents).should.equal( + fs.readFileSync("test/expected/test.html", "utf8") + ); done(); }); stream.write(fakeFile); - }); - }); });