Skip to content

Commit

Permalink
Merge pull request #169 from uqbar-project/node-lib-download
Browse files Browse the repository at this point in the history
Node lib download
  • Loading branch information
PalumboN committed Jul 1, 2024
2 parents 8e223ac + 26620e8 commit e6f4e5c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ src/wre/wre.json
coverage
.DS_Store
/public/diagram/lib/*
/public/game/lib/*
wollok.log
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"main": "./build/src/index.js",
"scripts": {
"preinstall": "sh scripts/download-libs.sh",
"preinstall": "node ./scripts/download-libs.js",
"start": "node ./build/src/index.js",
"test": "npm run lint && npm run test:unit",
"test-with-coverage": "npm run lint && nyc --reporter=json --lines 70 npm run test:unit",
Expand Down
6 changes: 3 additions & 3 deletions public/game/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<html>
<head>
<meta charset="UTF-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/addons/p5.sound.js"></script>
<script src="./lib/p5.js"></script>
<script src="./lib/p5.sound.js"></script>
<title>Wollok Game</title>

<script
Expand All @@ -27,7 +27,7 @@
let socket = 1;
</script>
<script type="module">
import { io } from "https://cdn.socket.io/4.4.1/socket.io.esm.min.js";
import { io } from "./lib/socket.io.esm.min.js";

socket = io();
socket.on("connect", function () {
Expand Down
79 changes: 79 additions & 0 deletions scripts/download-libs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var path = require("node:path");
var fs = require("fs");
var http = require("node:http");

const libsToDownload = [
{
libDest: "game",
libs: [
{
filename: "p5.js",
url: "http://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/p5.js",
},
{
filename: "p5.sound.js",
url: "http://cdnjs.cloudflare.com/ajax/libs/p5.js/1.5.0/addons/p5.sound.js",
},
{
filename: "socket.io.esm.min.js",
url: "http://cdn.socket.io/4.4.1/socket.io.esm.min.js",
},
{
filename: "socket.io.esm.min.js.map",
url: "http://cdn.socket.io/4.4.1/socket.io.esm.min.js.map",
},
],
},
{
libDest: "diagram",
libs: [
{
filename: "socket.io.esm.min.js",
url: "http://cdn.socket.io/4.4.1/socket.io.esm.min.js",
},
{
filename: "socket.io.esm.min.js.map",
url: "http://cdn.socket.io/4.4.1/socket.io.esm.min.js.map",
},
{
filename: "cytoscape.min.js",
url: "http://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.26.0/cytoscape.min.js",
},
],
},
];

for (const { libDest, libs } of libsToDownload) {
console.log(`Downloading ${libDest} libraries`);

const libFolder = path.resolve(__dirname, "../public", libDest, "lib");

if (!fs.existsSync(libFolder)) {
console.log("Creating lib folder");
fs.mkdirSync(libFolder, { recursive: true });
}

for (const { filename, url } of libs) {
const dest = path.join(libFolder, filename);
if (!fs.existsSync(dest)) {
download(url, dest);
} else {
console.log(`Found local version of ${filename}, skipping download`);
}
}
}

function download(url, dest) {
console.log(`Downloading from ${url}`);
const file = fs.createWriteStream(dest);
const options = {};
http
.get(url, options, (response) => {
response.pipe(file);
file.on("finish", () => file.close());
})
.on("error", function (err) {
fs.unlink(dest);
console.log(err.message);
});
}
12 changes: 0 additions & 12 deletions scripts/download-libs.sh

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"esModuleInterop": true,
"experimentalDecorators": true
},
"include": ["test/**/*.ts", "src/**/*.ts", "public/**/*"],
"include": ["test/**/*.ts", "src/**/*.ts"],
"exclude": ["**/*.js", "node_modules"]
}

0 comments on commit e6f4e5c

Please sign in to comment.