aboutsummaryrefslogtreecommitdiff
path: root/scripts/update_manifest.js
blob: bcb3ac7bd64b4c1d2c940fe3c40f2791d589f927 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env node

const fs = require("fs");
const crypto = require("crypto");
const path = require("path");
const ArgumentParser = require("argparse").ArgumentParser;

function isFile(file) {
    if (!fs.existsSync(file)) {
        console.error(`File '${file}' not found. Try again.`);
        throw Error();
    }
    return file;
}

const argParser = new ArgumentParser({
    addHelp: true,
    description: "Adds a new latest version to the manifest file based on the " +
        "provided zip file, including an incremental update."
});
argParser.addArgument("manifestFile", {
    metavar: "<manifest file>", type: isFile
});
argParser.addArgument("version", {
    metavar: "<version>"
});
argParser.addArgument([ "-f", "--full" ], {
    metavar: "<full zip file>", type: isFile, nargs: 1,
    dest: "fullZipFile"
});
argParser.addArgument([ "-i", "--incremental" ], {
    type: isFile, nargs: 2, dest: "incremental",
    metavar: ["<incremental zip file>", "<file containing list of deleted files>"]
});
argParser.addArgument([ "-e", "--executable" ], {
    metavar: "[executable file]", nargs: 1,
    dest: "executable"
});

const {
    manifestFile,
    version,
    fullZipFileArgs,
    incrementalArgs,
    executableArgs
} = argParser.parseArgs();

const [incrementalZipFile, deletionsFile] = incrementalArgs || [];
const [fullZipFile] = fullZipFileArgs || [];
const [executable] = executableArgs || [];

const manifest = JSON.parse(fs.readFileSync(manifestFile));

const deleteActions = deletionsFile ? fs.readFileSync(deletionsFile)
    .split("\n").map(file => {
        // XXX: This does not delete empty directories. Finding them would
        // actually be a substantial amount of work because Git does not
        // give us a good way of finding directories that were deleted.
        return { action: "delete", target: file };
    }) : [];

const urlBase = "https://s3.wasabisys.com/ao-downloads/";

manifest.versions = [{
    version,
    executable,
    prev: manifest.versions[0] ? manifest.versions[0].version : undefined,
    full: fullZipFile ? [
        {
            action: "dl",
            url: urlBase + encodeURIComponent(path.basename(fullZipFile)),
            hash: crypto.createHash("sha1")
                .update(fs.readFileSync(fullZipFile))
                .digest("hex")
        }
    ] : undefined,
    update: incrementalArgs ? [
        ...deleteActions,
        {
            action: "dl",
            url: urlBase + encodeURIComponent(path.basename(incrementalZipFile)),
            hash: crypto.createHash("sha1")
                .update(fs.readFileSync(incrementalZipFile))
                .digest("hex")
        }
    ] : undefined
}, ...manifest.versions];

fs.writeFileSync(manifestFile, JSON.stringify(manifest, null, 4));