(options: MigrateFilesOptions)
| 57 | * mutating the destination tree. |
| 58 | */ |
| 59 | export async function planMigrationFiles(options: MigrateFilesOptions): Promise<MigrationFilePlan> { |
| 60 | const sourceRoot = resolve(options.sourceRoot); |
| 61 | const outputRoot = resolve(options.outputRoot ?? sourceRoot); |
| 62 | const canonicalSourceRoot = await canonicalDirectory(sourceRoot, 'sourceRoot'); |
| 63 | const canonicalOutputRoot = await canonicalFutureDirectory(outputRoot, 'outputRoot'); |
| 64 | const outputName = options.outputName ?? localSpecOutputName; |
| 65 | const outputMode = options.mode ?? 'unselected'; |
| 66 | const sources: PlannedSource[] = []; |
| 67 | const canonicalSources = new Set<string>(); |
| 68 | |
| 69 | for (const input of options.files) { |
| 70 | const sourcePath = resolve(sourceRoot, input); |
| 71 | assertContained(sourceRoot, sourcePath, `Source path is outside sourceRoot: ${input}`); |
| 72 | |
| 73 | const canonicalSourcePath = await realpath(sourcePath); |
| 74 | assertContained(canonicalSourceRoot, canonicalSourcePath, `Source path resolves outside sourceRoot: ${input}`); |
| 75 | if (canonicalSources.has(canonicalSourcePath)) { |
| 76 | throw new Error(`Duplicate source path: ${input}`); |
| 77 | } |
| 78 | canonicalSources.add(canonicalSourcePath); |
| 79 | |
| 80 | const sourceStats = await stat(canonicalSourcePath); |
| 81 | if (!sourceStats.isFile()) throw new Error(`Source path is not a regular file: ${input}`); |
| 82 | |
| 83 | const localSourcePath = relative(sourceRoot, sourcePath).replaceAll('\\', '/'); |
| 84 | sources.push({ |
| 85 | sourcePath, |
| 86 | canonicalSourcePath, |
| 87 | localSourcePath, |
| 88 | source: await readFile(canonicalSourcePath, 'utf8'), |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | const files: MigratedFile[] = []; |
| 93 | const canonicalOutputs = new Set<string>(); |
| 94 | for (const plannedSource of sources) { |
| 95 | const outputLocalName = outputName({ sourcePath: plannedSource.sourcePath, sourceRoot, mode: outputMode }); |
| 96 | const outputPath = safeOutputPath(outputRoot, outputLocalName); |
| 97 | const canonicalOutputPath = await canonicalFuturePath(outputPath); |
| 98 | assertContained(canonicalOutputRoot, canonicalOutputPath, `Output path resolves outside outputRoot: ${outputLocalName}`); |
| 99 | |
| 100 | if (canonicalSources.has(canonicalOutputPath)) { |
| 101 | throw new Error(`Output path aliases a source file: ${outputLocalName}`); |
| 102 | } |
| 103 | if (canonicalOutputs.has(canonicalOutputPath)) { |
| 104 | throw new Error(`Duplicate output path: ${outputLocalName}`); |
| 105 | } |
| 106 | canonicalOutputs.add(canonicalOutputPath); |
| 107 | |
| 108 | const provenance: SourceProvenance = { |
| 109 | repository: options.sourceRepository, |
| 110 | sha: options.sourceSha, |
| 111 | path: plannedSource.localSourcePath, |
| 112 | }; |
| 113 | const result = migrateTestSource(plannedSource.source, { |
| 114 | ...options, |
| 115 | mode: options.mode, |
| 116 | provenance, |
no test coverage detected