#!/usr/bin/env node /* eslint-env node */ /* eslint-disable no-console */ // Build deployable version to TARGET_DIR const fs = require("fs-extra"), https = require("https"), TARGET_DIR = "build/", CONFIG_FILE = "build/resources/js/Config.js", VERSION_PLACEHOLDER = "$VERSION", version = require("./package.json").version, fileList = [{ url: "https://lab.las3.de/gitlab/eye-tracking-classroom/gaze-client.js/raw/master/build/gazeclient.js", localPath: "build/vendors/gazeclient.js", }, { url: "https://lab.las3.de/gitlab/eye-tracking-classroom/gaze-client.js/raw/master/build/gazeclient.js.map", localPath: "build/vendors/gazeclient.js.map", }, { url: "https://lab.las3.de/gitlab/eye-tracking-classroom/gaze-client.js/raw/master/build/gazeclient.min.js", localPath: "build/vendors/gazeclient.min.js", }, ]; function run() { createTargetDir(); downloadLibrary(); copyFiles(); insertVersionNumber(); } function onError(error) { console.log(error); } function createTargetDir() { console.log(`Ensure target directory [${TARGET_DIR}] exists and is empty`); fs.ensureDirSync(TARGET_DIR); fs.emptyDirSync(TARGET_DIR); } function downloadLibrary() { console.log(`Downloading client library (${fileList.length} files]`); let vendorsFolder = `${TARGET_DIR}vendors`; fs.ensureDirSync(vendorsFolder); for (let i = 0; i < fileList.length; i++) { let file = fileList[i]; downloadFile(file.url, file.localPath); } } function downloadFile(source, target) { let file; console.log(`Downloading [${source}] to ${target}`); file = fs.createWriteStream(target); https.get(source, function(response) { response.pipe(file); }); } function copyFiles() { console.log(`Copying files to ${TARGET_DIR}`); fs.copySync("resources/", `${TARGET_DIR}resources`); fs.copySync("index.html", `${TARGET_DIR}index.html`); } function insertVersionNumber() { console.log(`Replacing version placeholder in ${CONFIG_FILE}`); let result; fs.readFile(CONFIG_FILE, "utf8", function(err, data) { if (err) { onError(err); } result = data.replace(VERSION_PLACEHOLDER, version); fs.writeFile(CONFIG_FILE, result, "utf8", function(err) { if (err) { onError(err); } }); }); } run();