1
1
import BluebirdPromise from "bluebird-lst"
2
- import { access , createReadStream , createWriteStream , link , lstat , mkdirs , readdir , readlink , stat , Stats , symlink , unlink } from "fs-extra-p"
2
+ import { access , createReadStream , createWriteStream , link , lstat , mkdirs , readdir , readlink , stat , Stats , symlink , unlink , writeFile } from "fs-extra-p"
3
3
import isCi from "is-ci"
4
4
import * as path from "path"
5
5
import Mode from "stat-mode"
@@ -8,6 +8,7 @@ import { debug } from "./util"
8
8
export const MAX_FILE_REQUESTS = 8
9
9
export const CONCURRENCY = { concurrency : MAX_FILE_REQUESTS }
10
10
11
+ export type FileTransformer = ( path : string ) => Promise < null | string | Buffer > | null | string | Buffer
11
12
export type Filter = ( file : string , stat : Stats ) => boolean
12
13
13
14
export function unlinkIfExists ( file : string ) {
@@ -160,11 +161,23 @@ export function copyFile(src: string, dest: string, stats?: Stats | null, isUseH
160
161
}
161
162
162
163
export class FileCopier {
163
- constructor ( private isUseHardLinkFunction ?: ( file : string ) => boolean , private isUseHardLink = _isUseHardLink ) {
164
+ private isUseHardLink = _isUseHardLink
165
+
166
+ constructor ( private readonly isUseHardLinkFunction ?: ( file : string ) => boolean , private readonly transformer ?: FileTransformer ) {
164
167
}
165
168
166
169
async copy ( src : string , dest : string , stat : Stats | undefined ) {
167
170
try {
171
+ if ( this . transformer != null && stat != null && stat . isFile ( ) ) {
172
+ let data = this . transformer ( src )
173
+ if ( data != null ) {
174
+ if ( typeof ( < any > data ) . then === "function" ) {
175
+ data = await data
176
+ }
177
+ await writeFile ( dest , data )
178
+ return
179
+ }
180
+ }
168
181
await copyFile ( src , dest , stat , ( ! this . isUseHardLink || this . isUseHardLinkFunction == null ) ? this . isUseHardLink : this . isUseHardLinkFunction ( dest ) )
169
182
}
170
183
catch ( e ) {
@@ -189,13 +202,13 @@ export class FileCopier {
189
202
* Empty directories is never created.
190
203
* Hard links is used if supported and allowed.
191
204
*/
192
- export function copyDir ( src : string , destination : string , filter ?: Filter , isUseHardLink ?: ( file : string ) => boolean ) : Promise < any > {
205
+ export function copyDir ( src : string , destination : string , filter ?: Filter , transformer ?: FileTransformer , isUseHardLink ?: ( file : string ) => boolean ) : Promise < any > {
193
206
if ( debug . enabled ) {
194
207
debug ( `Copying ${ src } to ${ destination } ${ _isUseHardLink ? " using hard links" : "" } ` )
195
208
}
196
209
197
210
const createdSourceDirs = new Set < string > ( )
198
- const fileCopier = new FileCopier ( isUseHardLink )
211
+ const fileCopier = new FileCopier ( isUseHardLink , transformer )
199
212
const links : Array < Link > = [ ]
200
213
return walk ( src , filter , async ( file , stat , parent ) => {
201
214
if ( ! stat . isFile ( ) && ! stat . isSymbolicLink ( ) ) {
0 commit comments