-
Notifications
You must be signed in to change notification settings - Fork 22
/
file-utility.ts
179 lines (147 loc) · 3.78 KB
/
file-utility.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import chalk from 'chalk';
import * as checksum from 'checksum';
import filenamify from 'filenamify';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as multimatch from 'multimatch';
import * as path from 'path';
import Cache from './cache';
import Config from './config';
import { OperationCounts } from './interfaces';
/**
* File system interaction and tracking.
*/
export default class FileUtility {
constructor(config: Config) {
this.config = config;
this.existingCache = new Cache(config);
this.newCache = new Cache(config);
this.load();
}
/**
* Current configuration.
*/
private config: Config;
/**
* Existing files.
*/
private existingFiles: string[];
/**
* Existing cache.
*/
private existingCache: Cache;
/**
* Cache generated during file writing.
*/
private newCache: Cache;
/**
* Counts about files written / removed.
*/
private stats: OperationCounts = {
added: 0,
removed: 0,
updated: 0
};
/**
* Write file to the file system.
*
* @param file Directory to write, relative to root.
* @param file File name to write to.
* @param content File content to write.
*/
write(dir: string | false, file: string, content: string) {
if (dir === false) {
return;
}
// remove unsafe characters
file = filenamify(file);
if (!this.shouldWrite(file)) {
return;
}
file = path.join(this.config.getRoot(), dir, file);
content = content.trim();
const cacheKey = this.normalize(file);
const cacheValue = checksum(content);
this.newCache.add(cacheKey, cacheValue);
if (!this.doesExist(file)) {
this.stats.added++;
} else if (this.existingCache.didChange(cacheKey, cacheValue)) {
this.stats.updated++;
}
fs.outputFileSync(file, content);
this.markAsWritten(file);
}
/**
* Delete all paths remaining in `existing`.
*/
finalize() {
this.existingFiles.forEach(file => {
this.stats.removed++;
fs.removeSync(file);
});
this.newCache.write();
const added = chalk.green(this.stats.added.toString());
const updated = chalk.cyan(this.stats.updated.toString());
const removed = chalk.red(this.stats.removed.toString());
return `Successfully added ${added}, updated ${updated}, and removed ${removed} files.`;
}
/**
* Check if a file passes the glob pattern.
*
* @param file File path to check.
*/
private shouldWrite(file: string) {
if (!this.config.files || !this.config.files.length) {
return true;
}
const results = multimatch([file], this.config.files);
return !!results.length;
}
/**
* Check if a file existed.
*
* @param file File path to check.
*/
private doesExist(file: string) {
if (!this.existingFiles || !this.existingFiles.length) {
return false;
}
file = this.normalize(file);
const index = this.existingFiles.indexOf(file);
return index !== -1;
}
/**
* Remove `file` from `existing`, if it exists.
*
* @param file File path to check.
*/
private markAsWritten(file: string) {
if (!file) {
return;
}
file = this.normalize(file);
const index = this.existingFiles.indexOf(file);
if (index !== -1) {
this.existingFiles.splice(index, 1);
}
}
/**
* Normalize file path for comparison.
*
* @param file File path to normalize.
*/
private normalize(file: string) {
const root = this.config.getRoot();
if (root.startsWith('./') && !file.startsWith('./')) {
file = `./${file}`;
}
return file.replace(/\\/g, '/');
}
/**
* Load existing files and cache for comparison.
*/
private load() {
this.existingFiles = glob.sync(`${this.config.getRoot()}/**/*.sql`);
this.existingCache.load();
}
}