UNPKG

2.5 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var fs_1 = require("fs");
4var utils_1 = require("./utils");
5var git_utils_1 = require("./git-utils");
6var ModifiedFile = (function () {
7 function ModifiedFile(_a) {
8 var fullPath = _a.fullPath, gitDirectoryParent = _a.gitDirectoryParent, base = _a.base, head = _a.head, selectedFormatter = _a.selectedFormatter;
9 this.modifiedCharacterRanges = [];
10 this.fullPath = fullPath;
11 this.gitDirectoryParent = gitDirectoryParent;
12 this.base = base;
13 this.head = head;
14 this.selectedFormatter = selectedFormatter;
15 this.resolveFileContents();
16 this.resolveFormatterConfig();
17 }
18 ModifiedFile.prototype.isAlreadyFormatted = function () {
19 return this.selectedFormatter.isAlreadyFormatted(this.fileContents, this.formatterConfig);
20 };
21 ModifiedFile.prototype.hasValidFormattingForCharacterRanges = function () {
22 return this.selectedFormatter.checkFormattingOfRanges(this.fileContents, this.formatterConfig, this.modifiedCharacterRanges);
23 };
24 ModifiedFile.prototype.formatCharacterRangesWithinContents = function () {
25 this.formattedFileContents = this.selectedFormatter.formatRanges(this.fileContents, this.formatterConfig, this.modifiedCharacterRanges);
26 };
27 ModifiedFile.prototype.shouldContentsBeUpdatedOnDisk = function () {
28 return this.fileContents !== this.formattedFileContents;
29 };
30 ModifiedFile.prototype.updateFileOnDisk = function () {
31 fs_1.writeFileSync(this.fullPath, this.formattedFileContents);
32 };
33 ModifiedFile.prototype.calculateModifiedCharacterRanges = function () {
34 try {
35 var diff = git_utils_1.getDiffForFile(this.gitDirectoryParent, this.fullPath, this.base, this.head);
36 var lineChangeData = utils_1.extractLineChangeData(diff);
37 this.modifiedCharacterRanges = utils_1.calculateCharacterRangesFromLineChanges(lineChangeData, this.fileContents);
38 return { err: null };
39 }
40 catch (err) {
41 return { err: err };
42 }
43 };
44 ModifiedFile.prototype.resolveFileContents = function () {
45 this.fileContents = fs_1.readFileSync(this.fullPath, 'utf8');
46 };
47 ModifiedFile.prototype.resolveFormatterConfig = function () {
48 this.formatterConfig = this.selectedFormatter.resolveConfig(this.fullPath);
49 };
50 return ModifiedFile;
51}());
52exports.ModifiedFile = ModifiedFile;