Skip to content

Commit

Permalink
feat(deps): track @types packages
Browse files Browse the repository at this point in the history
Mismatches in @types packages significantly reduce
any type safety gained by using TypeScript.

Create a regularly updating issue that tracks all `major.minor`
versions for packages with a matching `@types/*`.
  • Loading branch information
mxdvl committed Jan 17, 2023
1 parent a9daebd commit 453ded8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scripts/ci-deno.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ deno run \
--allow-env="GITHUB_TOKEN" \
--allow-run=yarn,npm \
scripts/deno/peer-dependencies.ts

deno run \
--no-check=remote \
--allow-net \
--allow-env="GITHUB_TOKEN" \
scripts/deno/types-dependencies.ts
78 changes: 78 additions & 0 deletions scripts/deno/types-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import pkg from '../../dotcom-rendering/package.json' assert { type: 'json' };
import { octokit } from './github.ts';

const prefix = '@types/';

/**
* We only look at major.minor versions as per DefinitelyTyped docs:
* https://github.com/DefinitelyTyped/DefinitelyTyped#how-do-definitely-typed-package-versions-relate-to-versions-of-the-corresponding-library
*/
const majorMinorVersions = Object.entries(pkg.dependencies).map(
([dependency, version]) =>
[dependency, version.match(/(\d+\.\d+)/)?.at(0)] as const,
);

const dependencies = majorMinorVersions
.filter(([dependency]) => dependency.startsWith(prefix))
.map(([dependency, version]) => ({
main: majorMinorVersions.find(
([dep]) => dep === dependency.replace(prefix, ''),
),
type: [dependency, version] as const,
}));

const body = `## Audit of \`@types/\` packages
### No main package found
${dependencies
.filter(({ main }) => main === undefined)
.map(
({ type: [dependency, version] }) =>
`- [ ] \`${dependency}\` – ${version}`,
)
.join('\n')}
### Mismatched versions
${dependencies
.filter(({ main, type }) => main !== undefined && main[1] !== type[1])
.map(
({ main, type }) =>
`- [ ] \`${main?.[0]}\` – ${main?.[1]} (types ${type[1]})`,
)
.join('\n')}
### Matched dependencies
${dependencies
.filter(({ main, type }) => main !== undefined && main[1] === type[1])
.map(({ main }) => `- [X] \`${main?.[0]}\` – ${main?.[1]}`)
.join('\n')}
`;

if (!octokit) {
console.log(body);
Deno.exit();
}

/** https://github.com/guardian/dotcom-rendering/issues/6965 */
const issue_number = 6965;

try {
const {
data: { html_url },
} = await octokit.rest.issues.update({
owner: 'guardian',
repo: 'dotcom-rendering',
issue_number,
body,
});

console.info(`Updated list of issues for dotcom-rendering#${issue_number}`);
console.info(html_url);
} catch (error) {
console.warn(`Failed to update issue #${issue_number}`);
console.error(error);

console.log(body);
}

Deno.exit();

0 comments on commit 453ded8

Please sign in to comment.