( options: CopyFileOptions )
| 148 | * Copies a file from source directory to target directory with transformed relative path and content. |
| 149 | */ |
| 150 | export async function copyFileWithTransformsAsync( |
| 151 | options: CopyFileOptions |
| 152 | ): Promise<CopyFileResult> { |
| 153 | const { sourceFile, sourceDirectory, targetDirectory, transforms } = options; |
| 154 | const sourcePath = path.join(sourceDirectory, sourceFile); |
| 155 | |
| 156 | // Transform the target path according to rename rules. |
| 157 | const targetFile = transformString(sourceFile, transforms.path); |
| 158 | const targetPath = path.join(targetDirectory, targetFile); |
| 159 | |
| 160 | const filteredContentTransforms = getFilteredContentTransforms( |
| 161 | transforms.content ?? [], |
| 162 | sourceFile |
| 163 | ); |
| 164 | |
| 165 | // Transform source content. |
| 166 | const { content, transformsUsed } = await applyTransformsOnFileContentAsync( |
| 167 | sourcePath, |
| 168 | filteredContentTransforms |
| 169 | ); |
| 170 | |
| 171 | if (filteredContentTransforms.length > 0) { |
| 172 | // Save transformed source file at renamed target path. |
| 173 | await fs.outputFile(targetPath, content); |
| 174 | } else { |
| 175 | // When there are no transforms, it's safer to just copy the file. |
| 176 | // Only then binary files will be copied properly since the `content` is utf8-encoded. |
| 177 | await fs.copy(sourcePath, targetPath, { overwrite: true }); |
| 178 | } |
| 179 | |
| 180 | // Keep original file mode if needed. |
| 181 | if (options.keepFileMode) { |
| 182 | const { mode } = await fs.stat(sourcePath); |
| 183 | await fs.chmod(targetPath, mode); |
| 184 | } |
| 185 | |
| 186 | return { |
| 187 | content, |
| 188 | transformsUsed, |
| 189 | targetFile, |
| 190 | }; |
| 191 | } |
no test coverage detected
searching dependent graphs…