-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathindex.js
212 lines (175 loc) · 6.18 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict';
var spawn = require("cross-spawn");
var _ = require("lodash");
var compilerBinaryName = "elm-make";
var fs = require("fs");
var path = require("path");
var temp = require("temp").track();
var findAllDependencies = require("find-elm-dependencies").findAllDependencies;
var defaultOptions = {
emitWarning: console.warn,
spawn: spawn,
cwd: undefined,
pathToMake: undefined,
yes: undefined,
help: undefined,
output: undefined,
report: undefined,
warn: undefined,
debug: undefined,
verbose: false,
processOpts: undefined,
docs: undefined,
};
var supportedOptions = _.keys(defaultOptions);
function prepareSources(sources) {
if (!(sources instanceof Array || typeof sources === "string")) {
throw "compile() received neither an Array nor a String for its sources argument.";
}
return typeof sources === "string" ? [sources] : sources;
}
function prepareOptions(options, spawnFn) {
return _.defaults({ spawn: spawnFn }, options, defaultOptions);
}
function prepareProcessArgs(sources, options) {
var preparedSources = prepareSources(sources);
var compilerArgs = compilerArgsFromOptions(options, options.emitWarning);
return preparedSources ? preparedSources.concat(compilerArgs) : compilerArgs;
}
function prepareProcessOpts(options) {
var env = _.merge({LANG: 'en_US.UTF-8'}, process.env);
return _.merge({ env: env, stdio: "inherit", cwd: options.cwd }, options.processOpts);
}
function runCompiler(sources, options, pathToMake) {
if (typeof options.spawn !== "function") {
throw "options.spawn was a(n) " + (typeof options.spawn) + " instead of a function.";
}
var processArgs = prepareProcessArgs(sources, options);
var processOpts = prepareProcessOpts(options);
if (options.verbose) {
console.log(["Running", pathToMake].concat(processArgs || []).join(" "));
}
return options.spawn(pathToMake, processArgs, processOpts);
}
function handleCompilerError(err, pathToMake) {
if ((typeof err === "object") && (typeof err.code === "string")) {
handleError(pathToMake, err);
} else {
console.error("Exception thrown when attempting to run Elm compiler " + JSON.stringify(pathToMake) + ":\n" + err);
}
throw err;
process.exit(1);
}
function compileSync(sources, options) {
var optionsWithDefaults = prepareOptions(options, options.spawn || spawn.sync);
var pathToMake = options.pathToMake || compilerBinaryName;
try {
return runCompiler(sources, optionsWithDefaults, pathToMake);
} catch (err) {
handleCompilerError(err, pathToMake);
}
}
function compile(sources, options) {
var optionsWithDefaults = prepareOptions(options, options.spawn || spawn);
var pathToMake = options.pathToMake || compilerBinaryName;
try {
return runCompiler(sources, optionsWithDefaults, pathToMake)
.on('error', function(err) {
handleError(pathToMake, err);
process.exit(1);
});
} catch (err) {
handleCompilerError(err, pathToMake);
}
}
// write compiled Elm to a string output
// returns a Promise which will contain a Buffer of the text
// If you want html instead of js, use options object to set
// output to a html file instead
// creates a temp file and deletes it after reading
function compileToString(sources, options){
if (typeof options.output === "undefined"){
options.output = '.js';
}
return new Promise(function(resolve, reject){
temp.open({ suffix: options.output }, function(err, info){
if (err){
return reject(err);
}
options.output = info.path;
options.processOpts = { stdio: 'pipe' }
var compiler = compile(sources, options);
compiler.stdout.setEncoding("utf8");
compiler.stderr.setEncoding("utf8");
var output = '';
compiler.stdout.on('data', function(chunk) {
output += chunk;
});
compiler.stderr.on('data', function(chunk) {
output += chunk;
});
compiler.on("close", function(exitCode) {
if (exitCode !== 0) {
return reject(new Error('Compilation failed\n' + output));
} else if (options.verbose) {
console.log(output);
}
fs.readFile(info.path, {encoding: "utf8"}, function(err, data){
return err ? reject(err) : resolve(data);
});
});
});
});
}
function compileToStringSync(sources, options) {
if (typeof options.output === "undefined"){
options.output = '.js';
}
const file = temp.openSync({ suffix: options.output });
options.output = file.path;
compileSync(sources, options);
return fs.readFileSync(file.path, {encoding: "utf8"});
}
function handleError(pathToMake, err) {
if (err.code === "ENOENT") {
console.error("Could not find Elm compiler \"" + pathToMake + "\". Is it installed?")
} else if (err.code === "EACCES") {
console.error("Elm compiler \"" + pathToMake + "\" did not have permission to run. Do you need to give it executable permissions?");
} else {
console.error("Error attempting to run Elm compiler \"" + pathToMake + "\":\n" + err);
}
}
// Converts an object of key/value pairs to an array of arguments suitable
// to be passed to child_process.spawn for elm-make.
function compilerArgsFromOptions(options, emitWarning) {
return _.flatten(_.map(options, function(value, opt) {
if (value) {
switch(opt) {
case "yes": return ["--yes"];
case "help": return ["--help"];
case "output": return ["--output", value];
case "report": return ["--report", value];
case "warn": return ["--warn"];
case "debug": return ["--debug"];
case "docs": return ["--docs", value]
case "runtimeOptions": return ["+RTS", value, "-RTS"]
default:
if (supportedOptions.indexOf(opt) === -1) {
emitWarning('Unknown Elm compiler option: ' + opt);
}
return [];
}
} else {
return [];
}
}));
}
module.exports = {
compile: compile,
compileSync: compileSync,
compileWorker: require("./worker.js")(compile),
compileToString: compileToString,
compileToStringSync: compileToStringSync,
findAllDependencies: findAllDependencies,
_prepareProcessArgs: prepareProcessArgs
};