Skip to content

Commit 6c232d8

Browse files
committed
feat: add scripts to check pkg in attw / expectedVersionFailures
1 parent 28846ba commit 6c232d8

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env node
2+
3+
import { join as joinPaths } from "node:path";
4+
5+
import { getTypesVersions } from "@definitelytyped/header-parser";
6+
import { mangleScopedPackage } from "@definitelytyped/utils";
7+
8+
import { checkNpmVersionAndGetMatchingImplementationPackage, checkPackageJson } from "./checks";
9+
import { findDTRootAndPackageNameFrom, packageDirectoryNameWithVersionFromPath } from "./util";
10+
11+
async function main(): Promise<void> {
12+
const args = process.argv.slice(2);
13+
14+
console.log(`dtslint@${require("../package.json").version}`);
15+
if (args.length === 1 && args[0] === "types") {
16+
console.log(
17+
"Please provide a package name to test.\nTo test all changed packages at once, run `pnpm run test-all`.",
18+
);
19+
process.exit(1);
20+
}
21+
22+
const dirPath = args.reduce((acc, arg) => joinPaths(acc, mangleScopedPackage(arg)), "");
23+
24+
console.log(`Should ${dirPath} stay in attw? ${await shouldPackageStayInAttw(dirPath)}`);
25+
}
26+
27+
/**
28+
* @returns Warning text - should be displayed during the run, but does not indicate failure.
29+
*/
30+
async function shouldPackageStayInAttw(dirPath: string): Promise<boolean> {
31+
try {
32+
await findDTRootAndPackageNameFrom(dirPath);
33+
} catch {
34+
return false;
35+
}
36+
37+
const packageJson = checkPackageJson(dirPath, getTypesVersions(dirPath));
38+
if (Array.isArray(packageJson)) {
39+
console.error(new Error("\n\t* " + packageJson.join("\n\t* ")));
40+
41+
return false;
42+
}
43+
44+
const packageDirectoryNameWithVersion = packageDirectoryNameWithVersionFromPath(dirPath);
45+
46+
const { implementationPackage } = await checkNpmVersionAndGetMatchingImplementationPackage(
47+
packageJson,
48+
packageDirectoryNameWithVersion,
49+
);
50+
51+
return Boolean(implementationPackage);
52+
}
53+
54+
if (require.main === module) {
55+
main().catch((err) => {
56+
console.error(err.stack);
57+
process.exit(1);
58+
});
59+
} else {
60+
module.exports = shouldPackageStayInAttw;
61+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env node
2+
3+
import { join as joinPaths } from "node:path";
4+
5+
import { getTypesVersions } from "@definitelytyped/header-parser";
6+
import { mangleScopedPackage } from "@definitelytyped/utils";
7+
8+
import { checkNpmVersionAndGetMatchingImplementationPackage, checkPackageJson } from "./checks";
9+
import { findDTRootAndPackageNameFrom, packageDirectoryNameWithVersionFromPath } from "./util";
10+
11+
async function main(): Promise<void> {
12+
const args = process.argv.slice(2);
13+
14+
console.log(`dtslint@${require("../package.json").version}`);
15+
if (args.length === 1 && args[0] === "types") {
16+
console.log(
17+
"Please provide a package name to test.\nTo test all changed packages at once, run `pnpm run test-all`.",
18+
);
19+
process.exit(1);
20+
}
21+
22+
const dirPath = args.reduce((acc, arg) => joinPaths(acc, mangleScopedPackage(arg)), process.cwd());
23+
24+
console.log(
25+
`Should ${dirPath} stay in expectedNpmVersionFailures? ${await shouldPackageStayInExpectedVersionFailures(dirPath)}`,
26+
);
27+
}
28+
29+
/**
30+
* @returns Warning text - should be displayed during the run, but does not indicate failure.
31+
*/
32+
async function shouldPackageStayInExpectedVersionFailures(dirPath: string): Promise<boolean> {
33+
try {
34+
await findDTRootAndPackageNameFrom(dirPath);
35+
} catch {
36+
return false;
37+
}
38+
39+
const packageJson = checkPackageJson(dirPath, getTypesVersions(dirPath));
40+
if (Array.isArray(packageJson)) {
41+
console.error(new Error("\n\t* " + packageJson.join("\n\t* ")));
42+
43+
return false;
44+
}
45+
46+
const packageDirectoryNameWithVersion = packageDirectoryNameWithVersionFromPath(dirPath);
47+
48+
const { warnings } = await checkNpmVersionAndGetMatchingImplementationPackage(
49+
packageJson,
50+
packageDirectoryNameWithVersion,
51+
);
52+
53+
const partialRemovableWarning = " can be removed from expectedNpmVersionFailures.txt in ";
54+
const canBeRemovedForDirPath = warnings.some((msg) => msg.includes(partialRemovableWarning));
55+
56+
return !canBeRemovedForDirPath;
57+
}
58+
59+
if (require.main === module) {
60+
main().catch((err) => {
61+
console.error(err.stack);
62+
process.exit(1);
63+
});
64+
} else {
65+
module.exports = shouldPackageStayInExpectedVersionFailures;
66+
}

0 commit comments

Comments
 (0)