From 7f8c32a54bf7d3f712cc85f99b8f58cd7aaa5f38 Mon Sep 17 00:00:00 2001 From: djamaile Date: Wed, 23 Nov 2022 15:50:43 +0100 Subject: [PATCH 1/3] feat: introduce type-deps command in repo-tools package Signed-off-by: djamaile --- package.json | 2 +- packages/repo-tools/package.json | 1 + packages/repo-tools/src/commands/index.ts | 4 ++ .../src/commands/type-deps/type-deps.ts | 45 +++++++++---------- yarn.lock | 1 + 5 files changed, 29 insertions(+), 24 deletions(-) rename scripts/check-type-dependencies.js => packages/repo-tools/src/commands/type-deps/type-deps.ts (86%) mode change 100755 => 100644 diff --git a/package.json b/package.json index 2d2d5f335b..ad75fcdec3 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "lint": "backstage-cli repo lint --since origin/master", "lint:docs": "node ./scripts/check-docs-quality", "lint:all": "backstage-cli repo lint", - "lint:type-deps": "node scripts/check-type-dependencies.js", + "lint:type-deps": "backstage-repo-tools type-deps", "docker-build": "yarn tsc && yarn workspace example-backend build --build-dependencies && yarn workspace example-backend build-image", "new": "backstage-cli new --scope backstage --baseVersion 0.0.0 --no-private", "create-plugin": "echo \"use 'yarn new' instead\"", diff --git a/packages/repo-tools/package.json b/packages/repo-tools/package.json index 60f01c1840..b28e9b22f3 100644 --- a/packages/repo-tools/package.json +++ b/packages/repo-tools/package.json @@ -31,6 +31,7 @@ }, "dependencies": { "@backstage/errors": "workspace:^", + "@manypkg/get-packages": "^1.1.3", "@microsoft/api-documenter": "^7.17.11", "@microsoft/api-extractor": "^7.23.0", "@microsoft/api-extractor-model": "^7.17.2", diff --git a/packages/repo-tools/src/commands/index.ts b/packages/repo-tools/src/commands/index.ts index c4281b0c4f..90d4e22a69 100644 --- a/packages/repo-tools/src/commands/index.ts +++ b/packages/repo-tools/src/commands/index.ts @@ -28,6 +28,10 @@ export function registerCommands(program: Command) { .action( lazy(() => import('./api-reports/api-reports').then(m => m.default)), ); + + program + .command('type-deps') + .action(lazy(() => import('./type-deps/type-deps').then(m => m.default))); } // Wraps an action function so that it always exits and handles errors diff --git a/scripts/check-type-dependencies.js b/packages/repo-tools/src/commands/type-deps/type-deps.ts old mode 100755 new mode 100644 similarity index 86% rename from scripts/check-type-dependencies.js rename to packages/repo-tools/src/commands/type-deps/type-deps.ts index ebad9b6dfc..d4dd28aa14 --- a/scripts/check-type-dependencies.js +++ b/packages/repo-tools/src/commands/type-deps/type-deps.ts @@ -1,6 +1,5 @@ -#!/usr/bin/env node /* - * Copyright 2020 The Backstage Authors + * Copyright 2022 The Backstage Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,14 +14,14 @@ * limitations under the License. */ -const fs = require('fs'); -const { resolve: resolvePath } = require('path'); +import fs from 'fs'; +import { resolve as resolvePath } from 'path'; // Cba polluting root package.json, we'll have this // eslint-disable-next-line import/no-extraneous-dependencies -const chalk = require('chalk'); -const { getPackages } = require('@manypkg/get-packages'); +import chalk from 'chalk'; +import { getPackages, Package } from '@manypkg/get-packages'; -async function main() { +export default async () => { const { packages } = await getPackages(resolvePath('.')); let hadErrors = false; @@ -63,24 +62,29 @@ async function main() { process.exit(2); } -} +}; -function shouldCheckTypes(pkg) { +type PackageJsonWithTypes = { + packageJson: { + types?: string; + }; +}; +function shouldCheckTypes(pkg: Package & PackageJsonWithTypes) { return ( - !pkg.private && + !pkg.packageJson.private && pkg.packageJson.types && fs.existsSync(resolvePath(pkg.dir, 'dist/index.d.ts')) ); } -function findAllDeps(declSrc) { +function findAllDeps(declSrc: string) { const importedDeps = (declSrc.match(/^import .* from '.*';$/gm) || []) - .map(match => match.match(/from '(.*)'/)[1]) + .map(match => match.match(/from '(.*)'/)?.[1] ?? '') .filter(n => !n.startsWith('.')); const referencedDeps = ( declSrc.match(/^\/\/\/ $/gm) || [] ) - .map(match => match.match(/types="(.*)"/)[1]) + .map(match => match.match(/types="(.*)"/)?.[1] ?? '') .filter(n => !n.startsWith('.')) // We allow references to these without an explicit dependency. .filter(n => !['node', 'react'].includes(n)); @@ -91,7 +95,7 @@ function findAllDeps(declSrc) { * Scan index.d.ts for imports and return errors for any dependency that's * missing or incorrect in package.json */ -function checkTypes(pkg) { +function checkTypes(pkg: Package) { const typeDecl = fs.readFileSync( resolvePath(pkg.dir, 'dist/index.d.ts'), 'utf8', @@ -121,7 +125,7 @@ function checkTypes(pkg) { * Find the package used for types. This assumes that types are working is a package * can be resolved, it doesn't do any checking of presence of types inside the dep. */ -function findTypesPackage(dep, pkg) { +function findTypesPackage(dep: string, pkg: Package) { try { require.resolve(`@types/${dep}/package.json`, { paths: [pkg.dir] }); return `@types/${dep}`; @@ -161,7 +165,7 @@ function findTypesPackage(dep, pkg) { /** * Figures out what type dependencies are missing, or should be moved between dep types */ -function findTypeDepErrors(typeDeps, pkg) { +function findTypeDepErrors(typeDeps: string[], pkg: Package) { const devDeps = mkTypeDepSet(pkg.packageJson.devDependencies); const deps = mkTypeDepSet({ ...pkg.packageJson.dependencies, @@ -204,19 +208,14 @@ function findTypeDepErrors(typeDeps, pkg) { return errors; } -function mkTypeDepSet(deps) { +function mkTypeDepSet(deps: Record | undefined) { const typeDeps = Object.keys(deps || {}).filter(n => n.startsWith('@types/')); return new Set(typeDeps); } -function mkErr(name, msg, extra) { +function mkErr(name: string, msg: string, extra: Record) { const error = new Error(msg); error.name = name; Object.assign(error, extra); return error; } - -main().catch(error => { - console.error(error.stack || error); - process.exit(1); -}); diff --git a/yarn.lock b/yarn.lock index bbc526dd57..d06183fd53 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7920,6 +7920,7 @@ __metadata: resolution: "@backstage/repo-tools@workspace:packages/repo-tools" dependencies: "@backstage/errors": "workspace:^" + "@manypkg/get-packages": ^1.1.3 "@microsoft/api-documenter": ^7.17.11 "@microsoft/api-extractor": ^7.23.0 "@microsoft/api-extractor-model": ^7.17.2 From 71f80eb35475a771192611660d7116fce6b8097b Mon Sep 17 00:00:00 2001 From: djamaile Date: Wed, 23 Nov 2022 16:06:50 +0100 Subject: [PATCH 2/3] chore: run api build command Signed-off-by: djamaile --- .changeset/sour-queens-wait.md | 5 +++++ packages/repo-tools/cli-report.md | 10 ++++++++++ packages/repo-tools/src/commands/index.ts | 1 + 3 files changed, 16 insertions(+) create mode 100644 .changeset/sour-queens-wait.md diff --git a/.changeset/sour-queens-wait.md b/.changeset/sour-queens-wait.md new file mode 100644 index 0000000000..75cb1b18c9 --- /dev/null +++ b/.changeset/sour-queens-wait.md @@ -0,0 +1,5 @@ +--- +'@backstage/repo-tools': minor +--- + +add the command type-deps to the repo tool package. diff --git a/packages/repo-tools/cli-report.md b/packages/repo-tools/cli-report.md index 08568827ea..309ddad2a3 100644 --- a/packages/repo-tools/cli-report.md +++ b/packages/repo-tools/cli-report.md @@ -13,6 +13,7 @@ Options: Commands: api-reports [options] [path...] + type-deps help [command] ``` @@ -27,3 +28,12 @@ Options: --docs -h, --help ``` + +### `backstage-repo-tools type-deps` + +``` +Usage: backstage-repo-tools type-deps [options] + +Options: + -h, --help +``` diff --git a/packages/repo-tools/src/commands/index.ts b/packages/repo-tools/src/commands/index.ts index 90d4e22a69..ebbcb5be8e 100644 --- a/packages/repo-tools/src/commands/index.ts +++ b/packages/repo-tools/src/commands/index.ts @@ -31,6 +31,7 @@ export function registerCommands(program: Command) { program .command('type-deps') + .description('Find inconsistencies in types of all packages and plugins') .action(lazy(() => import('./type-deps/type-deps').then(m => m.default))); } From 9b2236fb24188fcaa6a91540ca504e0d9944c18a Mon Sep 17 00:00:00 2001 From: djamaile Date: Thu, 24 Nov 2022 10:44:33 +0100 Subject: [PATCH 3/3] chore: change changeset from minor to patch Signed-off-by: djamaile --- .changeset/sour-queens-wait.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/sour-queens-wait.md b/.changeset/sour-queens-wait.md index 75cb1b18c9..87231a1eb0 100644 --- a/.changeset/sour-queens-wait.md +++ b/.changeset/sour-queens-wait.md @@ -1,5 +1,5 @@ --- -'@backstage/repo-tools': minor +'@backstage/repo-tools': patch --- add the command type-deps to the repo tool package.