From de54fb7622dee2d22bb558c5090ebfc36f681efd Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Sat, 2 Nov 2024 22:15:59 -0300 Subject: [PATCH] add currentVersion to cleanupFiles. --- generators/base/shared-data.ts | 21 +++++++++++++++------ generators/base/types.d.ts | 6 +++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/generators/base/shared-data.ts b/generators/base/shared-data.ts index 76c8b1781692..a7dcc28fd244 100644 --- a/generators/base/shared-data.ts +++ b/generators/base/shared-data.ts @@ -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'; @@ -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 { _storage: any; @@ -67,7 +68,7 @@ export default class SharedData 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 = {}; @@ -77,8 +78,8 @@ export default class SharedData file) as string[]; } - const { removedInVersion } = assertions; - if (removedInVersion && jhipsterOldVersion && !semverLessThan(jhipsterOldVersion, removedInVersion)) { + const { removedInVersion, oldVersion = jhipsterOldVersion } = assertions; + if (removedInVersion && oldVersion && !semverLessThan(oldVersion, removedInVersion)) { return; } @@ -104,8 +105,16 @@ export default class SharedData) => { + 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[] = []; @@ -119,7 +128,7 @@ export default class SharedData; +export type CleanupArgumentType = Record; + export type Control = BaseApplicationControlProperties & { existingProject: boolean; ignoreNeedlesError: boolean; @@ -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) => Promise; + cleanupFiles: (cleanup: CleanupArgumentType) => Promise | ((oldVersion: string, cleanup: CleanupArgumentType) => Promise); getWebappTranslation?: GetWebappTranslationCallback; };