-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.spec.ts
135 lines (116 loc) · 5.6 KB
/
package.spec.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Test if all package.json files are in sync
// ----------------------------------------------------------------------------
// Import dependencies
const path = require('path'),
fs = require('fs');
// Run tests async
(async () => {
// Read main package.json
const packageJsonMain = JSON.parse(fs.readFileSync(path.join(__dirname, './package.json')));
// Find all other instances of package.json
const packageJsonPaths = (function findFiles (dir, matcherFn, aggregator = []) {
const paths = fs.readdirSync(dir);
for (const name of paths) {
const fullname = path.join(dir, name),
stat = fs.lstatSync(fullname);
if (stat.isDirectory()) {
findFiles(fullname, matcherFn, aggregator);
} else if (matcherFn(name)) {
aggregator.push(fullname.trim());
}
}
return aggregator;
})(
path.join(__dirname, './projects'),
(filename) => (filename === 'package.json')
);
// Check if package.json is in sync across the project
describe('package.json synced', () => {
for (const packageJsonNestedPath of packageJsonPaths) {
// Read nested package.json
const packageJsonNested = JSON.parse(fs.readFileSync(packageJsonNestedPath));
// Test nested package.json
describe(`... with "${packageJsonNestedPath}"`, () => {
// Check package version
it('Matches version', () => {
expect(packageJsonMain.version === packageJsonNested.version).toBeTruthy();
});
// Check package name
it('Matches name', () => {
expect(packageJsonMain.name === packageJsonNested.name).toBeTruthy();
});
// Check package description
it('Matches description', () => {
expect(packageJsonMain.description === packageJsonNested.description).toBeTruthy();
});
// Check package author
it('Matches author', () => {
expect(packageJsonMain.author === packageJsonNested.author).toBeTruthy();
});
// Check package license
it('Matches license', () => {
expect(packageJsonMain.license === packageJsonNested.license).toBeTruthy();
});
// Check package repository
it('Matches repository', () => {
expect(JSON.stringify(packageJsonMain.repository) === JSON.stringify(packageJsonNested.repository)).toBeTruthy();
});
// Check package keywords
it('Matches keywords', () => {
expect(JSON.stringify(packageJsonMain.keywords) === JSON.stringify(packageJsonNested.keywords)).toBeTruthy();
});
// Check package dependency versions
describe('Matches dependency versions', () => {
// Check primary dependencies
if (packageJsonNested.dependencies) {
for (const key of Object.keys(packageJsonNested.dependencies)) {
it(`Matches ${key} version ${packageJsonNested.dependencies[key]}`, () => {
if (packageJsonMain.dependencies && packageJsonMain.dependencies[key]) {
expect(packageJsonNested.dependencies[key] === packageJsonMain.dependencies[key]).toBeTruthy();
}
if (packageJsonMain.devDependencies && packageJsonMain.devDependencies[key]) {
expect(packageJsonNested.dependencies[key] === packageJsonMain.devDependencies[key]).toBeTruthy();
}
if (packageJsonMain.peerDependencies && packageJsonMain.peerDependencies[key]) {
expect(packageJsonNested.dependencies[key] === packageJsonMain.peerDependencies[key]).toBeTruthy();
}
});
}
}
// Check dev dependencies
if (packageJsonNested.devDependencies) {
for (const key of Object.keys(packageJsonNested.devDependencies)) {
it(`Matches ${key} version ${packageJsonNested.devDependencies[key]}`, () => {
if (packageJsonMain.dependencies && packageJsonMain.dependencies[key]) {
expect(packageJsonNested.devDependencies[key] === packageJsonMain.dependencies[key]).toBeTruthy();
}
if (packageJsonMain.devDependencies && packageJsonMain.devDependencies[key]) {
expect(packageJsonNested.devDependencies[key] === packageJsonMain.devDependencies[key]).toBeTruthy();
}
if (packageJsonMain.peerDependencies && packageJsonMain.peerDependencies[key]) {
expect(packageJsonNested.devDependencies[key] === packageJsonMain.peerDependencies[key]).toBeTruthy();
}
});
}
}
// Check peer dependencies
if (packageJsonNested.peerDependencies) {
for (const key of Object.keys(packageJsonNested.peerDependencies)) {
it(`Matches ${key} version ${packageJsonNested.peerDependencies[key]}`, () => {
if (packageJsonMain.dependencies && packageJsonMain.dependencies[key]) {
expect(packageJsonNested.peerDependencies[key] === packageJsonMain.dependencies[key]).toBeTruthy();
}
if (packageJsonMain.devDependencies && packageJsonMain.devDependencies[key]) {
expect(packageJsonNested.peerDependencies[key] === packageJsonMain.devDependencies[key]).toBeTruthy();
}
if (packageJsonMain.peerDependencies && packageJsonMain.peerDependencies[key]) {
expect(packageJsonNested.peerDependencies[key] === packageJsonMain.peerDependencies[key]).toBeTruthy();
}
});
}
}
});
});
}
});
})();