This repository was archived by the owner on Feb 14, 2024. It is now read-only.
forked from MaxPleaner/slim-lang-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (41 loc) · 1.73 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
var getRemainingRequest = require("loader-utils").getRemainingRequest;
var getOptions = require('loader-utils').getOptions;
var execSync = require('child_process').execSync
var fs = require("fs")
var path = require("path")
var defaults = require('lodash.defaults')
function prepareArguments(slimOptions) {
return Object.entries(slimOptions).map(function(option_with_value) {
return "-o " + option_with_value.join("=");
}).join(" ");
}
function compileToSlim(remainingRequest, slimOptions) {
var additionalArguments = prepareArguments(slimOptions);
return execSync(`slimrb ${additionalArguments} ${remainingRequest}`)
}
module.exports = function(source) {
var loader = this;
loader.cacheable && loader.cacheable();
var remainingRequest = getRemainingRequest(loader);
var config = defaults({}, getOptions(loader), {
slimOptions: {}
});
var result;
try {
result = compileToSlim(remainingRequest, config['slimOptions']);
} catch (e) {
var err = "";
if (e.location == null || e.location.first_column == null || e.location.first_line == null) {
err += "Got an unexpected exception from the slim-lang-script compiler. The original exception was: " + e + "\n";
} else {
var codeLine = source.split("\n")[e.location.first_line];
var offendingCharacter = (e.location.first_column < codeLine.length) ? codeLine[e.location.first_column] : "";
err += e + "\n";
// log erroneous line and highlight offending character
err += " L" + e.location.first_line + ": " + codeLine.substring(0, e.location.first_column) + offendingCharacter + codeLine.substring(e.location.first_column + 1) + "\n";
err += " " + (new Array(e.location.first_column + 1).join(" ")) + "^\n";
}
throw new Error(err);
}
this.callback(null, result);
}