Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration service worker #298

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions default-experiment-descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"label": "",
"basedir": ".",
"LaTeXinMD": false,
"service-worker": "/path/to/service-worker.js",
"units": [
{
"unit-type": "aim"
Expand Down
70 changes: 46 additions & 24 deletions exp_build/experiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function getAssessmentPath(src, units) {
let paths = getAssessmentPath(nextSrc, unit.units);
assessmentPath.push(...paths);
}
if(unit["content-type"] === ContentTypes.ASSESMENT || unit["content-type"] === ContentTypes.ASSESSMENT){
if (unit["content-type"] === ContentTypes.ASSESMENT || unit["content-type"] === ContentTypes.ASSESSMENT) {
const quiz = path.resolve(src, unit.source);
assessmentPath.push(quiz);
}
Expand Down Expand Up @@ -126,27 +126,27 @@ class Experiment {
const descriptor = require(descriptorPath);
const pathToValidator = path.resolve(__dirname, "../validation/validate.js");
try {
log.debug("Validating experiment descriptor");
shell.exec(`node ${pathToValidator} -f ${descriptorPath} >> ${buildPath}/validate.log`);
log.debug("Validating experiment descriptor");
shell.exec(`node ${pathToValidator} -f ${descriptorPath} >> ${buildPath}/validate.log`);
} catch (e) {
log.error("Error validating experiment descriptor", e);
}
// loop through the units and validate the content
try {
log.debug("Validating Assessment files");
const assessmentPath = getAssessmentPath(expPath, descriptor.units);
assessmentPath.forEach((file) => {
if (fs.existsSync(file)) {
// trim ep from file
const fileName = file.replace(expPath, "");
shell.exec(`echo =${fileName} >> ${buildPath}/assesment.log`);
shell.exec(
`node ${pathToValidator} -f ${file} -c assessment >> ${buildPath}/assesment.log`
);
} else {
log.error(`Assessment file ${path} does not exist`);
}
});
log.debug("Validating Assessment files");
const assessmentPath = getAssessmentPath(expPath, descriptor.units);
assessmentPath.forEach((file) => {
if (fs.existsSync(file)) {
// trim ep from file
const fileName = file.replace(expPath, "");
shell.exec(`echo =${fileName} >> ${buildPath}/assesment.log`);
shell.exec(
`node ${pathToValidator} -f ${file} -c assessment >> ${buildPath}/assesment.log`
);
} else {
log.error(`Assessment file ${path} does not exist`);
}
});
} catch (e) {
log.error("Error validating Assessment files", e);
}
Expand All @@ -159,18 +159,37 @@ class Experiment {
return renderMarkdown(name_file.toString());
}

generateServiceWorker(buildPath) {
const { generateSW } = require("@virtual-labs/service_worker");
const expPath = path.resolve(this.src, Config.Experiment.exp_dir);
let inputPath = "";
// check if the user has provided a service worker file path and if so, check if it exists
if (this.descriptor['service-worker']) {
const swPath = path.resolve(expPath, this.descriptor['service-worker']);
if (fs.existsSync(swPath)) {
inputPath = swPath;
} else {
log.error(`Service worker file ${swPath} does not exist`);
}
} else {
log.warn("No service worker file provided");
}
const swDest = path.resolve(buildPath, "sw.js");
generateSW(inputPath, buildPath, swDest);
}

build(hb, lab_data, options) {
/*
here we are assuming that the descriptor contains a simgle object
that represents the learning unit corresponding to the experiment.
*/
log.debug(`Building experiment`);
const explu = LearningUnit.fromRecord(this.descriptor, this.src);
const exp_info = {
name: this.name(),
menu: explu.units,
src: this.src,
bp: Config.build_path(this.src) + "/",
log.debug(`Building experiment`);
const explu = LearningUnit.fromRecord(this.descriptor, this.src);
const exp_info = {
name: this.name(),
menu: explu.units,
src: this.src,
bp: Config.build_path(this.src) + "/",
};

if (options.plugins) {
Expand All @@ -186,6 +205,9 @@ class Experiment {
if (options.plugins) {
Plugin.processPostBuildPlugins(exp_info, options);
}

// generate service worker
this.generateServiceWorker(exp_info.bp);
/*
This "tmp" directory is needed because when you have a sub-directory
with the same name, it can cause issue. So, we assume that there should
Expand Down
10 changes: 10 additions & 0 deletions exp_build/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ class Task extends Unit {
return final_paths;
}

serviceWorkerPath() {
let exp_service_worker = path.resolve(
path.join(Config.build_path(this.exp_path), this.basedir, "sw.js")
);
let task_folder = path.dirname(this.targetPath());
let final_service_worker = path.relative(task_folder, exp_service_worker);
return final_service_worker;
}

buildPage(exp_info, lab_data, options) {
let assets_path = path.relative(
path.dirname(this.targetPath()),
Expand Down Expand Up @@ -191,6 +200,7 @@ class Task extends Unit {
baseUrl: lab_data.baseUrl,
exp_name: lab_data.exp_name || exp_info_name_text,
exp_short_name: lab_data.exp_short_name,
service_worker_path: this.serviceWorkerPath(),
};
// Context Info for Bug report
page_data.bugreport_context_info = JSON.stringify({
Expand Down
8 changes: 8 additions & 0 deletions exp_build/templates/pages/content.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,13 @@
{{#each js_modules}}
<script src="{{this}}"></script>
{{/each}}

<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register("{{service_worker_path}}");
});
}
</script>
</body>
</html>
Loading