11import 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"
33import isCi from "is-ci"
44import * as path from "path"
55import Mode from "stat-mode"
@@ -8,6 +8,7 @@ import { debug } from "./util"
88export const MAX_FILE_REQUESTS = 8
99export const CONCURRENCY = { concurrency : MAX_FILE_REQUESTS }
1010
11+ export type FileTransformer = ( path : string ) => Promise < null | string | Buffer > | null | string | Buffer
1112export type Filter = ( file : string , stat : Stats ) => boolean
1213
1314export function unlinkIfExists ( file : string ) {
@@ -160,11 +161,23 @@ export function copyFile(src: string, dest: string, stats?: Stats | null, isUseH
160161}
161162
162163export 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 ) {
164167 }
165168
166169 async copy ( src : string , dest : string , stat : Stats | undefined ) {
167170 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+ }
168181 await copyFile ( src , dest , stat , ( ! this . isUseHardLink || this . isUseHardLinkFunction == null ) ? this . isUseHardLink : this . isUseHardLinkFunction ( dest ) )
169182 }
170183 catch ( e ) {
@@ -189,13 +202,13 @@ export class FileCopier {
189202 * Empty directories is never created.
190203 * Hard links is used if supported and allowed.
191204 */
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 > {
193206 if ( debug . enabled ) {
194207 debug ( `Copying ${ src } to ${ destination } ${ _isUseHardLink ? " using hard links" : "" } ` )
195208 }
196209
197210 const createdSourceDirs = new Set < string > ( )
198- const fileCopier = new FileCopier ( isUseHardLink )
211+ const fileCopier = new FileCopier ( isUseHardLink , transformer )
199212 const links : Array < Link > = [ ]
200213 return walk ( src , filter , async ( file , stat , parent ) => {
201214 if ( ! stat . isFile ( ) && ! stat . isSymbolicLink ( ) ) {
0 commit comments