Skip to content

Commit

Permalink
Update delete-files-plugin to delete files under a folder by default
Browse files Browse the repository at this point in the history
  • Loading branch information
D4N14L committed Jul 19, 2023
1 parent ed67015 commit e818170
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
30 changes: 8 additions & 22 deletions apps/heft/src/plugins/CopyFilesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export async function copyFilesAsync(
await _copyFilesInnerAsync(copyDescriptorByDestination, terminal);
}

function _normalizeCopyOperation(rootPath: string, copyOperation: ICopyOperation): void {
normalizeFileSelectionSpecifier(rootPath, copyOperation);
copyOperation.destinationFolders = copyOperation.destinationFolders.map((x) => path.resolve(rootPath, x));
}

async function _getCopyDescriptorsAsync(
rootPath: string,
copyConfigurations: Iterable<ICopyOperation>,
Expand All @@ -97,11 +102,11 @@ async function _getCopyDescriptorsAsync(
await Async.forEachAsync(
copyConfigurations,
async (copyConfiguration: ICopyOperation) => {
normalizeFileSelectionSpecifier(rootPath, copyConfiguration);
_normalizeCopyOperation(rootPath, copyConfiguration);

// "sourcePath" is required to be a folder. To copy a single file, put the parent folder in "sourcePath"
// and the filename in "includeGlobs". Also, we know that the sourcePath will be set because of the above
// call to normalizeFileSelectionSpecifier
// call to _normalizeCopyOperation
const sourceFolder: string = copyConfiguration.sourcePath!;
const sourceFilePaths: Set<string> | undefined = await getFilePathsAsync(copyConfiguration, fs);

Expand Down Expand Up @@ -194,25 +199,6 @@ async function _copyFilesInnerAsync(
);
}

function* _resolveCopyOperationPaths(
heftConfiguration: HeftConfiguration,
copyOperations: Iterable<ICopyOperation>
): IterableIterator<ICopyOperation> {
const { buildFolderPath } = heftConfiguration;

function resolvePath(inputPath: string | undefined): string {
return inputPath ? path.resolve(buildFolderPath, inputPath) : buildFolderPath;
}

for (const copyOperation of copyOperations) {
yield {
...copyOperation,
sourcePath: resolvePath(copyOperation.sourcePath),
destinationFolders: copyOperation.destinationFolders.map(resolvePath)
};
}
}

const PLUGIN_NAME: 'copy-files-plugin' = 'copy-files-plugin';

export default class CopyFilesPlugin implements IHeftTaskPlugin<ICopyFilesPluginOptions> {
Expand All @@ -224,7 +210,7 @@ export default class CopyFilesPlugin implements IHeftTaskPlugin<ICopyFilesPlugin
taskSession.hooks.registerFileOperations.tap(
PLUGIN_NAME,
(operations: IHeftTaskFileOperations): IHeftTaskFileOperations => {
for (const operation of _resolveCopyOperationPaths(heftConfiguration, pluginOptions.copyOperations)) {
for (const operation of pluginOptions.copyOperations) {
operations.copyOperations.add(operation);
}
return operations;
Expand Down
19 changes: 5 additions & 14 deletions apps/heft/src/plugins/DeleteFilesPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import * as path from 'path';
import { FileSystem, Async, ITerminal } from '@rushstack/node-core-library';

import { Constants } from '../utilities/Constants';
Expand Down Expand Up @@ -34,20 +35,10 @@ async function _getPathsToDeleteAsync(
async (deleteOperation: IDeleteOperation) => {
normalizeFileSelectionSpecifier(rootPath, deleteOperation);

if (
!deleteOperation.fileExtensions?.length &&
!deleteOperation.includeGlobs?.length &&
!deleteOperation.excludeGlobs?.length
) {
// If no globs or file extensions are provided add the path to the set of paths to delete. We know
// that sourcePath is defined because of the above call to normalizeFileSelectionSpecifier.
pathsToDelete.add(deleteOperation.sourcePath!);
} else {
// Glob the files under the source path and add them to the set of files to delete
const sourceFilePaths: Set<string> = await getFilePathsAsync(deleteOperation);
for (const sourceFilePath of sourceFilePaths) {
pathsToDelete.add(sourceFilePath);
}
// Glob the files under the source path and add them to the set of files to delete
const sourceFilePaths: Set<string> = await getFilePathsAsync(deleteOperation);
for (const sourceFilePath of sourceFilePaths) {
pathsToDelete.add(sourceFilePath);
}
},
{ concurrency: Constants.maxParallelism }
Expand Down

0 comments on commit e818170

Please sign in to comment.