forked from madsflensted/elm-brunch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (82 loc) · 3.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(function() {
var ElmCompiler, elmCompile, childProcess, path;
childProcess = require('child_process');
path = require('path');
module.exports = ElmCompiler = (function() {
ElmCompiler.prototype.brunchPlugin = true;
ElmCompiler.prototype.type = 'javascript';
ElmCompiler.prototype.extension = 'elm';
function ElmCompiler(config) {
var elm_config = {};
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;
this.skipedOnInit = {};
}
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 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 ( src, elmFolder
, path.join(outputFolder, moduleName + '.js')
, makeParameters
, callback );
});
} else {
return elmCompile ( modules, elmFolder
, path.join(outputFolder, outputFile)
, makeParameters
, callback );
}
};
return ElmCompiler;
})();
elmCompile = function(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 params = ['--yes'] // Reply 'yes' to all automated prompts
.concat(makeParameters) // other options from brunch-config.js
.concat(['--output', outputFile , srcFile ]);
var command = 'elm make ' + params.join(' ');
try {
childProcess.execSync(command, { cwd: elmFolder });
callback(null, "");
} catch (error) {
callback(error, "");
}
};
}).call(this);