aboutsummaryrefslogtreecommitdiffstats
path: root/packages/monorepo-scripts/src/deps_versions.ts
blob: 0d01a2e3ca37d5d5030fe1a56aa29c9667e25e88 (plain) (blame)
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
97
98
99
100
101
#!/usr/bin/env node

import chalk from 'chalk';
import * as fs from 'fs';
import { sync as globSync } from 'glob';
import * as path from 'path';

import { utils } from './utils/utils';

interface Dependencies {
    [depName: string]: string;
}
interface Versions {
    [packageName: string]: string;
}
interface VersionsByDependency {
    [depName: string]: Versions;
}

interface ParsedDependencies {
    ignored: VersionsByDependency;
    included: VersionsByDependency;
}

const PACKAGE_JSON_GLOB = '../../*/package.json';

const config = JSON.parse(fs.readFileSync(path.join(__dirname, '../../../package.json')).toString()).config;
const dependenciesWithIgnoredVersions: string[] = config.ignoreDependencyVersions.split(' ');
const packagesWithIgnoredVersions: string[] = config.ignoreDependencyVersionsForPackage.split(' ');

if (require.main === module) {
    const dependencies = parseDependencies();
    const ignoredMultiples = getDependenciesWithMultipleVersions(dependencies.ignored);
    const multiples = getDependenciesWithMultipleVersions(dependencies.included);
    printVersionsByDependency(multiples);
    utils.log(`├── ${chalk.bold('IGNORED')}`);
    printVersionsByDependency(ignoredMultiples);
    if (Object.keys(multiples).length !== 0) {
        utils.log(
            `Some dependencies have multiple versions. Please fix by trying to find compatible versions. As a last resort, you can add space-separated exceptions to root package.json config.ignoreDependencyVersions`,
        );
        process.exit(1);
    }
}

function getDependencies(_path: string): Dependencies {
    const file = fs.readFileSync(_path).toString();
    const parsed = JSON.parse(file);
    const dependencies = {
        ...parsed.dependencies,
        ...parsed.devDependencies,
    };
    return dependencies;
}

function parseDependencies(): ParsedDependencies {
    const files = globSync(path.join(__dirname, PACKAGE_JSON_GLOB));
    const parsedDependencies: ParsedDependencies = {
        ignored: {},
        included: {},
    };
    files.map(_path => {
        const pathParts = _path.split('/');
        const packageName = pathParts[pathParts.length - 2];
        const packageCategory = packagesWithIgnoredVersions.includes(packageName) ? 'ignored' : 'included';
        const dependencies = getDependencies(_path);
        Object.keys(dependencies).forEach((depName: string) => {
            const category = dependenciesWithIgnoredVersions.includes(depName) ? 'ignored' : packageCategory;
            if (parsedDependencies[category][depName] === undefined) {
                parsedDependencies[category][depName] = {};
            }
            const version = dependencies[depName];
            parsedDependencies[category][depName][packageName] = version;
        });
    });
    return parsedDependencies;
}

function getDependenciesWithMultipleVersions(versionsByDependency: VersionsByDependency): VersionsByDependency {
    return Object.keys(versionsByDependency)
        .filter((depName: string) => hasMultipleVersions(versionsByDependency[depName]))
        .reduce<VersionsByDependency>((obj, depName: string) => {
            obj[depName] = versionsByDependency[depName];
            return obj;
        }, {});
}

function printVersionsByDependency(versionsByDependency: VersionsByDependency): void {
    Object.keys(versionsByDependency).forEach((depName: string) => {
        const versions: Versions = versionsByDependency[depName];
        utils.log(chalk.bold(depName));
        Object.keys(versions).forEach((packageName: string) => {
            utils.log(`├── ${packageName} -> ${versions[packageName]}`);
        });
    });
}

function hasMultipleVersions(versions: Versions): boolean {
    const uniques = new Set(Object.values(versions));
    return uniques.size > 1;
}