From 75ed816cf7396de522d50ed9d3b31c3b4484c24b Mon Sep 17 00:00:00 2001 From: Jhonatan Sandoval Velasco <122501764+JhontSouth@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:07:50 -0500 Subject: [PATCH] fix eslint issues in botbuilder-repo-utils (#4838) --- .../botbuilder-repo-utils/eslint.config.cjs | 10 - libraries/botbuilder-repo-utils/package.json | 3 +- libraries/botbuilder-repo-utils/src/file.ts | 2 - .../src/updateVersions.ts | 194 +++++++++--------- .../botbuilder-repo-utils/src/workspace.ts | 94 +++++---- .../tests/updateVersions.test.ts | 9 +- 6 files changed, 150 insertions(+), 162 deletions(-) delete mode 100644 libraries/botbuilder-repo-utils/eslint.config.cjs diff --git a/libraries/botbuilder-repo-utils/eslint.config.cjs b/libraries/botbuilder-repo-utils/eslint.config.cjs deleted file mode 100644 index 63647b52cc..0000000000 --- a/libraries/botbuilder-repo-utils/eslint.config.cjs +++ /dev/null @@ -1,10 +0,0 @@ -const onlyWarn = require("eslint-plugin-only-warn"); -const sharedConfig = require("../../eslint.config.cjs") - -module.exports = [ - ...sharedConfig, - { - plugins: { - "only-warn": onlyWarn, - }, - }]; diff --git a/libraries/botbuilder-repo-utils/package.json b/libraries/botbuilder-repo-utils/package.json index 4bb1a7c8ee..8fa9531004 100644 --- a/libraries/botbuilder-repo-utils/package.json +++ b/libraries/botbuilder-repo-utils/package.json @@ -14,7 +14,6 @@ "dayjs": "^1.11.13", "fast-glob": "^3.3.2", "lodash": "^4.17.20", - "eslint-plugin-only-warn": "^1.1.0", "minimatch": "^9.0.5", "minimist": "^1.2.6", "p-map": "^4.0.0" @@ -27,7 +26,7 @@ "typescript": "~4.7" }, "scripts": { - "lint": "eslint .", + "lint": "eslint . --config ../../eslint.config.cjs", "test": "mocha -r ts-node/register tests/*.test.ts", "update-versions": "ts-node src/updateVersions.ts" } diff --git a/libraries/botbuilder-repo-utils/src/file.ts b/libraries/botbuilder-repo-utils/src/file.ts index dc653fb965..6978761088 100644 --- a/libraries/botbuilder-repo-utils/src/file.ts +++ b/libraries/botbuilder-repo-utils/src/file.ts @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -/* eslint-disable security/detect-non-literal-fs-filename */ - import fs from 'fs'; import util from 'util'; diff --git a/libraries/botbuilder-repo-utils/src/updateVersions.ts b/libraries/botbuilder-repo-utils/src/updateVersions.ts index 5894824e31..e8d9fd5e22 100644 --- a/libraries/botbuilder-repo-utils/src/updateVersions.ts +++ b/libraries/botbuilder-repo-utils/src/updateVersions.ts @@ -32,7 +32,7 @@ export interface PackageVersionOptions { export const getPackageVersion = ( pkg: Partial, newVersion: string, - options: PackageVersionOptions + options: PackageVersionOptions, ): string => { const prerelease = []; @@ -59,109 +59,111 @@ export const getPackageVersion = ( return compact([newVersion, compact(prerelease).join('.')]).join('-'); }; -export const command = (argv: string[], quiet = false) => async (): Promise => { - // Obtain the path of the repo root, useful for constructing absolute paths - const repoRoot = await gitRoot(); - - const packageFile = await readJsonFile(path.join(repoRoot, 'package.json')); - if (!packageFile) { - return failure('package.json not found', 20); - } - - // Parse process.argv for all configuration options - const { - _: [maybeNewVersion], - ...flags - } = minimist(argv, { - default: { - deprecated: 'deprecated', - git: 'false', - internal: 'internal', - preview: 'preview', - }, - string: ['buildLabel', 'date', 'deprecated', 'git', 'internal', 'preview'], - }); - - // If `maybeNewVersion` is falsy use version from the root packge.json file - const newVersion = maybeNewVersion || packageFile.version; - if (!newVersion) { - return failure('unable to resolve new version', 21); - } - - // Fetch and format date, if instructed - const date = flags.date ? dayjs().format(flags.date) : undefined; - - // Read git commit sha if instructed (JSON.parse properly coerces strings to boolean) - const commitSha = JSON.parse(flags.git) ? await gitSha('HEAD') : undefined; +export const command = + (argv: string[], quiet = false) => + async (): Promise => { + // Obtain the path of the repo root, useful for constructing absolute paths + const repoRoot = await gitRoot(); + + const packageFile = await readJsonFile(path.join(repoRoot, 'package.json')); + if (!packageFile) { + return failure('package.json not found', 20); + } + + // Parse process.argv for all configuration options + const { + _: [maybeNewVersion], + ...flags + } = minimist(argv, { + default: { + deprecated: 'deprecated', + git: 'false', + internal: 'internal', + preview: 'preview', + }, + string: ['buildLabel', 'date', 'deprecated', 'git', 'internal', 'preview'], + }); + + // If `maybeNewVersion` is falsy use version from the root packge.json file + const newVersion = maybeNewVersion || packageFile.version; + if (!newVersion) { + return failure('unable to resolve new version', 21); + } + + // Fetch and format date, if instructed + const date = flags.date ? dayjs().format(flags.date) : undefined; + + // Read git commit sha if instructed (JSON.parse properly coerces strings to boolean) + const commitSha = JSON.parse(flags.git) ? await gitSha('HEAD') : undefined; const projects: string[] = [ ...(packageFile.workspaces?.packages || []), ...(packageFile.workspaces?.generators || []), ]; - // Collect all workspaces from the repo root. Returns workspaces with absolute paths. - const workspaces = await collectWorkspacePackages(repoRoot, projects); - - // Build an object mapping a package name to its new, updated version - const workspaceVersions = workspaces.reduce>( - (acc, { pkg }) => ({ - ...acc, - [pkg.name]: getPackageVersion(pkg, pkg.private ? pkg.version : newVersion, { - buildLabel: flags.buildLabel, - commitSha, - date, - deprecated: flags.deprecated, - internal: flags.internal, - preview: flags.preview, + // Collect all workspaces from the repo root. Returns workspaces with absolute paths. + const workspaces = await collectWorkspacePackages(repoRoot, projects); + + // Build an object mapping a package name to its new, updated version + const workspaceVersions = workspaces.reduce>( + (acc, { pkg }) => ({ + ...acc, + [pkg.name]: getPackageVersion(pkg, pkg.private ? pkg.version : newVersion, { + buildLabel: flags.buildLabel, + commitSha, + date, + deprecated: flags.deprecated, + internal: flags.internal, + preview: flags.preview, + }), }), - }), - {} - ); - - // Rewrites the version for any dependencies found in `workspaceVersions` - const rewriteWithNewVersions = (dependencies: Record) => - Object.entries(dependencies) - .map(([dependency, version]) => [dependency, workspaceVersions[dependency] ?? version]) - .reduce>((acc, [dependency, version]) => { - acc[dependency] = version; - return acc; - }, {}); - - // Rewrite package.json files by updating version as well as dependencies, devDependencies and peerDependencies. - const results = await Promise.all( - workspaces.map(async ({ absPath, pkg }) => { - const newVersion = workspaceVersions[pkg.name]; - - if (newVersion) { - if (!quiet) { - console.log(`Updating ${pkg.name} to ${newVersion}`); + {}, + ); + + // Rewrites the version for any dependencies found in `workspaceVersions` + const rewriteWithNewVersions = (dependencies: Record) => + Object.entries(dependencies) + .map(([dependency, version]) => [dependency, workspaceVersions[dependency] ?? version]) + .reduce>((acc, [dependency, version]) => { + acc[dependency] = version; + return acc; + }, {}); + + // Rewrite package.json files by updating version as well as dependencies, devDependencies and peerDependencies. + const results = await Promise.all( + workspaces.map(async ({ absPath, pkg }) => { + const newVersion = workspaceVersions[pkg.name]; + + if (newVersion) { + if (!quiet) { + console.log(`Updating ${pkg.name} to ${newVersion}`); + } + pkg.version = newVersion.toString(); } - pkg.version = newVersion.toString(); - } - - if (pkg.dependencies) { - pkg.dependencies = rewriteWithNewVersions(pkg.dependencies); - } - - if (pkg.devDependencies) { - pkg.devDependencies = rewriteWithNewVersions(pkg.devDependencies); - } - - if (pkg.peerDependencies) { - pkg.peerDependencies = rewriteWithNewVersions(pkg.peerDependencies); - } - - try { - await writeJsonFile(absPath, pkg); - return success(); - } catch (err: any) { - return failure(err instanceof Error ? err.message : err, 22); - } - }) - ); - - return results.find(isFailure) ?? success(); -}; + + if (pkg.dependencies) { + pkg.dependencies = rewriteWithNewVersions(pkg.dependencies); + } + + if (pkg.devDependencies) { + pkg.devDependencies = rewriteWithNewVersions(pkg.devDependencies); + } + + if (pkg.peerDependencies) { + pkg.peerDependencies = rewriteWithNewVersions(pkg.peerDependencies); + } + + try { + await writeJsonFile(absPath, pkg); + return success(); + } catch (err: any) { + return failure(err instanceof Error ? err.message : err, 22); + } + }), + ); + + return results.find(isFailure) ?? success(); + }; if (require.main === module) { run(command(process.argv.slice(2))); diff --git a/libraries/botbuilder-repo-utils/src/workspace.ts b/libraries/botbuilder-repo-utils/src/workspace.ts index aa2feea71c..b2b43641a4 100644 --- a/libraries/botbuilder-repo-utils/src/workspace.ts +++ b/libraries/botbuilder-repo-utils/src/workspace.ts @@ -38,7 +38,7 @@ export interface Filters { export async function collectWorkspacePackages( repoRoot: string, workspaces: string[] = [], - filters: Partial = {} + filters: Partial = {}, ): Promise> { // Note: posix is required, this emits absolute paths that are platform specific const paths = await glob( @@ -48,54 +48,52 @@ export async function collectWorkspacePackages( ); const maybeWorkspaces = await Promise.all( - paths.map( - async (absPath): Promise => { - let relPath = absPath.replace(repoRoot, ''); - if (relPath[0] === path.sep) { - relPath = relPath.slice(1); - } - - // Strip `package.json` filename for path filters - const relWorkspacePath = path.dirname(relPath); - - if (filters.path?.length && !filters.path.some((path) => minimatch(relWorkspacePath, path))) { - return; - } - - if ( - filters.ignorePath?.length && - filters.ignorePath.some((ignorePath) => minimatch(relWorkspacePath, ignorePath)) - ) { - return; - } - - const pkg = await readJsonFile(absPath); - if (!pkg) { - return undefined; - } - - if (filters.noPrivate && pkg.private) { - return; - } - - if (filters.script && !(pkg.scripts ?? {})[filters.script]) { - return; - } - - if (filters.name?.length && !filters.name.some((name) => minimatch(pkg.name, name))) { - return; - } - - if ( - filters.ignoreName?.length && - filters.ignoreName.some((ignoreName) => minimatch(pkg.name, ignoreName)) - ) { - return; - } - - return { absPath, pkg, relPath }; + paths.map(async (absPath): Promise => { + let relPath = absPath.replace(repoRoot, ''); + if (relPath[0] === path.sep) { + relPath = relPath.slice(1); } - ) + + // Strip `package.json` filename for path filters + const relWorkspacePath = path.dirname(relPath); + + if (filters.path?.length && !filters.path.some((path) => minimatch(relWorkspacePath, path))) { + return; + } + + if ( + filters.ignorePath?.length && + filters.ignorePath.some((ignorePath) => minimatch(relWorkspacePath, ignorePath)) + ) { + return; + } + + const pkg = await readJsonFile(absPath); + if (!pkg) { + return undefined; + } + + if (filters.noPrivate && pkg.private) { + return; + } + + if (filters.script && !(pkg.scripts ?? {})[filters.script]) { + return; + } + + if (filters.name?.length && !filters.name.some((name) => minimatch(pkg.name, name))) { + return; + } + + if ( + filters.ignoreName?.length && + filters.ignoreName.some((ignoreName) => minimatch(pkg.name, ignoreName)) + ) { + return; + } + + return { absPath, pkg, relPath }; + }), ); return compact(maybeWorkspaces); diff --git a/libraries/botbuilder-repo-utils/tests/updateVersions.test.ts b/libraries/botbuilder-repo-utils/tests/updateVersions.test.ts index b2f9ec1f3b..70c5927067 100644 --- a/libraries/botbuilder-repo-utils/tests/updateVersions.test.ts +++ b/libraries/botbuilder-repo-utils/tests/updateVersions.test.ts @@ -8,7 +8,7 @@ import assert from 'assert'; import dayjs from 'dayjs'; import path from 'path'; import semver from 'semver'; -import sinon from 'sinon'; +import { createSandbox, match } from 'sinon'; import { Package } from '../src/package'; import { command, getPackageVersion } from '../src/updateVersions'; import { isSuccess } from '../src/run'; @@ -147,8 +147,9 @@ describe('updateVersions', function () { describe('command', function () { let sandbox: sinon.SinonSandbox; + beforeEach(function () { - sandbox = sinon.createSandbox(); + sandbox = createSandbox(); }); afterEach(function () { @@ -223,10 +224,10 @@ describe('updateVersions', function () { ), }); - let packageMatch = sinon.match.hasOwn('version', workspace.expectedVersion); + let packageMatch = match.hasOwn('version', workspace.expectedVersion); if (workspace.expectedDependencies) { packageMatch = packageMatch.and( - sinon.match.hasOwn('dependencies', sinon.match(workspace.expectedDependencies)), + match.hasOwn('dependencies', match(workspace.expectedDependencies)), ); }