-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckManifest.ts
75 lines (65 loc) · 2.14 KB
/
checkManifest.ts
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
import * as path from 'path';
import * as fs from 'fs';
import * as t from 'io-ts';
import { isLeft } from 'fp-ts/lib/Either';
import * as yaml from 'js-yaml';
import { DependencyHashes } from './hashDependencies';
import { hashesToRecord, DependenciesForManifest } from './writeManifest';
const fsPromises = fs.promises;
const Manifest = t.type({
dependencies: t.record(t.string, t.string),
});
export const dependenciesMatch = (fromManifest: DependenciesForManifest, forManifest: DependenciesForManifest): boolean => {
const unmatchedKeys = new Set(Object.keys(forManifest));
for (const [key, value] of Object.entries(fromManifest)) {
if (!(key in forManifest)) {
return false;
}
if (value !== forManifest[key]) {
return false;
}
unmatchedKeys.delete(key);
}
if (unmatchedKeys.size) {
return false;
}
return true;
};
interface ManifestCheckNeedsUpdate {
needsUpdate: true;
reason: string;
}
interface ManifestCheckNothingToDo {
needsUpdate: false;
}
export type ManifestCheck = ManifestCheckNeedsUpdate | ManifestCheckNothingToDo;
export const checkManifest = async (dirpath: string, targetOutput: string, dependencyHashes: DependencyHashes): Promise<ManifestCheck> => {
let rawManifest;
try {
rawManifest = await fsPromises.readFile(path.join(dirpath, targetOutput, '.dirbuildManifest.yml'), 'utf8');
} catch (err) {
return {
needsUpdate: true,
reason: 'No manifest',
};
}
const manifestYaml: unknown = yaml.safeLoad(rawManifest);
const isManifest = Manifest.decode(manifestYaml);
if (isLeft(isManifest)) {
return {
needsUpdate: true,
reason: 'Manifest file found, but unable to parse',
};
}
const manifest = isManifest.right;
const areEqual = dependenciesMatch(manifest.dependencies, hashesToRecord(dependencyHashes));
if (!areEqual) {
return {
needsUpdate: true,
reason: 'Calculated hashes do not match with existing manifest',
};
}
return {
needsUpdate: false,
};
};