Skip to content

Commit

Permalink
Merge pull request #30 from stelmakh/master
Browse files Browse the repository at this point in the history
Make compilation in onCompile hook instead of compile method
  • Loading branch information
madsflensted committed Feb 23, 2017
2 parents 9cb91a1 + fd31f3c commit 8b062af
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 100 deletions.
144 changes: 73 additions & 71 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,93 +13,95 @@
ElmCompiler.prototype.extension = 'elm';

function ElmCompiler(config) {
var elm_config = {};
elm_config.executablePath = (config.plugins.elmBrunch || {}).executablePath || "";
elm_config.outputFolder = (config.plugins.elmBrunch || {}).outputFolder || path.join(config.paths.public, 'js');
elm_config.outputFile = (config.plugins.elmBrunch || {}).outputFile || null;
elm_config.mainModules = (config.plugins.elmBrunch || {}).mainModules;
elm_config.elmFolder = (config.plugins.elmBrunch || {}).elmFolder || null;
elm_config.makeParameters = (config.plugins.elmBrunch || {}).makeParameters || [];
this.elm_config = elm_config;
const defaults = {executablePath: "", outputFolder: path.join(config.paths.public, 'js'), outputFile: null, elmFolder: null, makeParameters: []};
this.elm_config = Object.assign({}, defaults, config.plugins.elmBrunch);
this.skipedOnInit = {};
this.compiledOnCurrentStep = {};
}

function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

ElmCompiler.prototype.compile = function(data, inFile, callback) {
var elmFolder = this.elm_config.elmFolder;
var file = inFile;
if (elmFolder) {
file = inFile.replace(new RegExp('^' + escapeRegExp(elmFolder) + '[/\\\\]?'), '');
}
var modules = this.elm_config.mainModules || [file];
var compileModules = modules;
var file_is_module_index = modules.indexOf(file);
if (file_is_module_index >= 0) {
compileModules = [modules[file_is_module_index]];
} else {
if (this.skipedOnInit[file]){
} else {
this.skipedOnInit[file] = true;
return callback(null, '');
}
}
var executablePath = this.elm_config.executablePath;
var outputFolder = this.elm_config.outputFolder;
var outputFile = this.elm_config.outputFile;
const makeParameters = this.elm_config.makeParameters;
if (outputFile === null) {
return compileModules.forEach(function(src) {
var moduleName;
moduleName = path.basename(src, '.elm').toLowerCase();
return elmCompile ( executablePath
, src
, elmFolder
, path.join(outputFolder, moduleName + '.js')
, makeParameters
, callback );
});
} else {
return elmCompile ( executablePath
, modules
, elmFolder
, path.join(outputFolder, outputFile)
, makeParameters
, callback );
}
this.compiledOnCurrentStep = {};
return callback(null, '');
};

return ElmCompiler;
ElmCompiler.prototype.onCompile = function(files, assets) {
if(!Array.isArray(files)) files = [files];
const _this = this;

files.forEach(function(item) {
item.sourceFiles.forEach(function(file) {
if(/\.elm$/i.test(file.path)){
var filePath = file.path;

const elmFolder = _this.elm_config.elmFolder;
if (elmFolder) {
filePath = filePath.replace(new RegExp('^' + escapeRegExp(elmFolder) + '[/\\\\]?'), '');
}

const modules = _this.elm_config.mainModules || [filePath];
const file_is_module_index = modules.indexOf(filePath);

var compileModules = modules;

if (file_is_module_index >= 0) {
compileModules = [modules[file_is_module_index]];
} else {
if (!_this.skipedOnInit[filePath]){
_this.skipedOnInit[filePath] = true;
return;
}
}

const outputFolder = _this.elm_config.outputFolder;
const outputFile = _this.elm_config.outputFile;

if (outputFile === null) {
return compileModules.forEach(function(src) {
if(_this.compiledOnCurrentStep[src]) return;

const moduleName = path.basename(src, '.elm').toLowerCase();
return _this.elmCompile(src, path.join(outputFolder, moduleName + '.js'));
});
} else {
if(_this.compiledOnCurrentStep[modules]) return;

return _this.elmCompile(modules, path.join(outputFolder, outputFile));
}
}
})
})
};

})();
ElmCompiler.prototype.elmCompile = function(srcFile, outputFile) {
const elmFolder = this.elm_config.elmFolder;
const executablePath = this.elm_config.executablePath;
const makeParameters = this.elm_config.makeParameters;

elmCompile = function(executablePath, srcFile, elmFolder, outputFile, makeParameters, callback) {
if (Array.isArray(srcFile)) {
srcFile = srcFile.join(' ');
}
var info = 'Elm compile: ' + srcFile;
if (elmFolder) {
info += ', in ' + elmFolder;
}
info += ', to ' + outputFile;
console.log(info);
const originSrcFile = srcFile;
if (Array.isArray(srcFile)) srcFile = srcFile.join(' ');

const params = ['--yes'] // Reply 'yes' to all automated prompts
.concat(makeParameters) // other options from brunch-config.js
.concat(['--output', outputFile , srcFile ]);
var info = 'Elm compile: ' + srcFile;
if (elmFolder) info += ', in ' + elmFolder;
info += ', to ' + outputFile;
console.log(info);

var executable = path.join(executablePath, 'elm-make');
var command = executable + ' ' + params.join(' ');

const params = ['--yes'] // Reply 'yes' to all automated prompts
.concat(makeParameters) // other options from brunch-config.js
.concat(['--output', outputFile , srcFile ]);

const executable = path.join(executablePath, 'elm-make');
const command = executable + ' ' + params.join(' ');


try {
childProcess.execSync(command, { cwd: elmFolder });
callback(null, "");
} catch (error) {
callback(error, "");
}
};
this.compiledOnCurrentStep[originSrcFile] = true;
};

return ElmCompiler;
})();
}).call(this);
51 changes: 22 additions & 29 deletions test/index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ describe('ElmCompiler', function (){
it('has a #compile method', function () {
expect(elmCompiler.compile).to.be.an.instanceof(Function);
});

it('has a #onCompile method', function () {
expect(elmCompiler.onCompile).to.be.an.instanceof(Function);
});
});

describe('elm config', function () {
Expand Down Expand Up @@ -184,13 +188,10 @@ describe('ElmCompiler', function (){

it('shells out to the `elm-make` command with a null cwd', function () {
var content = '';
elmCompiler.compile(content, 'File.elm', function(error, data) {
expect(error).to.not.be.ok;
expect(data).to.equal('');
});
elmCompiler.compile(content, 'File.elm', function(error) {
expect(error).to.not.be.ok;
});
elmCompiler.compile(null, null, function(){});
elmCompiler.onCompile({sourceFiles: [{path: 'File.elm'}]}, []);
elmCompiler.compile(null, null, function(){});
elmCompiler.onCompile({sourceFiles: [{path: 'File.elm'}]}, []);
expected = 'elm-make --yes --output test/output/folder/test.js Test.elm';
expect(childProcess.execSync).to.have.been.calledWith(expected, {cwd: null});
});
Expand All @@ -205,21 +206,18 @@ describe('ElmCompiler', function (){

it('shells out to the `elm-make` command with the specified elm folder as the cwd', function () {
var content = '';
elmCompiler.compile(content, 'File.elm', function(error) {
expect(error).to.not.be.ok;
});
elmCompiler.compile(content, 'File.elm', function(error) {
expect(error).to.not.be.ok;
});
elmCompiler.compile(null, null, function(){});
elmCompiler.onCompile({sourceFiles: [{path: 'File.elm'}]}, []);
elmCompiler.compile(null, null, function(){});
elmCompiler.onCompile({sourceFiles: [{path: 'File.elm'}]}, []);
expected = 'elm-make --yes --output test/output/folder/test.js Test.elm';
expect(childProcess.execSync).to.have.been.calledWith(expected, {cwd: 'test/elm/folder'});
});

it('normalises the brunch file path to the elmFolder path', function () {
var content = '';
elmCompiler.compile(content, 'test/elm/folder/Test.elm', function(error) {
expect(error).to.not.be.ok;
});
elmCompiler.compile(null, null, function(){});
elmCompiler.onCompile({sourceFiles: [{path: 'test/elm/folder/Test.elm'}]}, []);
expected = 'elm-make --yes --output test/output/folder/test.js Test.elm';
expect(childProcess.execSync).to.have.been.calledWith(expected, {cwd: 'test/elm/folder'});
});
Expand All @@ -235,19 +233,16 @@ describe('ElmCompiler', function (){

it('should skip non main modules', function () {
var content = '';
elmCompiler.compile(content, 'File.elm', function(error, data) {
expect(error).to.not.be.ok;
expect(data).to.equal('');
});
elmCompiler.compile(null, null, function(){});
elmCompiler.onCompile({sourceFiles: [{path: 'File.elm'}]}, []);
expected = '';
expect(childProcess.execSync).to.not.have.been.called;
});

it('should compile main modules', function () {
var content = '';
elmCompiler.compile(content, 'Test.elm', function(error) {
expect(error).to.not.be.ok;
});
elmCompiler.compile(null, null, function(){});
elmCompiler.onCompile({sourceFiles: [{path: 'Test.elm'}]}, []);
expected = 'elm-make --yes --output test/output/folder/test.js Test.elm';
expect(childProcess.execSync).to.have.been.calledWith(expected, {cwd: null});
});
Expand All @@ -262,9 +257,8 @@ describe('ElmCompiler', function (){

it('should compile all modules', function () {
var content = '';
elmCompiler.compile(content, 'Test.elm', function(error) {
expect(error).to.not.be.ok;
});
elmCompiler.compile(null, null, function(){});
elmCompiler.onCompile({sourceFiles: [{path: 'Test.elm'}]}, []);
expected = 'elm-make --yes --output test/output/folder/test.js Test.elm';
expect(childProcess.execSync).to.have.been.calledWith(expected, {cwd: null});
});
Expand All @@ -280,9 +274,8 @@ describe('ElmCompiler', function (){
});
it('should compile all modules into a single file', function () {
var content = '';
elmCompiler.compile(content, 'Test1.elm', function(error) {
expect(error).to.not.be.ok;
});
elmCompiler.compile(null, null, function(){});
elmCompiler.onCompile({sourceFiles: [{path: 'Test1.elm'}]}, []);
expected = 'elm-make --yes --output test/output/folder/test.js Test1.elm Test2.elm';
expect(childProcess.execSync).to.have.been.calledWith(expected, {cwd: null});
});
Expand Down

0 comments on commit 8b062af

Please sign in to comment.