build.js 2.28 KB
Newer Older
Alexander Bazo committed
1 2 3 4 5 6 7 8 9 10
#!/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/",
11
  CONFIG_FILE = "build/resources/js/config/GameConfiguration.js",
Alexander Bazo committed
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
  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();