-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
118 lines (89 loc) · 3.49 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
/* global __dirname, process */
var fs = require("fs");
var path = require("path");
var semver = require("semver");
var createApp = require("multiversum/bootstrap").bootstrap;
// @ts-ignore
var package = require("./package.json");
var TOOTHROT_DIR = __dirname;
function createToothrotApp(config) {
var app;
var resources = {};
config = config || {};
config.applicationStep = config.applicationStep || "build";
config.environments = Array.isArray(config.environments) ? config.environments : ["node"];
config.debug = config.debug === true;
config.paths = Array.isArray(config.paths) ? config.paths : [];
config.paths.unshift(TOOTHROT_DIR);
config.paths.unshift(process.cwd());
app = createApp(config.paths, {
patterns: ["**/*.com.json"],
onError: config.debug ? (config.onError || undefined) : (config.onError || function () {}),
filter: function (component, filePath) {
if (component.application !== "toothrot") {
return false;
}
if (!semver.satisfies(package.version, component.applicationVersion)) {
return false;
}
if (
!("environments" in component) ||
!Array.isArray(component.environments) ||
!matchesEnvironments(component.environments, config.environments)
) {
return;
}
if (
!config.debug &&
// @ts-ignore
Array.isArray(component.flags) &&
// @ts-ignore
(!config.debug && component.flags.indexOf("debug") >= 0)
) {
return false;
}
if (component.applicationSteps.indexOf(config.applicationStep) >= 0) {
if (component.resources && typeof component.resources === "object") {
addResources(component.resources, path.dirname(filePath));
}
return true;
}
return false;
}
});
app.decorate("getModule", function (fn) {
return function (name) {
var result = fn(name);
if (result) {
return result;
}
return require(name);
};
});
app.connect("getResource", function (name) {
return resources[name];
});
app.connect("getEngineVersion", function () {
return package.version;
});
return app;
function addResources(componentResources, componentDir) {
Object.keys(componentResources).forEach(function (resourceName) {
var resource = componentResources[resourceName];
var filePath = typeof resource === "string" ? resource : resource.source;
var fullPath = path.join(componentDir, filePath);
if (!fs.existsSync(fullPath)) {
throw new Error("Resource cannot be found: " + fullPath);
}
resources[resourceName] = "" + fs.readFileSync(fullPath);
});
}
}
function matchesEnvironments(componentEnvironments, applicationEnvironments) {
return componentEnvironments.some(function (env) {
return applicationEnvironments.indexOf(env) >= 0;
});
}
module.exports = {
createApp: createToothrotApp
};