From 45c01b0fe0c8e8d4cbdf1b9ac4e54e6346b915ca Mon Sep 17 00:00:00 2001 From: korotkovao <98463389+korotkovao@users.noreply.github.com> Date: Thu, 9 Jan 2025 15:29:25 +0000 Subject: [PATCH] fix(cap-config-writer): remove cds version check for watch script (#2752) * fix(cap-config-writer): remove cds version check for watch script * fix(cap-config-writer): changeset * Apply suggestions from code review Co-authored-by: Austin Devine * fix(cap-config-writer): code review * fix(cap-config-writer): remove warning log --------- Co-authored-by: Austin Devine --- .changeset/thin-roses-shave.md | 5 +++ .../src/cap-writer/package-json.ts | 43 ++++--------------- .../test/unit/cap-writer/package-json.test.ts | 23 ---------- .../test/unit/cap-writer/updates.test.ts | 26 ----------- 4 files changed, 14 insertions(+), 83 deletions(-) create mode 100644 .changeset/thin-roses-shave.md diff --git a/.changeset/thin-roses-shave.md b/.changeset/thin-roses-shave.md new file mode 100644 index 0000000000..d4696eeedc --- /dev/null +++ b/.changeset/thin-roses-shave.md @@ -0,0 +1,5 @@ +--- +'@sap-ux/cap-config-writer': minor +--- + +Remove cds version check when adding a cds watch sciprt to root package.json diff --git a/packages/cap-config-writer/src/cap-writer/package-json.ts b/packages/cap-config-writer/src/cap-writer/package-json.ts index f1b65312a4..290c6e5451 100644 --- a/packages/cap-config-writer/src/cap-writer/package-json.ts +++ b/packages/cap-config-writer/src/cap-writer/package-json.ts @@ -2,10 +2,9 @@ import type { Package } from '@sap-ux/project-access'; import { getCapCustomPaths } from '@sap-ux/project-access'; import type { Editor } from 'mem-fs-editor'; import path, { join } from 'path'; -import type { CdsUi5PluginInfo, CapServiceCdsInfo } from '../cap-config/types'; -import { enableCdsUi5Plugin, checkCdsUi5PluginEnabled, satisfiesMinCdsVersion, minCdsVersion } from '../cap-config'; +import type { CapServiceCdsInfo } from '../cap-config/types'; +import { enableCdsUi5Plugin, checkCdsUi5PluginEnabled } from '../cap-config'; import type { Logger } from '@sap-ux/logger'; -import { t } from '../i18n'; /** * Retrieves the CDS watch script for the CAP app. @@ -47,8 +46,6 @@ function updatePackageJsonWithScripts(fs: Editor, packageJsonPath: string, scrip * @param {string} projectName - The project's name, which is the module name. * @param {string} appId - The application's ID, including its namespace and the module name. * @param {boolean} [enableNPMWorkspaces] - Whether to enable npm workspaces. - * @param {CapServiceCdsInfo} cdsUi5PluginInfo - cds Ui5 plugin info. - * @param {Logger} [log] - The logger instance for logging warnings. * @returns {Promise} A Promise that resolves once the scripts are updated. */ async function updateScripts( @@ -56,31 +53,17 @@ async function updateScripts( packageJsonPath: string, projectName: string, appId: string, - enableNPMWorkspaces?: boolean, - cdsUi5PluginInfo?: CdsUi5PluginInfo, - log?: Logger + enableNPMWorkspaces?: boolean ): Promise { - const packageJson = (fs.readJSON(packageJsonPath) ?? {}) as Package; const hasNPMworkspaces = await checkCdsUi5PluginEnabled(packageJsonPath, fs); - // Determine whether to add cds watch scripts for the app based on the availability of minimum CDS version information. - // If 'cdsUi5PluginInfo' contains version information and it satisfies the minimum required CDS version, - // or if 'cdsUi5PluginInfo' is not available and the version specified in 'package.json' satisfies the minimum required version, - // then set 'addScripts' to true. Otherwise, set it to false. - const addScripts = cdsUi5PluginInfo?.hasMinCdsVersion - ? cdsUi5PluginInfo.hasMinCdsVersion - : satisfiesMinCdsVersion(packageJson); let cdsScript; - if (addScripts) { - if (enableNPMWorkspaces ?? hasNPMworkspaces) { - // If the project uses npm workspaces (and specifically cds-plugin-ui5 ) then the project is served using the appId - cdsScript = getCDSWatchScript(projectName, appId); - } else { - cdsScript = getCDSWatchScript(projectName); - } - updatePackageJsonWithScripts(fs, packageJsonPath, cdsScript); + if (enableNPMWorkspaces ?? hasNPMworkspaces) { + // If the project uses npm workspaces (and specifically cds-plugin-ui5 ) then the project is served using the appId + cdsScript = getCDSWatchScript(projectName, appId); } else { - log?.warn(t('warn.cdsDKNotInstalled', { minCdsVersion: minCdsVersion })); + cdsScript = getCDSWatchScript(projectName); } + updatePackageJsonWithScripts(fs, packageJsonPath, cdsScript); } /** @@ -114,15 +97,7 @@ export async function updateRootPackageJson( await enableCdsUi5Plugin(capService.projectPath, fs); } if (capService?.capType === capNodeType) { - await updateScripts( - fs, - packageJsonPath, - projectName, - appId, - enableNPMWorkspaces, - capService.cdsUi5PluginInfo, - log - ); + await updateScripts(fs, packageJsonPath, projectName, appId, enableNPMWorkspaces); } if (sapux) { const dirPath = join(capService.appPath ?? (await getCapCustomPaths(capService.projectPath)).app, projectName); diff --git a/packages/cap-config-writer/test/unit/cap-writer/package-json.test.ts b/packages/cap-config-writer/test/unit/cap-writer/package-json.test.ts index 6a5129aa29..8548a5cd9b 100644 --- a/packages/cap-config-writer/test/unit/cap-writer/package-json.test.ts +++ b/packages/cap-config-writer/test/unit/cap-writer/package-json.test.ts @@ -69,29 +69,6 @@ describe('Writing/package json files', () => { }); }); - test('should log warning if no minimum cds version is being satisfied', async () => { - const testProjectMinCds = 'test-cap-package-no-min-cds-version'; - const isSapUxEnabled = false; - const logger = new ToolsLogger(); - const loggerMock = jest.fn(); - logger.warn = loggerMock; - (satisfiesMinCdsVersion as jest.Mock).mockReturnValue(false); - const capServiceWithNoMinCds = capService; - capServiceWithNoMinCds.cdsUi5PluginInfo.hasMinCdsVersion = false; - await updateRootPackageJson( - fs, - testProjectMinCds, - isSapUxEnabled, - capServiceWithNoMinCds, - 'test.app.project', - logger - ); - expect(logger.warn).toHaveBeenCalledTimes(1); - expect(logger.warn).toHaveBeenCalledWith( - expect.stringContaining(`minimum cds-dk ${minCdsVersion} version is required to add cds watch scripts`) - ); - }); - test('should enable CdsUi5Plugin when workspace is enabled', async () => { const isSapUxEnabled = true; const isNpmWorkspacesEnabled = true; diff --git a/packages/cap-config-writer/test/unit/cap-writer/updates.test.ts b/packages/cap-config-writer/test/unit/cap-writer/updates.test.ts index e16b76fc8c..3fca7f5e02 100644 --- a/packages/cap-config-writer/test/unit/cap-writer/updates.test.ts +++ b/packages/cap-config-writer/test/unit/cap-writer/updates.test.ts @@ -182,30 +182,4 @@ describe('Test applyCAPUpdates updates files correctly', () => { const applicationYaml = fs.read(applicationYamlPath).toString(); expect(applicationYaml).toContain('spring:\n web.resources.static-locations: file:./app/'); }); - - test('applyCAPUpdates skips updating watch scripts in package.json for CAP Node.js projects when hasMinCdsVersion is false and minimum cds version is not specified', async () => { - const testProjectName = 'test-cap-package-no-min-cds-version'; - const capService: CapServiceCdsInfo = { - ...capInfo, - cdsUi5PluginInfo: { - ...cdsUi5PluginInfo, - hasMinCdsVersion: false - }, - projectPath: join(__dirname, '../cap-writer/test-inputs', testProjectName), - capType: 'Node.js' - }; - const settings: CapProjectSettings = { - appRoot: join(__dirname, '../cap-writer/test-inputs', testProjectName), - packageName: testProjectName, - appId: `${testProjectName}-id` - }; - await applyCAPUpdates(fs, capService, settings); - const packageJsonPath = join(capService.projectPath, 'package.json'); - const packageJson = fs.readJSON(packageJsonPath) as Package; - const scripts = packageJson.scripts; - expect(scripts).toEqual({ - // package json file should not be updated with watch scripts since hasMinCdsVersion is false & minimum cds version is not specified - 'test-script': 'Run some scripts here' - }); - }); });