From a493617d5c710d78246d315e67fa7eb2d7b6b9f9 Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Fri, 5 Jan 2024 15:27:03 +0100 Subject: [PATCH] feat: replace lerna-alias with nx APIs (#30149) --- package.json | 2 - packages/fluentui/accessibility/package.json | 3 +- packages/fluentui/e2e/package.json | 1 - packages/fluentui/perf/gulp/webpack.config.ts | 2 +- packages/fluentui/react-bindings/package.json | 3 +- packages/fluentui/react-builder/package.json | 3 +- .../package.json | 1 - .../package.json | 3 +- .../fluentui/react-component-ref/package.json | 3 +- .../react-icons-northstar/package.json | 3 +- .../package.json | 3 +- .../package.json | 1 - .../package.json | 3 +- .../fluentui/react-northstar/package.json | 1 - .../fluentui/react-proptypes/package.json | 3 +- .../fluentui/react-telemetry/package.json | 3 +- packages/fluentui/state/package.json | 3 +- packages/fluentui/styles/package.json | 3 +- scripts/gulp/src/config.ts | 33 ++++++++--- .../gulp/src/tasks/test-dependencies/utils.ts | 3 +- .../gulp/src/webpack/webpack.config.e2e.ts | 2 +- scripts/gulp/src/webpack/webpack.config.ts | 9 ++- scripts/jest/src/jest.preset.v0.js | 7 +-- scripts/lint-staged/eslint.js | 18 +++--- scripts/monorepo/src/get-lerna-aliases.js | 16 ----- scripts/monorepo/src/getAllPackageInfo.js | 22 ++++--- scripts/monorepo/src/index.d.ts | 2 +- scripts/monorepo/src/index.js | 2 +- scripts/monorepo/src/types.ts | 7 ++- scripts/monorepo/src/workspace-utils.js | 47 +++++++++++++++ scripts/monorepo/src/workspace-utils.spec.ts | 58 +++++++++++++++++++ .../update-release-notes/changelogsAndTags.ts | 10 ++-- scripts/update-release-notes/package.json | 3 +- syncpack.config.js | 1 - yarn.lock | 19 ------ 35 files changed, 187 insertions(+), 116 deletions(-) delete mode 100644 scripts/monorepo/src/get-lerna-aliases.js create mode 100644 scripts/monorepo/src/workspace-utils.js create mode 100644 scripts/monorepo/src/workspace-utils.spec.ts diff --git a/package.json b/package.json index 3f12d23b4f4de..bb9984e5d4667 100644 --- a/package.json +++ b/package.json @@ -162,7 +162,6 @@ "@types/jest-axe": "3.5.5", "@types/jju": "1.4.1", "@types/json-schema": "^7.0.8", - "@types/lerna-alias": "3.0.0", "@types/loader-utils": "2.0.3", "@types/lodash": "4.14.182", "@types/markdown-table": "2.0.0", @@ -272,7 +271,6 @@ "just-scripts": "1.8.2", "lage": "1.8.8", "lerna": "7.1.3", - "lerna-alias": "3.0.3-0", "license-webpack-plugin": "2.3.10", "lint-staged": "10.2.10", "loader-utils": "2.0.4", diff --git a/packages/fluentui/accessibility/package.json b/packages/fluentui/accessibility/package.json index cc88a21d733f3..b202e7a9ef768 100644 --- a/packages/fluentui/accessibility/package.json +++ b/packages/fluentui/accessibility/package.json @@ -12,8 +12,7 @@ "@fluentui/eslint-plugin": "*", "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", - "@fluentui/scripts-jest": "*", - "lerna-alias": "^3.0.3-0" + "@fluentui/scripts-jest": "*" }, "files": [ "dist" diff --git a/packages/fluentui/e2e/package.json b/packages/fluentui/e2e/package.json index 1ca5e7d0a0d01..4e7eac043d6cd 100644 --- a/packages/fluentui/e2e/package.json +++ b/packages/fluentui/e2e/package.json @@ -10,7 +10,6 @@ "@fluentui/react-northstar": "*", "@fluentui/react-northstar-prototypes": "*", "@types/react-router-dom": "^5.1.5", - "lerna-alias": "^3.0.3-0", "lodash": "^4.17.15", "react-router-dom": "^5.2.0" }, diff --git a/packages/fluentui/perf/gulp/webpack.config.ts b/packages/fluentui/perf/gulp/webpack.config.ts index 7469e569b24ae..1bbfd5b36b99e 100644 --- a/packages/fluentui/perf/gulp/webpack.config.ts +++ b/packages/fluentui/perf/gulp/webpack.config.ts @@ -56,7 +56,7 @@ export const webpackConfig: webpack.Configuration = { }, extensions: ['.ts', '.tsx', '.js', '.json'], alias: { - ...config.lernaAliases({ type: 'webpack' }), + ...config.webpackAliases, // We are using React in production mode with tracing. // https://gist.github.com/bvaughn/25e6233aeb1b4f0cdb8d8366e54a3977 diff --git a/packages/fluentui/react-bindings/package.json b/packages/fluentui/react-bindings/package.json index eefd0cb18d826..04984535185fd 100644 --- a/packages/fluentui/react-bindings/package.json +++ b/packages/fluentui/react-bindings/package.json @@ -24,8 +24,7 @@ "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", "@fluentui/scripts-jest": "*", - "@types/classnames": "^2.2.9", - "lerna-alias": "^3.0.3-0" + "@types/classnames": "^2.2.9" }, "files": [ "dist" diff --git a/packages/fluentui/react-builder/package.json b/packages/fluentui/react-builder/package.json index b29f1f719ecda..1a2c71bbeefce 100644 --- a/packages/fluentui/react-builder/package.json +++ b/packages/fluentui/react-builder/package.json @@ -26,8 +26,7 @@ "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", "@fluentui/scripts-jest": "*", - "@types/react-frame-component": "^4.1.1", - "lerna-alias": "^3.0.3-0" + "@types/react-frame-component": "^4.1.1" }, "peerDependencies": { "@babel/standalone": "^7.8.3", diff --git a/packages/fluentui/react-component-event-listener/package.json b/packages/fluentui/react-component-event-listener/package.json index 8df497f8a00aa..7f7f38bbfcf63 100644 --- a/packages/fluentui/react-component-event-listener/package.json +++ b/packages/fluentui/react-component-event-listener/package.json @@ -13,7 +13,6 @@ "@fluentui/scripts-gulp": "*", "@fluentui/scripts-jest": "*", "@types/simulant": "^0.2.0", - "lerna-alias": "^3.0.3-0", "simulant": "^0.2.2" }, "files": [ diff --git a/packages/fluentui/react-component-nesting-registry/package.json b/packages/fluentui/react-component-nesting-registry/package.json index 721a51e806f2a..99ad89c84af13 100644 --- a/packages/fluentui/react-component-nesting-registry/package.json +++ b/packages/fluentui/react-component-nesting-registry/package.json @@ -12,8 +12,7 @@ "@fluentui/eslint-plugin": "*", "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", - "@fluentui/scripts-jest": "*", - "lerna-alias": "^3.0.3-0" + "@fluentui/scripts-jest": "*" }, "files": [ "dist" diff --git a/packages/fluentui/react-component-ref/package.json b/packages/fluentui/react-component-ref/package.json index 178ec5d121093..cebb0dd5c8db1 100644 --- a/packages/fluentui/react-component-ref/package.json +++ b/packages/fluentui/react-component-ref/package.json @@ -12,8 +12,7 @@ "@fluentui/eslint-plugin": "*", "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", - "@fluentui/scripts-jest": "*", - "lerna-alias": "^3.0.3-0" + "@fluentui/scripts-jest": "*" }, "files": [ "dist" diff --git a/packages/fluentui/react-icons-northstar/package.json b/packages/fluentui/react-icons-northstar/package.json index 3405dc06c554a..b683ce63ba962 100644 --- a/packages/fluentui/react-icons-northstar/package.json +++ b/packages/fluentui/react-icons-northstar/package.json @@ -15,8 +15,7 @@ "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", "@fluentui/scripts-jest": "*", - "@types/classnames": "^2.2.9", - "lerna-alias": "^3.0.3-0" + "@types/classnames": "^2.2.9" }, "files": [ "dist" diff --git a/packages/fluentui/react-northstar-emotion-renderer/package.json b/packages/fluentui/react-northstar-emotion-renderer/package.json index e55e670e78253..06effdb8e6a4f 100644 --- a/packages/fluentui/react-northstar-emotion-renderer/package.json +++ b/packages/fluentui/react-northstar-emotion-renderer/package.json @@ -19,8 +19,7 @@ "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", "@fluentui/scripts-jest": "*", - "@types/stylis": "4.0.0", - "lerna-alias": "^3.0.3-0" + "@types/stylis": "4.0.0" }, "files": [ "dist" diff --git a/packages/fluentui/react-northstar-fela-renderer/package.json b/packages/fluentui/react-northstar-fela-renderer/package.json index d73420597f9bf..4c2446ccae9e5 100644 --- a/packages/fluentui/react-northstar-fela-renderer/package.json +++ b/packages/fluentui/react-northstar-fela-renderer/package.json @@ -25,7 +25,6 @@ "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", "@fluentui/scripts-jest": "*", - "lerna-alias": "^3.0.3-0", "react-fela": "^10.6.1" }, "files": [ diff --git a/packages/fluentui/react-northstar-styles-renderer/package.json b/packages/fluentui/react-northstar-styles-renderer/package.json index ebdbd2f4e4708..cbbc2707a8e03 100644 --- a/packages/fluentui/react-northstar-styles-renderer/package.json +++ b/packages/fluentui/react-northstar-styles-renderer/package.json @@ -10,8 +10,7 @@ "devDependencies": { "@fluentui/eslint-plugin": "*", "@fluentui/scripts-babel": "*", - "@fluentui/scripts-gulp": "*", - "lerna-alias": "^3.0.3-0" + "@fluentui/scripts-gulp": "*" }, "files": [ "dist" diff --git a/packages/fluentui/react-northstar/package.json b/packages/fluentui/react-northstar/package.json index 82adfa9947fdf..1f53ec95b3e3e 100644 --- a/packages/fluentui/react-northstar/package.json +++ b/packages/fluentui/react-northstar/package.json @@ -41,7 +41,6 @@ "csstype": "^3.0.2", "faker": "^4.1.0", "fela-tools": "^10.6.1", - "lerna-alias": "^3.0.3-0", "qs": "^6.8.0", "simulant": "^0.2.2" }, diff --git a/packages/fluentui/react-proptypes/package.json b/packages/fluentui/react-proptypes/package.json index 9b035b6c2d558..e351514133ce3 100644 --- a/packages/fluentui/react-proptypes/package.json +++ b/packages/fluentui/react-proptypes/package.json @@ -13,8 +13,7 @@ "@fluentui/eslint-plugin": "*", "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", - "@fluentui/scripts-jest": "*", - "lerna-alias": "^3.0.3-0" + "@fluentui/scripts-jest": "*" }, "files": [ "dist" diff --git a/packages/fluentui/react-telemetry/package.json b/packages/fluentui/react-telemetry/package.json index 8959d4afa2e84..618b3eac14321 100644 --- a/packages/fluentui/react-telemetry/package.json +++ b/packages/fluentui/react-telemetry/package.json @@ -15,8 +15,7 @@ "@fluentui/eslint-plugin": "*", "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", - "@types/react-table": "^7.0.19", - "lerna-alias": "^3.0.3-0" + "@types/react-table": "^7.0.19" }, "files": [ "dist" diff --git a/packages/fluentui/state/package.json b/packages/fluentui/state/package.json index dbe30d9715e57..55f763234a4aa 100644 --- a/packages/fluentui/state/package.json +++ b/packages/fluentui/state/package.json @@ -10,8 +10,7 @@ "devDependencies": { "@fluentui/eslint-plugin": "*", "@fluentui/scripts-babel": "*", - "@fluentui/scripts-gulp": "*", - "lerna-alias": "^3.0.3-0" + "@fluentui/scripts-gulp": "*" }, "files": [ "dist" diff --git a/packages/fluentui/styles/package.json b/packages/fluentui/styles/package.json index b58ba70d9f45a..649b63a0cf429 100644 --- a/packages/fluentui/styles/package.json +++ b/packages/fluentui/styles/package.json @@ -13,8 +13,7 @@ "@fluentui/eslint-plugin": "*", "@fluentui/scripts-babel": "*", "@fluentui/scripts-gulp": "*", - "@fluentui/scripts-jest": "*", - "lerna-alias": "^3.0.3-0" + "@fluentui/scripts-jest": "*" }, "files": [ "dist" diff --git a/scripts/gulp/src/config.ts b/scripts/gulp/src/config.ts index 16eccae5149f0..6aa5ae4197adf 100644 --- a/scripts/gulp/src/config.ts +++ b/scripts/gulp/src/config.ts @@ -1,15 +1,32 @@ /* eslint-disable @typescript-eslint/naming-convention */ import * as path from 'path'; -import * as _ from 'lodash'; -import * as webpack from 'webpack'; import { URL } from 'url'; -import { getLernaAliases } from '@fluentui/scripts-monorepo'; +import { getWorkspaceProjects, getWorkspaceProjectsAliases } from '@fluentui/scripts-monorepo'; +import * as _ from 'lodash'; +import * as webpack from 'webpack'; -// northstar packages should pull these from npm, not the repo -const excludedPackages = ['@fluentui/dom-utilities']; -const lernaAliases = (options: Omit[number], 'excludedPackages'>) => - getLernaAliases({ excludedPackages, ...options }); +const excludedPackages = [ + // northstar packages should pull these from npm, not the repo + '@fluentui/dom-utilities', + // we need to apply aliases only for v0 projects - otherwise webpack will explode. This will(can) be mitigated by implementing no-build needed for v0 e2e (https://github.com/microsoft/fluentui/pull/26928/commits/6dbc149b54c251ba74b3ebb70057846389ed3f88#diff-f1ffee4111d5db5b78ed08c5318068a3b5e313d6dbc72b60231cc65e4bd5eb2a) + ...getProjectsExceptV0(), +]; + +const webpackAliases = getWorkspaceProjectsAliases({ excludeProjects: excludedPackages, type: 'webpack' }); + +function getProjectsExceptV0() { + const projectConfigs = getWorkspaceProjects().values(); + const projects: string[] = []; + for (const projectConfig of projectConfigs) { + const tags = projectConfig.tags ?? []; + if (!tags.includes('react-northstar')) { + projects.push(projectConfig.name!); + } + } + + return projects; +} // ------------------------------------ // Environment vars @@ -182,7 +199,7 @@ const config = { isRoot, /** Package name the task is running against: default to react if running at root, or cwd otherwise */ package: packageName, - lernaAliases, + webpackAliases, }; export default config; diff --git a/scripts/gulp/src/tasks/test-dependencies/utils.ts b/scripts/gulp/src/tasks/test-dependencies/utils.ts index 9aa11389b1d04..39bc79226e5cf 100644 --- a/scripts/gulp/src/tasks/test-dependencies/utils.ts +++ b/scripts/gulp/src/tasks/test-dependencies/utils.ts @@ -1,4 +1,5 @@ import * as path from 'path'; + import { LicenseWebpackPlugin } from 'license-webpack-plugin'; import webpack from 'webpack'; @@ -59,7 +60,7 @@ export const prepareWebpackConfig = (options: WebpackOptions) => { resolve: { extensions: ['.ts', '.tsx', '.js', '.json'], alias: { - ...config.lernaAliases({ type: 'webpack' }), + ...config.webpackAliases, src: paths.packageSrc('react-northstar'), }, }, diff --git a/scripts/gulp/src/webpack/webpack.config.e2e.ts b/scripts/gulp/src/webpack/webpack.config.e2e.ts index 850ec70a551ad..91f72fed1b17d 100644 --- a/scripts/gulp/src/webpack/webpack.config.e2e.ts +++ b/scripts/gulp/src/webpack/webpack.config.e2e.ts @@ -57,7 +57,7 @@ const webpackConfig: webpack.Configuration = { path: require.resolve('path-browserify'), }, extensions: ['.ts', '.tsx', '.js', '.json'], - alias: config.lernaAliases({ type: 'webpack' }), + alias: config.webpackAliases, }, performance: { hints: false, // to (temporarily) disable "WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit") diff --git a/scripts/gulp/src/webpack/webpack.config.ts b/scripts/gulp/src/webpack/webpack.config.ts index 245df3332c74b..c6502c690ff05 100644 --- a/scripts/gulp/src/webpack/webpack.config.ts +++ b/scripts/gulp/src/webpack/webpack.config.ts @@ -1,13 +1,12 @@ +import { getDefaultEnvironmentVars } from '@fluentui/scripts-monorepo'; import CopyWebpackPlugin from 'copy-webpack-plugin'; +import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'; import HtmlWebpackPlugin from 'html-webpack-plugin'; import _ from 'lodash'; -import webpack from 'webpack'; import TerserPlugin from 'terser-webpack-plugin'; -import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'; +import webpack from 'webpack'; import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'; -import { getDefaultEnvironmentVars } from '@fluentui/scripts-monorepo'; - import config from '../config'; const { paths } = config; @@ -125,7 +124,7 @@ const webpackConfig: webpack.Configuration & resolve: { extensions: ['.ts', '.tsx', '.js', '.json'], alias: { - ...config.lernaAliases({ type: 'webpack' }), + ...config.webpackAliases, src: paths.packageSrc('react-northstar'), faker: 'faker/locale/en', 'react-hook-form': 'react-hook-form/dist/react-hook-form.ie11', diff --git a/scripts/jest/src/jest.preset.v0.js b/scripts/jest/src/jest.preset.v0.js index 5d47d3e20b9f5..a5b4c027948a2 100644 --- a/scripts/jest/src/jest.preset.v0.js +++ b/scripts/jest/src/jest.preset.v0.js @@ -1,4 +1,4 @@ -const { getLernaAliases, workspaceRoot } = require('@fluentui/scripts-monorepo'); +const { getWorkspaceProjectsAliases } = require('@fluentui/scripts-monorepo'); // northstar packages should pull these from npm, not the repo const excludedPackages = ['@fluentui/dom-utilities']; @@ -20,10 +20,9 @@ const createConfig = (/** @type {import('@jest/types').Config.InitialOptions} */ clearMocks: true, ...customConfig, moduleNameMapper: { - ...getLernaAliases({ + ...getWorkspaceProjectsAliases({ type: 'jest', - excludedPackages, - directory: workspaceRoot, + excludeProjects: excludedPackages, }), ...customConfig.moduleNameMapper, }, diff --git a/scripts/lint-staged/eslint.js b/scripts/lint-staged/eslint.js index d083754272b4b..d127501da4e19 100644 --- a/scripts/lint-staged/eslint.js +++ b/scripts/lint-staged/eslint.js @@ -6,7 +6,8 @@ const fs = require('fs'); const os = require('os'); const path = require('path'); const { promisify } = require('util'); -const { rollup: lernaAliases } = require('lerna-alias'); + +const { getWorkspaceProjects, workspaceRoot } = require('@fluentui/scripts-monorepo'); const { default: PQueue } = require('p-queue'); const exec = promisify(child_process.exec); @@ -28,13 +29,14 @@ function groupFilesByPackage() { /** @type {{ [packagePath: string]: string[] }} */ const filesByPackage = {}; - const packagesWithEslint = Object.values(lernaAliases({ sourceDirectory: false })).filter( - packagePath => - // exclude @fluentui/noop (northstar packages root) - path.basename(packagePath) !== 'fluentui' && - // only include packages with an eslintrc (any extension) - fs.readdirSync(packagePath).some(f => f.startsWith('.eslintrc')), - ); + const packagesWithEslint = []; + const projects = getWorkspaceProjects(); + for (const [, projectConfig] of projects) { + const absoluteRootPath = path.join(workspaceRoot, projectConfig.root); + if (fs.readdirSync(absoluteRootPath).some(f => f.startsWith('.eslintrc'))) { + packagesWithEslint.push(absoluteRootPath); + } + } for (const file of files) { // eslint-disable-next-line no-shadow diff --git a/scripts/monorepo/src/get-lerna-aliases.js b/scripts/monorepo/src/get-lerna-aliases.js deleted file mode 100644 index 81e9f7e414f7c..0000000000000 --- a/scripts/monorepo/src/get-lerna-aliases.js +++ /dev/null @@ -1,16 +0,0 @@ -const lernaAlias = require('lerna-alias'); - -/** - * - * @param {{excludedPackages:string[];type:keyof typeof lernaAlias} & lernaAlias.Options} options - */ -function getLernaAliases(options) { - const { excludedPackages, type, ...lernaOptions } = options; - const aliases = lernaAlias[type](lernaOptions); - for (const pkg of excludedPackages) { - delete aliases[`^${pkg}$`]; - } - return aliases; -} - -exports.getLernaAliases = getLernaAliases; diff --git a/scripts/monorepo/src/getAllPackageInfo.js b/scripts/monorepo/src/getAllPackageInfo.js index b40b9ce4f4b0f..d3c2941a75306 100644 --- a/scripts/monorepo/src/getAllPackageInfo.js +++ b/scripts/monorepo/src/getAllPackageInfo.js @@ -1,7 +1,9 @@ -const fs = require('fs-extra'); +const fs = require('fs'); const path = require('path'); -const lernaAlias = require('lerna-alias'); -const findGitRoot = require('./findGitRoot'); + +const { workspaceRoot } = require('@nx/devkit'); + +const { getWorkspaceProjects } = require('./workspace-utils'); /** * @type {import('./types').AllPackageInfo} @@ -20,19 +22,15 @@ function getAllPackageInfo() { return packageInfo; } - // Get mapping from package name to package path - // (rollup helper happens to be good for getting basic package name/path pairs) - const packagePaths = lernaAlias.rollup({ sourceDirectory: false }); - delete packagePaths['@fluentui/noop']; // not a real package + const projects = getWorkspaceProjects(); packageInfo = {}; cwdForPackageInfo = process.cwd(); - const gitRoot = findGitRoot(); - for (const [packageName, packagePath] of Object.entries(packagePaths)) { - packageInfo[packageName] = { - packagePath: path.relative(gitRoot, packagePath), - packageJson: fs.readJSONSync(path.join(packagePath, 'package.json')), + for (const [projectName, projectConfig] of projects) { + packageInfo[projectName] = { + packagePath: projectConfig.root, + packageJson: JSON.parse(fs.readFileSync(path.join(workspaceRoot, projectConfig.root, 'package.json'), 'utf-8')), }; } diff --git a/scripts/monorepo/src/index.d.ts b/scripts/monorepo/src/index.d.ts index 53d016949011c..38fe8c6964ec0 100644 --- a/scripts/monorepo/src/index.d.ts +++ b/scripts/monorepo/src/index.d.ts @@ -5,9 +5,9 @@ export { default as findRepoDeps } from './findRepoDeps'; export { default as getAllPackageInfo } from './getAllPackageInfo'; export { isConvergedPackage, shipsAMD } from './isConvergedPackage'; export { getAffectedPackages } from './getAffectedPackages'; -export { getLernaAliases } from './get-lerna-aliases'; export { getDefaultEnvironmentVars } from './getDefaultEnvironmentVars'; export { getProjectMetadata, workspaceRoot, getUncommittedFiles, getUntrackedFiles } from './utils'; export * as eslintConstants from './eslint-constants'; export { getNthCommit } from './getNthCommit'; export { tree, flushTreeChanges } from './tree'; +export { getWorkspaceProjects, getWorkspaceProjectsAliases } from './workspace-utils'; diff --git a/scripts/monorepo/src/index.js b/scripts/monorepo/src/index.js index 0b55f2b4196ba..29876e9cb4645 100644 --- a/scripts/monorepo/src/index.js +++ b/scripts/monorepo/src/index.js @@ -8,7 +8,7 @@ module.exports = { ...require('./getAffectedPackages'), ...require('./getNthCommit'), ...require('./getDefaultEnvironmentVars'), - ...require('./get-lerna-aliases'), ...require('./utils'), ...require('./tree'), + ...require('./workspace-utils'), }; diff --git a/scripts/monorepo/src/types.ts b/scripts/monorepo/src/types.ts index b12748e1241ed..2f6e8b39e954f 100644 --- a/scripts/monorepo/src/types.ts +++ b/scripts/monorepo/src/types.ts @@ -19,4 +19,9 @@ export interface PackageInfo { packageJson: PackageJson; } -export type AllPackageInfo = { [packageName: string]: PackageInfo }; +/** + * packageName - valid npm package name including scope. Example: `@fluentui/foo-bar` + */ +export type AllPackageInfo = { + [packageName: string]: PackageInfo; +}; diff --git a/scripts/monorepo/src/workspace-utils.js b/scripts/monorepo/src/workspace-utils.js new file mode 100644 index 0000000000000..76809d79a09ac --- /dev/null +++ b/scripts/monorepo/src/workspace-utils.js @@ -0,0 +1,47 @@ +const path = require('path'); + +const { getProjects, workspaceRoot } = require('@nx/devkit'); + +const { tree } = require('./tree'); + +function getWorkspaceProjects(_tree = tree) { + const projects = getProjects(_tree); + + return projects; +} + +/** + * + * @param {{excludeProjects?: string[]; type?:'default' | 'webpack' | 'jest'}} [options] + */ +function getWorkspaceProjectsAliases(options = {}) { + const { type = 'default', excludeProjects = [] } = options; + const projects = getWorkspaceProjects(); + + for (const projectName of excludeProjects) { + projects.delete(projectName); + } + + /** + * @type {Record} + */ + const aliases = {}; + + for (const [projectName, projectConfig] of projects) { + const key = getAliasKey[type](projectName); + const sourceRoot = projectConfig.sourceRoot ?? path.join(projectConfig.root, 'src'); + const entry = path.join(sourceRoot, 'index'); + aliases[key] = path.join(workspaceRoot, entry); + } + + return aliases; +} + +const getAliasKey = { + jest: (/** @type {string} */ packageName) => `^${packageName}$`, + webpack: (/** @type {string} */ packageName) => `${packageName}$`, + default: (/** @type {string} */ packageName) => packageName, +}; + +exports.getWorkspaceProjects = getWorkspaceProjects; +exports.getWorkspaceProjectsAliases = getWorkspaceProjectsAliases; diff --git a/scripts/monorepo/src/workspace-utils.spec.ts b/scripts/monorepo/src/workspace-utils.spec.ts new file mode 100644 index 0000000000000..fb0f80d93457f --- /dev/null +++ b/scripts/monorepo/src/workspace-utils.spec.ts @@ -0,0 +1,58 @@ +import { workspaceRoot } from '@nx/devkit'; + +import { getWorkspaceProjects, getWorkspaceProjectsAliases } from './workspace-utils'; + +describe(`workspace-utils`, () => { + describe(`#getWorkspaceProjects`, () => { + it(`should return Map of all workspace valid/registered projects`, () => { + const actual = getWorkspaceProjects(); + expect(actual.has('@fluentui/noop')).toEqual(false); + + expect(actual.has('@fluentui/react-text')).toBe(true); + expect(actual.get('@fluentui/react-text')).toEqual( + expect.objectContaining({ + $schema: expect.any(String), + implicitDependencies: expect.any(Array), + name: '@fluentui/react-text', + projectType: 'library', + root: expect.any(String), + sourceRoot: expect.any(String), + tags: expect.any(Array), + }), + ); + }); + }); + + describe(`#getWorkspaceProjectsAliases`, () => { + const projectSourcePathRegex = new RegExp(workspaceRoot + '[a-z_/-]*/src/index$'); + + it(`should create alias mapping`, () => { + const actual = getWorkspaceProjectsAliases(); + + expect(actual['@fluentui/noop']).toEqual(undefined); + + const keys = Object.keys(actual); + const values = Object.values(actual); + expect(keys).toEqual(expect.arrayContaining([expect.stringMatching(/@fluentui\/[a-z-]+[a-z]$/)])); + expect(values).toEqual(expect.arrayContaining([expect.stringMatching(projectSourcePathRegex)])); + }); + + it(`should exclude specified projects`, () => { + const actual = getWorkspaceProjectsAliases({ excludeProjects: ['@fluentui/react-components'] }); + + expect(actual['@fluentui/react-components']).toEqual(undefined); + }); + + it.each([ + { type: 'webpack', expected: /@fluentui\/[a-z-]+\$$/ }, + { type: 'jest', expected: /^\^@fluentui\/[a-z-]+\$$/ }, + ] as const)(`should create alias mapping for 'type=$type'`, ({ type, expected }) => { + const actual = getWorkspaceProjectsAliases({ type }); + + const keys = Object.keys(actual); + const values = Object.values(actual); + expect(keys).toEqual(expect.arrayContaining([expect.stringMatching(expected)])); + expect(values).toEqual(expect.arrayContaining([expect.stringMatching(projectSourcePathRegex)])); + }); + }); +}); diff --git a/scripts/update-release-notes/changelogsAndTags.ts b/scripts/update-release-notes/changelogsAndTags.ts index 74b4208559fb9..a07f20f83e9d2 100644 --- a/scripts/update-release-notes/changelogsAndTags.ts +++ b/scripts/update-release-notes/changelogsAndTags.ts @@ -1,9 +1,9 @@ import { ExecFileException, execSync } from 'child_process'; import * as path from 'path'; +import { getWorkspaceProjects } from '@fluentui/scripts-monorepo'; import { ChangelogJson } from 'beachball'; import * as fs from 'fs-extra'; -import { rollup as lernaAliases } from 'lerna-alias'; import { IChangelogEntry } from './types'; @@ -19,12 +19,10 @@ export function getTagToChangelogMap(maxAgeDays?: number): Map(); - // Get all package directories so we can check if they have a CHANGELOG.json - // (sourceDirectory: false means don't append src/index) - const packagePaths = Object.values(lernaAliases({ sourceDirectory: false })); + const workspaceProjects = getWorkspaceProjects(); - for (const packagePath of packagePaths) { - const changelogPath = path.join(packagePath, 'CHANGELOG.json'); + for (const [, projectConfig] of workspaceProjects) { + const changelogPath = path.join(projectConfig.root, 'CHANGELOG.json'); if (fs.existsSync(changelogPath)) { const changelog: ChangelogJson = fs.readJSONSync(changelogPath); for (const entry of changelog.entries) { diff --git a/scripts/update-release-notes/package.json b/scripts/update-release-notes/package.json index 46a0ddb4851e8..be0ecfd781467 100644 --- a/scripts/update-release-notes/package.json +++ b/scripts/update-release-notes/package.json @@ -11,6 +11,7 @@ "type-check": "tsc -b tsconfig.json" }, "dependencies": { - "@fluentui/scripts-github": "*" + "@fluentui/scripts-github": "*", + "@fluentui/scripts-monorepo": "*" } } diff --git a/syncpack.config.js b/syncpack.config.js index 163627aed882c..ee08fc680c92c 100644 --- a/syncpack.config.js +++ b/syncpack.config.js @@ -56,7 +56,6 @@ const config = { 'fs-extra', 'glob', 'jju', - 'lerna-alias', 'loader-utils', 'lodash', 'schema-utils', diff --git a/yarn.lock b/yarn.lock index cfaeb7380bcc9..3766da3efcbe2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5356,11 +5356,6 @@ dependencies: "@types/node" "*" -"@types/lerna-alias@3.0.0": - version "3.0.0" - resolved "https://registry.yarnpkg.com/@types/lerna-alias/-/lerna-alias-3.0.0.tgz#e4d70f75bad9f5a7b697f76b7d7b3fa97aa57844" - integrity sha512-EfwEzSWxAxrZgUJT5sECi4RPFCIgOm/K9e5EWXp2hR9H6wzV1QIBgjGtGEl/OpOCcglNWzmqrVRX/qjvNwli9Q== - "@types/loader-utils@2.0.3": version "2.0.3" resolved "https://registry.yarnpkg.com/@types/loader-utils/-/loader-utils-2.0.3.tgz#fbc2337358f8f4a7dc532ac0a3646c74275edf2d" @@ -13375,13 +13370,6 @@ get-intrinsic@^1.0.2, get-intrinsic@^1.1.0, get-intrinsic@^1.1.1: has "^1.0.3" has-symbols "^1.0.1" -get-lerna-packages@^0.1.1: - version "0.1.1" - resolved "https://registry.yarnpkg.com/get-lerna-packages/-/get-lerna-packages-0.1.1.tgz#37aaaac36fd14f00082f17b1096dd6c69290cef9" - integrity sha512-venxkvga57gOUZoTmXZlJ0zHc8cmvsx7Y0GTMcoEK/7OCEK3PLpG5gVTbUUMJaLxbzK13AWQdKPf1LSkuEfWqA== - dependencies: - glob "^7.1.2" - get-own-enumerable-property-symbols@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-3.0.0.tgz#b877b49a5c16aefac3655f2ed2ea5b684df8d203" @@ -17127,13 +17115,6 @@ lead@^1.0.0: dependencies: flush-write-stream "^1.0.2" -lerna-alias@3.0.3-0, lerna-alias@^3.0.3-0: - version "3.0.3-0" - resolved "https://registry.yarnpkg.com/lerna-alias/-/lerna-alias-3.0.3-0.tgz#d681842deed66644ab9f38a7b365658cfe52dd43" - integrity sha512-IX0c2QH3v4rkgE+iG7Ro0FSEWKqqrMhvbGbB1txrnaJ8YbcrNbJ0IPpcJi+uiiPLV3olNX74EO08x9hGZ+J5ig== - dependencies: - get-lerna-packages "^0.1.1" - lerna@7.1.3: version "7.1.3" resolved "https://registry.yarnpkg.com/lerna/-/lerna-7.1.3.tgz#9aef15620b54038b1e4c5ea620c6b9b7590f26a4"