diff options
| author | 2023-04-28 01:36:44 +0800 | |
|---|---|---|
| committer | 2023-04-28 01:36:44 +0800 | |
| commit | dd84b9d64fb98746a230cd24233ff50a562c39c9 (patch) | |
| tree | b583261ef00b3afe72ec4d6dacb31e57779a6faf /packages/turbo-codemod/src/runner/FileTransform.ts | |
| parent | 0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff) | |
| download | HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip | |
Diffstat (limited to 'packages/turbo-codemod/src/runner/FileTransform.ts')
| -rw-r--r-- | packages/turbo-codemod/src/runner/FileTransform.ts | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/packages/turbo-codemod/src/runner/FileTransform.ts b/packages/turbo-codemod/src/runner/FileTransform.ts new file mode 100644 index 0000000..3b23f73 --- /dev/null +++ b/packages/turbo-codemod/src/runner/FileTransform.ts @@ -0,0 +1,94 @@ +import chalk from "chalk"; +import { diffLines, Change, diffJson } from "diff"; +import fs from "fs-extra"; +import os from "os"; +import path from "path"; + +import type { FileTransformArgs, LogFileArgs } from "./types"; + +export default class FileTransform { + filePath: string; + rootPath: string; + before: string | object; + after?: string | object; + error?: Error; + changes: Array<Change> = []; + + constructor(args: FileTransformArgs) { + this.filePath = args.filePath; + this.rootPath = args.rootPath; + this.after = args.after; + this.error = args.error; + + // load original file for comparison + if (args.before === undefined) { + try { + if (path.extname(args.filePath) === ".json") { + this.before = fs.readJsonSync(args.filePath); + } else { + this.before = fs.readFileSync(args.filePath); + } + } catch (err) { + this.before = ""; + } + } else if (args.before === null) { + this.before = ""; + } else { + this.before = args.before; + } + + // determine diff + if (args.after) { + if (typeof this.before === "object" || typeof args.after === "object") { + this.changes = diffJson(this.before, args.after); + } else { + this.changes = diffLines(this.before, args.after); + } + } else { + this.changes = []; + } + } + + fileName(): string { + return path.relative(this.rootPath, this.filePath); + } + + write(): void { + if (this.after) { + if (typeof this.after === "object") { + fs.writeJsonSync(this.filePath, this.after, { spaces: 2 }); + } else { + fs.writeFileSync(this.filePath, this.after); + } + } + } + + additions(): number { + return this.changes.filter((c) => c.added).length; + } + + deletions(): number { + return this.changes.filter((c) => c.removed).length; + } + + hasChanges(): boolean { + return this.additions() > 0 || this.deletions() > 0; + } + + log(args: LogFileArgs): void { + if (args.diff) { + this.changes.forEach((part) => { + if (part.added) { + process.stdout.write(chalk.green(part.value)); + } else if (part.removed) { + process.stdout.write(chalk.red(part.value)); + } else { + process.stdout.write(chalk.dim(part.value)); + } + }); + console.log(os.EOL); + } else { + console.log(this.after); + } + } +} |
