Skip to content

Commit

Permalink
fix(preprocess): set correct extension for the preprocessed path
Browse files Browse the repository at this point in the history
Closes #843
  • Loading branch information
vojtajina committed Dec 1, 2013
1 parent 9dbadb4 commit c9a64d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
9 changes: 4 additions & 5 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
var path = require('path');
var fs = require('graceful-fs');
var crypto = require('crypto');
var mm = require('minimatch');

var log = require('./logger').create('preprocess');

// TODO(vojta): extract get/create temp dir somewhere else (use the same for launchers etc)
var TMP = process.env.TMPDIR || process.env.TMP || process.env.TEMP || '/tmp';

var sha1 = function(data) {
var hash = crypto.createHash('sha1');
Expand All @@ -20,6 +23,7 @@ var createPreprocessor = function(config, basePath, injector) {
var preprocessors = [];
var nextPreprocessor = function(content) {
if (!preprocessors.length) {
file.contentPath = path.normalize(TMP + '/' + sha1(file.path) + path.extname(file.path));
return fs.writeFile(file.contentPath, content, function() {
done();
});
Expand Down Expand Up @@ -56,11 +60,6 @@ var createPreprocessor = function(config, basePath, injector) {

if (preprocessors.length) {
return fs.readFile(file.originalPath, function(err, buffer) {
// TODO(vojta): extract get/create temp dir somewhere else (use the same for launchers etc)
var env = process.env;
var tmp = env.TMPDIR || env.TMP || env.TEMP || '/tmp';
file.contentPath = tmp + '/' + sha1(file.originalPath) + '.js';

nextPreprocessor(buffer.toString());
});
}
Expand Down
33 changes: 28 additions & 5 deletions test/unit/preprocessor.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ describe 'preprocessor', ->
mockFs = mocks.fs.create
some:
'a.js': mocks.fs.file 0, 'content'
'style.less': mocks.fs.file 0, 'whatever'
temp: {} # so that we can write preprocessed content here


mocks_ =
'graceful-fs': mockFs
minimatch: require 'minimatch'

m = mocks.loadFile __dirname + '/../../lib/preprocessor.js', mocks_
globals_ =
process:
env: TMPDIR: '/temp'
nextTick: process.nextTick

m = mocks.loadFile __dirname + '/../../lib/preprocessor.js', mocks_, globals_


it 'should preprocess matching file', (done) ->
fakePreprocessor = sinon.spy (content, file, done) ->
file.path = file.path + '-preprocessed'
file.contentPath = '/some/new.js'
done 'new-content'

injector = new di.Injector [{'preprocessor:fake': ['factory', -> fakePreprocessor]}]
Expand All @@ -33,7 +40,7 @@ describe 'preprocessor', ->
pp file, ->
expect(fakePreprocessor).to.have.been.called
expect(file.path).to.equal 'path-preprocessed'
expect(mockFs.readFileSync('/some/new.js').toString()).to.equal 'new-content'
expect(mockFs.readFileSync(file.contentPath).toString()).to.equal 'new-content'
done()


Expand All @@ -53,7 +60,6 @@ describe 'preprocessor', ->

it 'should apply all preprocessors', (done) ->
fakePreprocessor1 = sinon.spy (content, file, done) ->
file.contentPath = '/some/new.js'
file.path = file.path + '-p1'
done content + '-c1'

Expand All @@ -74,5 +80,22 @@ describe 'preprocessor', ->
expect(fakePreprocessor1).to.have.been.calledOnce
expect(fakePreprocessor2).to.have.been.calledOnce
expect(file.path).to.equal 'path-p1-p2'
expect(mockFs.readFileSync('/some/new.js').toString()).to.equal 'content-c1-c2'
expect(mockFs.readFileSync(file.contentPath).toString()).to.equal 'content-c1-c2'
done()


it 'should keep processed extension', (done) ->
fakePreprocessor = sinon.spy (content, file, done) ->
file.path = file.path.replace '.less', '.css'
done content

injector = new di.Injector [{
'preprocessor:less': ['factory', -> fakePreprocessor]
}]

pp = m.createPreprocessor {'**/*.less': ['less']}, null, injector
file = {originalPath: '/some/style.less', path: '/some/style.less'}

pp file, ->
expect(file.contentPath).to.match /\.css$/
done()

0 comments on commit c9a64d2

Please sign in to comment.