Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Prettier #1

Merged
merged 1 commit into from Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 14 additions & 11 deletions index.js
@@ -1,26 +1,29 @@
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) {
var contents, extension;

// 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);

Expand All @@ -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];
}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -17,6 +17,7 @@
"devDependencies": {
"gulp-util": "3.0.x",
"mocha": "*",
"prettier": "^1.13.5",
"should": "*"
},
"engines": {
Expand Down
118 changes: 61 additions & 57 deletions 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);

});

});
});