|
| 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