-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (62 loc) · 2.98 KB
/
index.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
const fs = require('fs-extra');
var decompress = require('decompress');
var request = require('request');
const tempFolderName = "ClockworkWebBridgeTemp";
const contentFolderName = "/gamefiles";
const templateFolderName = "/template";
function build(path, outPath) {
return new Promise(function (resolve, reject) {
fs.removeSync(outPath + "/" + tempFolderName);
fs.mkdirSync(outPath + "/" + tempFolderName);
console.log("Decompressing files");
decompress(path, outPath + "/" + tempFolderName).then(files => {
console.log("Reading manifest");
var manifestText = fs.readFileSync(outPath + "/" + tempFolderName + "/manifest.json", "utf-8");
var manifest = JSON.parse(manifestText);
var outputFolder = outPath != "" ? outPath + "/" + manifest.name : manifest.name;
fs.removeSync(outputFolder);
fs.mkdirSync(outputFolder);
fs.mkdirSync(outputFolder + contentFolderName);
console.log("Copying files");
fs.copySync(outPath + "/" + tempFolderName + "/" + manifest.scope, outputFolder + contentFolderName);
fs.copySync(__dirname + templateFolderName, outputFolder);
var RTapis = fs.readFileSync(outputFolder + "/clockwork/RTpolyfill.js", "utf-8");
var updatedRTapis = RTapis.replace("/*manifest*/{}", JSON.stringify(manifest));
fs.writeFileSync(outputFolder + "/clockwork/RTpolyfill.js", updatedRTapis, "utf-8");
fs.removeSync(outPath + "/" + tempFolderName);
fs.mkdirSync(outputFolder + "/dependencies");
console.log("Downloading dependencies");
var dependencyPromises = [];
for (var name in manifest.dependencies) {
var version = manifest.dependencies[name];
dependencyPromises.push(downloadDependency(outputFolder, name, version));
}
Promise.all(dependencyPromises).then(function () {
console.log("Project exported succesfully");
resolve(outputFolder);
});
});
});
}
function downloadDependency(manifestName, name, version) {
return new Promise(function (resolve, reject) {
request('https://clockworkdev.github.io/ClockworkPackages/packages/' + name + "/" + version + '/components.js', function (error, response, body) {
if (!error && response.statusCode == 200) {
if (body == "") {
downloadDependency(manifestName, name, version).then(resolve);
} else {
fs.mkdirSync(manifestName + "/dependencies/" + name);
fs.writeFileSync(manifestName + "/dependencies/" + name + "/" + version + ".js", body, "utf-8");
resolve();
}
}
});
});
}
module.exports = build;
if (require.main === module) {
var userArguments = process.argv.slice(2);
if (userArguments.length == 1) {
build(userArguments[0], "");
}
}