-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
76 lines (54 loc) · 1.55 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
"use strict";
const Path = require("path"),
Fs = require("fs");
class HCL {
constructor(fileTypes) {
this.fileTypes = false
if (Array.isArray(fileTypes)) {
this.fileTypes = fileTypes;
}
if (typeof(fileTypes) === "string") {
this.fileTypes = [fileTypes];
}
if (this.fileTypes) {
this.bindRequire();
}
}
// requires an HCL file and returns the result object
bindRequire() {
const Parser = this.parse;
function addRequire(file) {
if (require.extensions[file]) {
if (!require.extensions[file].__hcljs) {
console.warning(
"WARNING: Some other require implementation " +
"is already present for " + file + " type" +
",aborting."
);
}
return;
}
require.extensions[file] = function (module, filename) {
const fs = require('fs'),
hcl = Parser(fs.readFileSync(filename, 'utf8'));
module.exports = hcl;
};
if (!require.extensions['.hcl']) {
require.extensions['.hcl'] = require.extensions[file];
}
require.extensions[file].__hcljs = true;
}
for (let fileType of this.fileTypes) {
addRequire(fileType);
}
}
}
// converts an object into an HCL string
HCL.prototype.stringify = require("./lib/stringify");
// parses an HCL string into an object
HCL.prototype.parse = require("./lib/hcl").parse;
// parses an HCL file into an object
HCL.prototype.load = require("./lib/load");
module.exports = function(opts) {
return new HCL(opts);
}