Skip to content

Commit

Permalink
fix(cap-config-writer): remove cds version check for watch script (#2752
Browse files Browse the repository at this point in the history
)

* 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 <devinea@users.noreply.github.com>

* fix(cap-config-writer): code review

* fix(cap-config-writer): remove warning log

---------

Co-authored-by: Austin Devine <devinea@users.noreply.github.com>
  • Loading branch information
korotkovao and devinea authored Jan 9, 2025
1 parent 4df9313 commit 45c01b0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 83 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-roses-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-ux/cap-config-writer': minor
---

Remove cds version check when adding a cds watch sciprt to root package.json
43 changes: 9 additions & 34 deletions packages/cap-config-writer/src/cap-writer/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -47,40 +46,24 @@ 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<void>} A Promise that resolves once the scripts are updated.
*/
async function updateScripts(
fs: Editor,
packageJsonPath: string,
projectName: string,
appId: string,
enableNPMWorkspaces?: boolean,
cdsUi5PluginInfo?: CdsUi5PluginInfo,
log?: Logger
enableNPMWorkspaces?: boolean
): Promise<void> {
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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 0 additions & 26 deletions packages/cap-config-writer/test/unit/cap-writer/updates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
});
});

0 comments on commit 45c01b0

Please sign in to comment.