This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
load.js
117 lines (109 loc) · 3.19 KB
/
load.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
import { dirname, join } from "./deps.js";
import walk from "./walk.js";
export default function load(ast, options) {
options = getOptions(options);
// clone the ast
ast = JSON.parse(JSON.stringify(ast));
return walk(ast, function (node) {
if (node.str === undefined) {
if (
node.type === "Include" ||
node.type === "RawInclude" ||
node.type === "Extends"
) {
var file = node.file;
if (file.type !== "FileReference") {
throw new Error('Expected file.type to be "FileReference"');
}
var path, str, raw;
try {
path = options.resolve(file.path, file.filename, options);
file.fullPath = path;
raw = options.read(path, options);
str = raw.toString("utf8");
} catch (ex) {
ex.message += "\n at " + node.filename + " line " + node.line;
throw ex;
}
file.str = str;
file.raw = raw;
if (node.type === "Extends" || node.type === "Include") {
file.ast = load.string(
str,
Object.assign({}, options, {
filename: path,
}),
);
}
}
}
});
}
load.string = function loadString(src, options) {
options = Object.assign(getOptions(options), {
src: src,
});
var tokens = options.lex(src, options);
var ast = options.parse(tokens, options);
return load(ast, options);
};
load.file = function loadFile(filename, options) {
options = Object.assign(getOptions(options), {
filename: filename,
});
var str = options.read(filename).toString("utf8");
return load.string(str, options);
};
load.resolve = function resolve(filename, source, options) {
filename = filename.trim();
if (filename[0] !== "/" && !source) {
throw new Error(
'the "filename" option is required to use includes and extends with "relative" paths',
);
}
if (filename[0] === "/" && !options.basedir) {
throw new Error(
'the "basedir" option is required to use includes and extends with "absolute" paths',
);
}
filename = join(
filename[0] === "/" ? options.basedir : dirname(source.trim()),
filename,
);
return filename;
};
load.read = function read(filename, options) {
return Deno.readTextFileSync(filename);
};
load.validateOptions = function validateOptions(options) {
/* istanbul ignore if */
if (typeof options !== "object") {
throw new TypeError("options must be an object");
}
/* istanbul ignore if */
if (typeof options.lex !== "function") {
throw new TypeError("options.lex must be a function");
}
/* istanbul ignore if */
if (typeof options.parse !== "function") {
throw new TypeError("options.parse must be a function");
}
/* istanbul ignore if */
if (options.resolve && typeof options.resolve !== "function") {
throw new TypeError("options.resolve must be a function");
}
/* istanbul ignore if */
if (options.read && typeof options.read !== "function") {
throw new TypeError("options.read must be a function");
}
};
function getOptions(options) {
load.validateOptions(options);
return Object.assign(
{
resolve: load.resolve,
read: load.read,
},
options,
);
}