aboutsummaryrefslogtreecommitdiff
path: root/npmPackages/_scripts/resolve.js
blob: e482e7215341c1e6a8a1743fdcbc9201274cf6c5 (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
90
91
92
93
94
95
96
const process = require('process');
const fs = require('fs');

const {
    pipeThrough,
    readPackage
} = require('./package.js');

const semver = require('./vendor/semver/semver.js');

var jailbreak = false;
var npmPackages = [];

function resolveDeps(pkgMap, deps, missed, unsatisfied) {
    for (var d in deps) {
        if (d in pkgMap) {
            const actual = pkgMap[d].info.version;
            var required = deps[d];
            if (semver.valid(required)) {
                required = `>=${required}`;
            }
            if (!semver.satisfies(actual, required)) {
                unsatisfied(`want ${d}:"${required}", but "${actual}" provided`);
            }
            deps[d] = `file:${pkgMap[d].path}`;
        } else {
            missed(`${d}:${deps[d]}`);
        }
    }

    return deps;
}

function resolvePackage(pkg, depPkgs) {
    const pkgMap = depPkgs.reduce((pkgs, pkg) => {
        pkgs[pkg.info.name] = pkg;
        return pkgs;
    }, {});

    var unsatisfied = [];
    var missed = [];

    function missedCallback(msg) {
        missed.push(msg);
    }

    function unsatisfiedCallback(msg) {
        unsatisfied.push(msg);
    }

    pkg.dependencies = resolveDeps(pkgMap, pkg.dependencies, missedCallback, unsatisfiedCallback);
    pkg.devDependencies = resolveDeps(pkgMap, pkg.devDependencies, missedCallback, unsatisfiedCallback);

    if (missed.length > 0) {
        console.error(`** ERROR: missing dependencies: ${missed.join('; ')}.`);
    }

    if (unsatisfied.length > 0) {
        console.error(`** ${jailbreak ? 'WARNING' : 'ERROR'}: unsatisfied dependencies: ${unsatisfied.join('; ')}.`);
    }

    if (missed.length > 0) {
        process.exit(1);
    }

    if (unsatisfied.length > 0 && !jailbreak) {
        process.exit(1);
    }

    return pkg;
}

const args = process.argv.slice(2);

for (var i = 0; i < args.length; i++) {
    switch (args[i]) {
        case '--jailbreak':
            jailbreak = true;
            break;
        default:
            npmPackages.push(args[i]);
    }
}

console.error('Building with:', npmPackages);

Promise.all(npmPackages.map(readPackage)).then((pkgs) => {
    pipeThrough((pkg, done) => {
        pkg.dependencies = pkg.dependencies || {};
        pkg.devDependencies = pkg.devDependencies || {};
        done(resolvePackage(pkg, pkgs));
    });
}).catch((err) => {
    console.error(err);
    process.exit(1);
});