-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.ts
270 lines (233 loc) · 7.56 KB
/
compiler.ts
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import * as path from "https://deno.land/std@0.77.0/path/mod.ts";
type Sources = string | Array<string>;
export type Mode = "debug" | "optimize" | "no_mode";
interface Options {
run: (o: Deno.RunOptions) => Deno.Process;
mode: Mode;
pathToElm: string;
cwd?: string;
help?: any;
output?: any;
report?: any;
verbose: boolean;
processOpts?: any;
docs?: any;
}
interface UserOptions {
run?: (o: Deno.RunOptions) => Deno.Process;
mode?: Mode;
cwd?: string;
pathToElm?: string;
help?: any;
output?: any;
report?: any;
verbose?: boolean;
processOpts?: any;
docs?: any;
}
const ELM_BINARY_NAME = "elm";
const defaultOptions: Options = {
run: Deno.run,
mode: "no_mode",
pathToElm: ELM_BINARY_NAME,
cwd: undefined,
help: undefined,
output: undefined,
report: undefined,
verbose: false,
processOpts: undefined,
docs: undefined,
};
const supportedOptions = Object.keys(defaultOptions);
function prepareSources(sources: 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?: UserOptions): Options {
if (options == null) {
return defaultOptions;
}
return { ...defaultOptions, ...options };
}
function prepareProcessArgs(sources: Sources, options: Options) {
const preparedSources = prepareSources(sources);
const compilerArgs = compilerArgsFromOptions(options);
return ["make"].concat(
preparedSources ? preparedSources.concat(compilerArgs) : compilerArgs
);
}
function prepareProcessOpts(options: Options) {
const env = {
LANG: "en_US.UTF-8",
...Deno.env.toObject(),
};
return {
env,
stdio: "inherit",
cwd: options.cwd,
...options.processOpts,
};
}
function buildElmProcess(
sources: Sources,
options: Options,
pathToElm: string
): Deno.Process {
if (typeof options.run !== "function") {
throw `options.run was a(n) ${typeof options.run} instead of a function.`;
}
const processArgs = prepareProcessArgs(sources, options);
const processOpts = prepareProcessOpts(options);
if (options.verbose) {
console.log(["Running", pathToElm].concat(processArgs).join(" "));
}
return options.run({
...processOpts,
cmd: [pathToElm, ...processArgs],
});
}
function compilerErrorToString(err: any, pathToElm: string) {
if (typeof err === "object" && typeof err.code === "string") {
switch (err.code) {
case "ENOENT":
return `Could not find Elm compiler "${pathToElm}". Is it installed?`;
case "EACCES":
return `Elm compiler ${pathToElm}" did not have permission to run. Do you need to give it executable permissions?`;
default:
return `Error attempting to run Elm compiler "${pathToElm}":\n${err}`;
}
} else if (typeof err === "object" && typeof err.message === "string") {
return JSON.stringify(err.message);
} else {
return `Exception thrown when attempting to run Elm compiler ${JSON.stringify(
pathToElm
)}`;
}
}
async function compile(sources: Sources, options?: UserOptions) {
const optionsWithDefaults = prepareOptions(options);
const pathToElm = optionsWithDefaults.pathToElm;
const process = buildElmProcess(sources, optionsWithDefaults, pathToElm);
try {
const { success, code } = await process.status();
if (!success) {
throw { code };
}
} catch (error) {
throw compilerErrorToString(error, pathToElm);
} finally {
await process.close();
}
}
// 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
async function compileToString(sources: Sources, options?: UserOptions) {
const decoder = new TextDecoder("utf-8");
const internalOptions: UserOptions = { ...options };
const tempDir = await Deno.makeTempDir();
internalOptions.output = path.join(
tempDir,
internalOptions.output || "elm.js"
);
internalOptions.processOpts = { stdout: "piped", stderr: "piped" };
const optionsWithDefaults = prepareOptions(internalOptions);
const pathToElm = optionsWithDefaults.pathToElm;
const process = buildElmProcess(sources, optionsWithDefaults, pathToElm);
const stderr = await process.stderrOutput();
await process.output();
const standardError = decoder.decode(stderr);
if (standardError) {
throw standardError;
}
const { success, code } = await process.status();
if (!success) {
console.log("failure", code);
}
await process.close();
const data = await Deno.readFile(optionsWithDefaults.output);
const compiledElm = decoder.decode(data);
await Deno.remove(tempDir, { recursive: true });
return compiledElm;
}
async function compileToModule(sources: Sources, options?: UserOptions) {
const outputPath = options?.output || "elm.js";
const result = await compileToModuleString(sources, options);
const encoder = new TextEncoder();
const data = encoder.encode(result);
await Deno.writeFile(outputPath, data);
}
async function compileToModuleString(sources: Sources, options?: UserOptions) {
const outputPath = options?.output || "elm.js";
const initial = await compileToString(sources, {
...options,
output: outputPath,
});
const intermitent = initial
.replace("(function(scope){", "function init(scope){")
.replace(";}(this));", ";}");
const result = `${intermitent}
const moduleScope = {};
init(moduleScope);
export default moduleScope.Elm;`;
return result;
}
// Converts an object of key/value pairs to an array of arguments suitable
// to be passed to run for `elm make`.
function compilerArgsFromOptions(options: Options): Array<string> {
return Object.entries(options).flatMap(function ([opt, value]) {
if (value) {
switch (opt) {
case "help":
return ["--help"];
case "output":
return ["--output", value];
case "report":
return ["--report", value];
case "mode": {
switch (value) {
case "debug":
return ["--debug"];
case "optimize":
return ["--optimize"];
case "no_mode":
return [];
}
}
case "docs":
return ["--docs", value];
case "runtimeOptions":
return ["+RTS", value, "-RTS"];
default:
if (supportedOptions.indexOf(opt) === -1) {
if (opt === "yes") {
throw new Error(
"deno-elm-compiler received the `yes` option, but that was removed in Elm 0.19. Try re-running without passing the `yes` option."
);
} else if (opt === "warn") {
throw new Error(
"deno-elm-compiler received the `warn` option, but that was removed in Elm 0.19. Try re-running without passing the `warn` option."
);
} else if (opt === "pathToMake") {
throw new Error(
"deno-elm-compiler received the `pathToMake` option, but that was renamed to `pathToElm` in Elm 0.19. Try re-running after renaming the parameter to `pathToElm`."
);
} else {
throw new Error(
"deno-elm-compiler was given an unrecognized Elm compiler option: " +
opt
);
}
}
return [];
}
} else {
return [];
}
});
}
export { compile, compileToModule, compileToString, compileToModuleString };