|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as path from 'node:path'; |
| 5 | +import { FileSystem, JsonFile } from '@rushstack/node-core-library'; |
| 6 | +import { TestUtilities } from '@rushstack/heft-config-file'; |
| 7 | +import { RushConfiguration } from '../../api/RushConfiguration'; |
| 8 | + |
| 9 | +describe('RushPnpmCommandLineParser', () => { |
| 10 | + describe('catalog syncing', () => { |
| 11 | + let testRepoPath: string; |
| 12 | + let pnpmConfigPath: string; |
| 13 | + let pnpmWorkspacePath: string; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + testRepoPath = path.join(__dirname, 'temp', 'catalog-sync-test-repo'); |
| 17 | + FileSystem.ensureFolder(testRepoPath); |
| 18 | + |
| 19 | + const rushJsonPath: string = path.join(testRepoPath, 'rush.json'); |
| 20 | + const rushJson = { |
| 21 | + $schema: 'https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json', |
| 22 | + rushVersion: '5.166.0', |
| 23 | + pnpmVersion: '10.28.1', |
| 24 | + nodeSupportedVersionRange: '>=18.0.0', |
| 25 | + projects: [] |
| 26 | + }; |
| 27 | + JsonFile.save(rushJson, rushJsonPath, { ensureFolderExists: true }); |
| 28 | + |
| 29 | + const configDir: string = path.join(testRepoPath, 'common', 'config', 'rush'); |
| 30 | + FileSystem.ensureFolder(configDir); |
| 31 | + |
| 32 | + pnpmConfigPath = path.join(configDir, 'pnpm-config.json'); |
| 33 | + const pnpmConfig = { |
| 34 | + globalCatalogs: { |
| 35 | + default: { |
| 36 | + react: '^18.0.0', |
| 37 | + 'react-dom': '^18.0.0' |
| 38 | + } |
| 39 | + } |
| 40 | + }; |
| 41 | + JsonFile.save(pnpmConfig, pnpmConfigPath); |
| 42 | + |
| 43 | + const tempDir: string = path.join(testRepoPath, 'common', 'temp'); |
| 44 | + FileSystem.ensureFolder(tempDir); |
| 45 | + |
| 46 | + pnpmWorkspacePath = path.join(tempDir, 'pnpm-workspace.yaml'); |
| 47 | + const workspaceYaml = `packages: |
| 48 | + - '../../apps/*' |
| 49 | +catalogs: |
| 50 | + default: |
| 51 | + react: ^18.0.0 |
| 52 | + react-dom: ^18.0.0 |
| 53 | +`; |
| 54 | + FileSystem.writeFile(pnpmWorkspacePath, workspaceYaml); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + if (FileSystem.exists(testRepoPath)) { |
| 59 | + FileSystem.deleteFolder(testRepoPath); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it('syncs updated catalogs from pnpm-workspace.yaml to pnpm-config.json', () => { |
| 64 | + const updatedWorkspaceYaml = `packages: |
| 65 | + - '../../apps/*' |
| 66 | +catalogs: |
| 67 | + default: |
| 68 | + react: ^18.2.0 |
| 69 | + react-dom: ^18.2.0 |
| 70 | + typescript: ~5.3.0 |
| 71 | + frontend: |
| 72 | + vue: ^3.4.0 |
| 73 | +`; |
| 74 | + FileSystem.writeFile(pnpmWorkspacePath, updatedWorkspaceYaml); |
| 75 | + |
| 76 | + const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( |
| 77 | + path.join(testRepoPath, 'rush.json') |
| 78 | + ); |
| 79 | + |
| 80 | + const subspace = rushConfiguration.getSubspace('default'); |
| 81 | + const pnpmOptions = subspace.getPnpmOptions(); |
| 82 | + |
| 83 | + expect(TestUtilities.stripAnnotations(pnpmOptions?.globalCatalogs)).toEqual({ |
| 84 | + default: { |
| 85 | + react: '^18.0.0', |
| 86 | + 'react-dom': '^18.0.0' |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile'); |
| 91 | + const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath); |
| 92 | + |
| 93 | + pnpmOptions?.updateGlobalCatalogs(newCatalogs); |
| 94 | + |
| 95 | + const updatedRushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( |
| 96 | + path.join(testRepoPath, 'rush.json') |
| 97 | + ); |
| 98 | + const updatedSubspace = updatedRushConfiguration.getSubspace('default'); |
| 99 | + const updatedPnpmOptions = updatedSubspace.getPnpmOptions(); |
| 100 | + |
| 101 | + expect(TestUtilities.stripAnnotations(updatedPnpmOptions?.globalCatalogs)).toEqual({ |
| 102 | + default: { |
| 103 | + react: '^18.2.0', |
| 104 | + 'react-dom': '^18.2.0', |
| 105 | + typescript: '~5.3.0' |
| 106 | + }, |
| 107 | + frontend: { |
| 108 | + vue: '^3.4.0' |
| 109 | + } |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + it('handles singular catalog field from pnpm', () => { |
| 114 | + const workspaceWithSingularCatalog = `packages: |
| 115 | + - '../../apps/*' |
| 116 | +catalog: |
| 117 | + react: ^18.2.0 |
| 118 | + react-dom: ^18.2.0 |
| 119 | +`; |
| 120 | + FileSystem.writeFile(pnpmWorkspacePath, workspaceWithSingularCatalog); |
| 121 | + |
| 122 | + const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile'); |
| 123 | + const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath); |
| 124 | + |
| 125 | + const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( |
| 126 | + path.join(testRepoPath, 'rush.json') |
| 127 | + ); |
| 128 | + const subspace = rushConfiguration.getSubspace('default'); |
| 129 | + const pnpmOptions = subspace.getPnpmOptions(); |
| 130 | + |
| 131 | + pnpmOptions?.updateGlobalCatalogs(newCatalogs); |
| 132 | + |
| 133 | + const updatedRushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( |
| 134 | + path.join(testRepoPath, 'rush.json') |
| 135 | + ); |
| 136 | + const updatedSubspace = updatedRushConfiguration.getSubspace('default'); |
| 137 | + const updatedPnpmOptions = updatedSubspace.getPnpmOptions(); |
| 138 | + |
| 139 | + expect(TestUtilities.stripAnnotations(updatedPnpmOptions?.globalCatalogs)).toEqual({ |
| 140 | + default: { |
| 141 | + react: '^18.2.0', |
| 142 | + 'react-dom': '^18.2.0' |
| 143 | + } |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it('does not update pnpm-config.json when catalogs are unchanged', () => { |
| 148 | + const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile'); |
| 149 | + const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath); |
| 150 | + |
| 151 | + const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( |
| 152 | + path.join(testRepoPath, 'rush.json') |
| 153 | + ); |
| 154 | + const subspace = rushConfiguration.getSubspace('default'); |
| 155 | + const pnpmOptions = subspace.getPnpmOptions(); |
| 156 | + |
| 157 | + pnpmOptions?.updateGlobalCatalogs(newCatalogs); |
| 158 | + |
| 159 | + const savedConfig = JsonFile.load(pnpmConfigPath); |
| 160 | + expect(savedConfig.globalCatalogs).toEqual({ |
| 161 | + default: { |
| 162 | + react: '^18.0.0', |
| 163 | + 'react-dom': '^18.0.0' |
| 164 | + } |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it('removes catalogs when pnpm-workspace.yaml has no catalogs', () => { |
| 169 | + const workspaceWithoutCatalogs = `packages: |
| 170 | + - '../../apps/*' |
| 171 | +`; |
| 172 | + FileSystem.writeFile(pnpmWorkspacePath, workspaceWithoutCatalogs); |
| 173 | + |
| 174 | + const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile'); |
| 175 | + const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath); |
| 176 | + |
| 177 | + expect(newCatalogs).toBeUndefined(); |
| 178 | + |
| 179 | + const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( |
| 180 | + path.join(testRepoPath, 'rush.json') |
| 181 | + ); |
| 182 | + const subspace = rushConfiguration.getSubspace('default'); |
| 183 | + const pnpmOptions = subspace.getPnpmOptions(); |
| 184 | + |
| 185 | + pnpmOptions?.updateGlobalCatalogs(newCatalogs); |
| 186 | + |
| 187 | + const updatedRushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( |
| 188 | + path.join(testRepoPath, 'rush.json') |
| 189 | + ); |
| 190 | + const updatedSubspace = updatedRushConfiguration.getSubspace('default'); |
| 191 | + const updatedPnpmOptions = updatedSubspace.getPnpmOptions(); |
| 192 | + |
| 193 | + expect(updatedPnpmOptions?.globalCatalogs).toBeUndefined(); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments