Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add currentVersion to cleanupFiles. #27771

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions generators/base/shared-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import assert from 'node:assert';
import { existsSync, readFileSync, statSync } from 'fs';
import { rm } from 'fs/promises';
import { isAbsolute, join, relative } from 'path';
Expand All @@ -25,7 +26,7 @@ import type { MemFsEditor } from 'mem-fs-editor';
import { create } from 'mem-fs-editor';
import { type BaseApplication } from '../base-application/types.js';
import { GENERATOR_JHIPSTER } from '../generator-constants.js';
import { type Control } from './types.js';
import type { CleanupArgumentType, Control } from './types.js';

export default class SharedData<ApplicationType extends BaseApplication = BaseApplication> {
_storage: any;
Expand Down Expand Up @@ -67,7 +68,7 @@ export default class SharedData<ApplicationType extends BaseApplication = BaseAp
});

let customizeRemoveFiles: ((file: string) => string | undefined)[] = [];
const removeFiles = async (assertions: { removedInVersion?: string } | string, ...files: string[]) => {
const removeFiles = async (assertions: { oldVersion?: string; removedInVersion?: string } | string, ...files: string[]) => {
if (typeof assertions === 'string') {
files = [assertions, ...files];
assertions = {};
Expand All @@ -77,8 +78,8 @@ export default class SharedData<ApplicationType extends BaseApplication = BaseAp
files = files.map(customize).filter(file => file) as string[];
}

const { removedInVersion } = assertions;
if (removedInVersion && jhipsterOldVersion && !semverLessThan(jhipsterOldVersion, removedInVersion)) {
const { removedInVersion, oldVersion = jhipsterOldVersion } = assertions;
if (removedInVersion && oldVersion && !semverLessThan(oldVersion, removedInVersion)) {
return;
}

Expand All @@ -104,8 +105,16 @@ export default class SharedData<ApplicationType extends BaseApplication = BaseAp
jhipsterOldVersion,
removeFiles,
customizeRemoveFiles: [],
cleanupFiles: async (cleanup: Record<string, (string | [boolean, ...string[]])[]>) => {
cleanupFiles: async (oldVersionOrCleanup: string | CleanupArgumentType, cleanup?: CleanupArgumentType) => {
if (!jhipsterOldVersion) return;
let oldVersion: string;
if (typeof oldVersionOrCleanup === 'string') {
oldVersion = oldVersionOrCleanup;
assert(cleanup, 'cleanupFiles requires cleanup object');
} else {
cleanup = oldVersionOrCleanup;
oldVersion = jhipsterOldVersion;
}
await Promise.all(
Object.entries(cleanup).map(async ([version, files]) => {
const stringFiles: string[] = [];
Expand All @@ -119,7 +128,7 @@ export default class SharedData<ApplicationType extends BaseApplication = BaseAp
stringFiles.push(file);
}
}
await removeFiles({ removedInVersion: version }, ...stringFiles);
await removeFiles({ oldVersion, removedInVersion: version }, ...stringFiles);
}),
);
},
Expand Down
6 changes: 5 additions & 1 deletion generators/base/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type command from './command.ts';

type BaseApplicationControlProperties = ExportControlPropertiesFromCommand<typeof command>;

export type CleanupArgumentType = Record<string, (string | [boolean, ...string[]])[]>;

export type Control = BaseApplicationControlProperties & {
existingProject: boolean;
ignoreNeedlesError: boolean;
Expand All @@ -23,7 +25,9 @@ export type Control = BaseApplicationControlProperties & {
* Cleanup files conditionally based on version and condition.
* @example
* cleanupFiles({ '6.0.0': ['file1', 'file2', [application.shouldRemove, 'file3']] })
* @example
* cleanupFiles('4.0.0', { '6.0.0': ['file1', 'file2', [application.shouldRemove, 'file3']] })
*/
cleanupFiles: (cleanup: Record<string, (string | [boolean, ...string[]])[]>) => Promise<void>;
cleanupFiles: (cleanup: CleanupArgumentType) => Promise<void> | ((oldVersion: string, cleanup: CleanupArgumentType) => Promise<void>);
getWebappTranslation?: GetWebappTranslationCallback;
};
Loading