-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
123 lines (108 loc) · 4.61 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(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.optimize = (config.plugins.elmBrunch || {}).optimize || false;
elm_config.elmMake = (config.plugins.elmBrunch || {}).elmMake || "elm make";
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.independentModules = (config.plugins.elmBrunch || {}).independentModules || null;
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 elmMake = this.elm_config.elmMake + ( this.elm_config.optimize ? " --optimize" : "" );
var executablePath = this.elm_config.executablePath;
var outputFolder = this.elm_config.outputFolder;
var independentModules = this.elm_config.independentModules;
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();
if(independentModules){
var src_path_length = src.split(path.sep).length;
if(src_path_length > 1){
moduleName = path.dirname(src).replace(path.sep,'_') + '_' + moduleName;
outputFolder = ('..' + path.sep).repeat(src_path_length - 1) + outputFolder;
if(elmFolder == null)
elmFolder = path.dirname(src);
else
elmFolder = elmFolder + path.sep + path.dirname(src);
}
src = path.basename(src);
}
return elmCompile ( executablePath
, src
, elmMake
, elmFolder
, path.join(outputFolder, moduleName + '.js')
, makeParameters
, callback );
});
} else {
return elmCompile ( executablePath
, modules
, elmMake
, elmFolder
, path.join(outputFolder, outputFile)
, makeParameters
, callback );
}
};
return ElmCompiler;
})();
elmCompile = function(executablePath, srcFile, elmMake, 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 = [] // Reply 'yes' to all automated prompts
.concat(makeParameters) // other options from brunch-config.js
.concat(['--output', outputFile , srcFile ]);
var executable = path.join(executablePath, elmMake);
var command = executable + ' ' + params.join(' ');
try {
childProcess.execSync(command, { cwd: elmFolder });
callback(null, "");
} catch (error) {
callback(error, "");
}
};
}).call(this);