-
Notifications
You must be signed in to change notification settings - Fork 674
Expand file tree
/
Copy pathcheckTsConfigs.js
More file actions
213 lines (180 loc) · 8.16 KB
/
checkTsConfigs.js
File metadata and controls
213 lines (180 loc) · 8.16 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env node
import { dirname, join, relative, resolve } from "path";
import { getPackage, getPackages, PROJECT_ROOT, rootPackageJson } from "./utils/getPackages.js";
import chalk from "chalk";
import yargs from "yargs";
import minimatch from "minimatch";
const { cyan, gray, green, red, yellow } = chalk;
const { _: packagesToCheck } = yargs().argv;
// If a package reference is pointing to a package that is a part of a workspace, return true;
const pathPointsToWorkspacePackage = packageAbsolutePath => {
const workspaces = rootPackageJson.workspaces.packages;
for (let i = 0; i < workspaces.length; i++) {
const absolutePath = resolve(join(PROJECT_ROOT, workspaces[i])).replace(/\\/g, "/");
if (minimatch(packageAbsolutePath, absolutePath)) {
return true;
}
}
return false;
};
const TSCONFIG = {
DEV: "tsconfig.json",
BUILD: "tsconfig.build.json"
};
/**
* This is a small tool that checks if all TS configs in all packages in order. In other words,
* if a "@webiny/*" dependency exists
*
* Usage, check all packages: yarn check-ts-configs
* Usage, specify packages: yarn check-ts-configs @webiny/api-i18n @webiny/api-i18n-content
*/
(async () => {
const errors = {};
let errorsCount = 0;
const warningsCount = 0;
const includes = ["/packages/"];
const workspacesPackages = getPackages({ includes }).filter(pkg => pkg.isTs);
for (const wpObject of workspacesPackages) {
if (packagesToCheck.length) {
if (!packagesToCheck.includes(wpObject.packageJson.name)) {
continue;
}
}
const wpErrors = [];
try {
// 1. Check if all dependencies are listed in TS configs.
// In current workspace package's "package.json", in "dependencies" and "devDependencies", only
// examine dependency packages that are registered as workspaces (e.g. "@webiny/..." packages).
const workspacePackageWbyDepsNames = Object.keys({
...wpObject.packageJson.dependencies,
...wpObject.packageJson.devDependencies,
...wpObject.packageJson.peerDependencies
}).filter(getPackage);
for (const wpWbyDepName of workspacePackageWbyDepsNames) {
const wpWbyDepObject = getPackage(wpWbyDepName);
if (!wpWbyDepObject) {
errorsCount++;
wpErrors.push({
message: `Dependency package "${wpWbyDepName}" not found. Is the package name correct?`
});
continue;
}
// If a dependency is not a TS package, skip it.
if (!wpWbyDepObject.isTs) {
continue;
}
const depPackageRelativePath = relative(
wpObject.packageFolder,
wpWbyDepObject.packageFolder
).replace(/\\/g, "/");
const checkReferences = (config, configType) => {
const checkPath =
configType === TSCONFIG.DEV
? depPackageRelativePath
: `${depPackageRelativePath}/tsconfig.build.json`;
const exists = (config.references || []).find(item => item.path === checkPath);
if (!exists) {
errorsCount++;
wpErrors.push({
package: wpObject,
message: `missing "${wpWbyDepObject.packageJson.name}" in "references" property`,
file: configType
});
}
};
// 1.1 Check tsconfig.json - "references" property.
if (wpObject.tsConfigJson) {
checkReferences(wpObject.tsConfigJson, TSCONFIG.DEV);
}
if (wpObject.tsConfigBuildJson) {
checkReferences(wpObject.tsConfigBuildJson, TSCONFIG.BUILD);
}
}
const checkForExtraPackagesInTsConfig = (wpObject, config, configType) => {
for (const ref of config.references || []) {
// Check if a package is defined in TS config, but not listed in package.json.
const referencePath = resolve(
join(wpObject.packageFolder, dirname(ref.path))
).replace(/\\/g, "/");
const refPackageObject = getPackage(referencePath);
if (refPackageObject) {
if (refPackageObject.isTs) {
const exists = workspacePackageWbyDepsNames.includes(
refPackageObject.packageJson.name
);
if (!exists) {
errorsCount++;
wpErrors.push({
file: configType,
message: `package "${refPackageObject.packageJson.name}" defined in ${configType} ("references" property), but missing in package.json.`
});
}
} else {
errorsCount++;
wpErrors.push({
file: configType,
message: `package "${refPackageObject.packageJson.name}" is not a TypeScript package - remove it from "references"`
});
}
} else {
// Only throw an error if the path points to a workspace folder.
if (pathPointsToWorkspacePackage(referencePath)) {
errorsCount++;
wpErrors.push({
file: configType,
message: `Could not find the package referenced via the "${ref.path}" path in "references" property`
});
}
}
}
};
// 2. Check if TS configs have extra package listed (packages not present in package.json).
checkForExtraPackagesInTsConfig(wpObject, wpObject.tsConfigJson, TSCONFIG.DEV);
checkForExtraPackagesInTsConfig(wpObject, wpObject.tsConfigBuildJson, TSCONFIG.BUILD);
if (wpErrors.length) {
errors[wpObject.packageJson.name] = {
package: wpObject,
errors: wpErrors
};
}
} catch (err) {
console.log(`ERROR IN ${wpObject.packageJson.name}!`, err);
}
}
for (const workspacePackageName in errors) {
const { package: workspacePackageObject, errors: workspacePackageErrors } =
errors[workspacePackageName];
console.log(
`${green(workspacePackageObject.packageJson.name)} (${cyan(
relative(process.cwd(), workspacePackageObject.packageFolder)
)})`
);
const errorsByFiles = workspacePackageErrors.reduce((current, item) => {
if (!current[item.file]) {
current[item.file] = [];
}
current[item.file].push(item);
return current;
}, {});
for (const file in errorsByFiles) {
const fileErrors = errorsByFiles[file];
if (fileErrors.length) {
console.log(` ${gray(file)}`);
for (let i = 0; i < fileErrors.length; i++) {
const fileError = fileErrors[i];
const color = fileError.warning ? yellow : red;
console.log(` ${color(`${i + 1}. ${fileError.message}`)}`);
}
}
}
}
console.log();
console.log(red(`Total errors: ${errorsCount}`));
console.log(yellow(`Total warnings: ${warningsCount}`));
if (errorsCount === 0) {
console.log();
console.log(green("✅ All TS configs in order!"));
process.exit(0);
}
process.exit(1);
})();