-
Notifications
You must be signed in to change notification settings - Fork 14
/
Task.js
218 lines (189 loc) · 5.23 KB
/
Task.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
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
const path = require("path");
const fs = require("fs");
const marked = require("marked");
const { JSDOM } = require("jsdom");
const process = require("process");
const Handlebars = require("handlebars");
const shell = require("shelljs");
const Config = require("./Config.js");
const { Unit } = require("./Unit.js");
const {
UnitTypes,
ContentTypes,
BuildEnvs,
validType,
validContentType,
} = require("./Enums.js");
class Task extends Unit {
constructor(
unit_type,
label,
content_type,
exp_path,
basedir,
source,
target,
lu
) {
super(unit_type, label, exp_path, basedir);
this.lu = lu;
this.content_type = validContentType(content_type);
this.source = source;
this.target = target;
}
static unit_type = UnitTypes.TASK;
static fromRecord(t, lu, exp_path) {
return new Task(
t["unit-type"],
t["label"],
t["content-type"],
exp_path,
t["basedir"],
t["source"],
t["target"],
lu
);
}
menuItemInfo(host_path) {
return {
label: this.label,
unit_type: this.unit_type,
isCurrentItem: false,
lu: this.lu,
target: path.relative(path.dirname(host_path), this.targetPath())
};
}
getMenu(menu_data) {
return menu_data
.map((mi) => {
return mi.menuItemInfo(this.targetPath());
});
}
setCurrent(menu) {
menu.map((u) => {
if (u.unit_type === UnitTypes.TASK || u.unit_type === UnitTypes.AIM) {
u.isCurrentItem = this.target === u.target;
}
else {
u.isCurrentLU = (u.units? u.units.some((t) => {
return t.target === this.target;
})
: false);
u.units = u.units
? u.units.map((t) => {
t.isCurrentItem = this.target === t.target;
return t;
})
: [];
}
return u;
});
return menu;
}
targetPath() {
return path.resolve(path.join(
Config.build_path(this.exp_path),
this.basedir,
this.target
));
}
sourcePath() {
return path.resolve(path.join(
Config.build_path(this.exp_path),
this.basedir,
this.source
));
}
buildPage(exp_info, lab_data, options) {
let assets_path = path.relative(path.dirname(this.targetPath()), Config.build_path(this.exp_path));
assets_path = assets_path?assets_path:".";
const page_data = {
production: (options.env === BuildEnvs.PRODUCTION),
testing: options.testing,
local: options.local,
units: this.setCurrent(this.getMenu(exp_info.menu)),
experiment_name: exp_info.name,
isText: false,
isVideo: false,
isSimulation: false,
isAssesment: false,
assets_path: assets_path,
lab: lab_data.lab,
broadArea: lab_data.broadArea,
deployLab: lab_data.deployLab,
phase: lab_data.phase,
collegeName: lab_data.collegeName,
baseUrl: lab_data.baseUrl,
exp_name: lab_data.exp_name,
exp_short_name: lab_data.exp_short_name
};
switch (this.content_type) {
case ContentTypes.TEXT:
let mdContent = fs.readFileSync(this.sourcePath()).toString();
let htmlContent = marked(mdContent);
page_data.content = htmlContent;
page_data.isText = true;
break;
case ContentTypes.VIDEO:
let vidContent = fs.readFileSync(this.sourcePath()).toString();
let htmlvidContent = marked(vidContent);
page_data.content = htmlvidContent;
page_data.isVideo = true;
break;
case ContentTypes.SIMULATION:
page_data.isSimulation = true;
page_data.sim_src = this.source;
// Inject IframeResizer
let content = fs
.readFileSync(path.resolve(this.sourcePath()))
.toString();
let rp = path.join(
path.relative(path.dirname(this.sourcePath()),
Config.build_path(this.exp_path)),
"assets/js/iframeResize.js"
);
content = content.replace(
"</body>",
`<script src="${rp}"></script></body>`
);
fs.writeFileSync(path.resolve(this.sourcePath()), content);
break;
case ContentTypes.ASSESMENT:
page_data.isAssesment = true;
if(shell.test("-f", this.sourcePath())){
page_data.questions = require(this.sourcePath());
page_data.questions_str = JSON.stringify(page_data.questions);
page_data.isJsonVersion = true;
}
else {
const jsonpath = this.sourcePath();
const jspath = path.resolve(path.dirname(jsonpath), `${path.basename(jsonpath, "json")}js`);
if(shell.test("-f", jspath)){
page_data.quiz_src = path.basename(jspath);
page_data.isJsVersion = true;
}
else {
console.log(`${jspath} is missing`);
process.exit(-1);
}
}
break;
}
const page_template = fs.readFileSync(
path.resolve(
Config.Experiment.ui_template_name,
"pages",
"content.handlebars"
)
);
try {
fs.writeFileSync(this.targetPath(), Handlebars.compile(page_template.toString())(page_data));
} catch (e) {
console.error(e.message);
}
}
build(exp_info, lab_data, options) {
this.buildPage(exp_info, lab_data, options);
}
}
module.exports = { Task };