Skip to content

Commit

Permalink
add currentVersion to cleanupFiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Nov 3, 2024
1 parent 6aaea8b commit de54fb7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
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;
};

0 comments on commit de54fb7

Please sign in to comment.