-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathCopyFilesPlugin.ts
More file actions
287 lines (253 loc) · 9.86 KB
/
CopyFilesPlugin.ts
File metadata and controls
287 lines (253 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { createHash } from 'node:crypto';
import type * as fs from 'node:fs';
import * as path from 'node:path';
import { AlreadyExistsBehavior, FileSystem, Async } from '@rushstack/node-core-library';
import type { ITerminal } from '@rushstack/terminal';
import { Constants } from '../utilities/Constants';
import {
asAbsoluteFileSelectionSpecifier,
getFileSelectionSpecifierPathsAsync,
type IFileSelectionSpecifier
} from './FileGlobSpecifier';
import type { HeftConfiguration } from '../configuration/HeftConfiguration';
import type { IHeftTaskPlugin } from '../pluginFramework/IHeftPlugin';
import type { IHeftTaskSession, IHeftTaskFileOperations } from '../pluginFramework/HeftTaskSession';
import type { WatchFileSystemAdapter } from '../utilities/WatchFileSystemAdapter';
import {
type IIncrementalBuildInfo,
makePathRelative,
tryReadBuildInfoAsync,
writeBuildInfoAsync
} from '../pluginFramework/IncrementalBuildInfo';
import type {
CopyFilesHeftTaskEventOptions as ICopyFilesPluginOptions,
CopyOperationBase as ICopyOperationBase
} from '../schemas/copy-files-options.schema.json.d.ts';
export type { ICopyOperationBase };
/**
* Used to specify a selection of files to copy from a specific source folder to one
* or more destination folders.
*
* @public
*/
export interface ICopyOperation extends IFileSelectionSpecifier, ICopyOperationBase {}
/**
* Used to specify a selection of files to copy from a specific source folder to one
* or more destination folders.
*
* @public
*/
export interface IIncrementalCopyOperation extends ICopyOperation {
/**
* If true, the file will be copied only if the source file is contained in the
* IHeftTaskRunIncrementalHookOptions.changedFiles map.
*/
onlyIfChanged?: boolean;
}
interface ICopyDescriptor {
sourcePath: string;
destinationPath: string;
hardlink: boolean;
}
export function asAbsoluteCopyOperation(
rootFolderPath: string,
copyOperation: ICopyOperation
): ICopyOperation {
const absoluteCopyOperation: ICopyOperation = asAbsoluteFileSelectionSpecifier(
rootFolderPath,
copyOperation
);
absoluteCopyOperation.destinationFolders = copyOperation.destinationFolders.map((folder) =>
path.resolve(rootFolderPath, folder)
);
return absoluteCopyOperation;
}
export function asRelativeCopyOperation(
rootFolderPath: string,
copyOperation: ICopyOperation
): ICopyOperation {
return {
...copyOperation,
destinationFolders: copyOperation.destinationFolders.map((folder) =>
makePathRelative(folder, rootFolderPath)
),
sourcePath: copyOperation.sourcePath && makePathRelative(copyOperation.sourcePath, rootFolderPath)
};
}
export async function copyFilesAsync(
copyOperations: Iterable<ICopyOperation>,
terminal: ITerminal,
buildInfoPath: string,
configHash: string,
watchFileSystemAdapter?: WatchFileSystemAdapter
): Promise<void> {
const copyDescriptorByDestination: Map<string, ICopyDescriptor> = await _getCopyDescriptorsAsync(
copyOperations,
watchFileSystemAdapter
);
await _copyFilesInnerAsync(copyDescriptorByDestination, configHash, buildInfoPath, terminal);
}
async function _getCopyDescriptorsAsync(
copyConfigurations: Iterable<ICopyOperation>,
fileSystemAdapter: WatchFileSystemAdapter | undefined
): Promise<Map<string, ICopyDescriptor>> {
// Create a map to deduplicate and prevent double-writes
// resolvedDestinationFilePath -> descriptor
const copyDescriptorByDestination: Map<string, ICopyDescriptor> = new Map();
await Async.forEachAsync(
copyConfigurations,
async (copyConfiguration: ICopyOperation) => {
// "sourcePath" is required to be a folder. To copy a single file, put the parent folder in "sourcePath"
// and the filename in "includeGlobs".
const sourceFolder: string = copyConfiguration.sourcePath!;
const sourceFiles: Map<string, fs.Dirent> = await getFileSelectionSpecifierPathsAsync({
fileGlobSpecifier: copyConfiguration,
fileSystemAdapter
});
// Dedupe and throw if a double-write is detected
for (const destinationFolderPath of copyConfiguration.destinationFolders) {
// We only need to care about the keys of the map since we know all the keys are paths to files
for (const sourceFilePath of sourceFiles.keys()) {
// Only include the relative path from the sourceFolder if flatten is false
const resolvedDestinationPath: string = path.resolve(
destinationFolderPath,
copyConfiguration.flatten
? path.basename(sourceFilePath)
: path.relative(sourceFolder, sourceFilePath)
);
// Throw if a duplicate copy target with a different source or options is specified
const existingDestinationCopyDescriptor: ICopyDescriptor | undefined =
copyDescriptorByDestination.get(resolvedDestinationPath);
if (existingDestinationCopyDescriptor) {
if (
existingDestinationCopyDescriptor.sourcePath === sourceFilePath &&
existingDestinationCopyDescriptor.hardlink === !!copyConfiguration.hardlink
) {
// Found a duplicate, avoid adding again
continue;
}
throw new Error(
`Cannot copy multiple files to the same destination "${resolvedDestinationPath}".`
);
}
// Finally, default hardlink to false, add to the result, and add to the map for deduping
const processedCopyDescriptor: ICopyDescriptor = {
sourcePath: sourceFilePath,
destinationPath: resolvedDestinationPath,
hardlink: !!copyConfiguration.hardlink
};
copyDescriptorByDestination.set(resolvedDestinationPath, processedCopyDescriptor);
}
}
},
{ concurrency: Constants.maxParallelism }
);
return copyDescriptorByDestination;
}
async function _copyFilesInnerAsync(
copyDescriptors: Map<string, ICopyDescriptor>,
configHash: string,
buildInfoPath: string,
terminal: ITerminal
): Promise<void> {
if (copyDescriptors.size === 0) {
return;
}
let oldBuildInfo: IIncrementalBuildInfo | undefined = await tryReadBuildInfoAsync(buildInfoPath);
if (oldBuildInfo && oldBuildInfo.configHash !== configHash) {
terminal.writeVerboseLine(`File copy configuration changed, discarding incremental state.`);
oldBuildInfo = undefined;
}
// Since in watch mode only changed files will get passed in, need to ensure that all files from
// the previous build are still tracked.
const inputFileVersions: Map<string, string> = new Map(oldBuildInfo?.inputFileVersions);
const buildInfo: IIncrementalBuildInfo = {
configHash,
inputFileVersions
};
const allInputFiles: Set<string> = new Set();
for (const copyDescriptor of copyDescriptors.values()) {
allInputFiles.add(copyDescriptor.sourcePath);
}
await Async.forEachAsync(
allInputFiles,
async (inputFilePath: string) => {
const fileContent: Buffer = await FileSystem.readFileToBufferAsync(inputFilePath);
const fileHash: string = createHash('sha256').update(fileContent).digest('base64');
inputFileVersions.set(inputFilePath, fileHash);
},
{
concurrency: Constants.maxParallelism
}
);
const copyDescriptorsWithWork: ICopyDescriptor[] = [];
for (const copyDescriptor of copyDescriptors.values()) {
const { sourcePath } = copyDescriptor;
const sourceFileHash: string | undefined = inputFileVersions.get(sourcePath);
if (!sourceFileHash) {
throw new Error(`Missing hash for input file: ${sourcePath}`);
}
if (oldBuildInfo?.inputFileVersions.get(sourcePath) === sourceFileHash) {
continue;
}
copyDescriptorsWithWork.push(copyDescriptor);
}
if (copyDescriptorsWithWork.length === 0) {
terminal.writeLine('All requested file copy operations are up to date. Nothing to do.');
return;
}
let copiedFileCount: number = 0;
let linkedFileCount: number = 0;
await Async.forEachAsync(
copyDescriptorsWithWork,
async (copyDescriptor: ICopyDescriptor) => {
if (copyDescriptor.hardlink) {
linkedFileCount++;
await FileSystem.createHardLinkAsync({
linkTargetPath: copyDescriptor.sourcePath,
newLinkPath: copyDescriptor.destinationPath,
alreadyExistsBehavior: AlreadyExistsBehavior.Overwrite
});
terminal.writeVerboseLine(
`Linked "${copyDescriptor.sourcePath}" to "${copyDescriptor.destinationPath}".`
);
} else {
copiedFileCount++;
await FileSystem.copyFilesAsync({
sourcePath: copyDescriptor.sourcePath,
destinationPath: copyDescriptor.destinationPath,
alreadyExistsBehavior: AlreadyExistsBehavior.Overwrite
});
terminal.writeVerboseLine(
`Copied "${copyDescriptor.sourcePath}" to "${copyDescriptor.destinationPath}".`
);
}
},
{ concurrency: Constants.maxParallelism }
);
terminal.writeLine(
`Copied ${copiedFileCount} file${copiedFileCount === 1 ? '' : 's'} and ` +
`linked ${linkedFileCount} file${linkedFileCount === 1 ? '' : 's'}`
);
await writeBuildInfoAsync(buildInfo, buildInfoPath);
}
const PLUGIN_NAME: 'copy-files-plugin' = 'copy-files-plugin';
export default class CopyFilesPlugin implements IHeftTaskPlugin<ICopyFilesPluginOptions> {
public apply(
taskSession: IHeftTaskSession,
heftConfiguration: HeftConfiguration,
pluginOptions: ICopyFilesPluginOptions
): void {
taskSession.hooks.registerFileOperations.tap(
PLUGIN_NAME,
(operations: IHeftTaskFileOperations): IHeftTaskFileOperations => {
for (const operation of pluginOptions.copyOperations) {
operations.copyOperations.add(operation);
}
return operations;
}
);
}
}