forked from gios/gzipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompress.ts
379 lines (344 loc) · 10.7 KB
/
Compress.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import fs from 'fs';
import path from 'path';
import util from 'util';
import { v4 } from 'uuid';
import stream from 'stream';
import { Helpers } from './helpers';
import { Logger } from './logger/Logger';
import { BrotliCompression } from './compressions/Brotli';
import { GzipCompression } from './compressions/Gzip';
import {
OUTPUT_FILE_FORMAT_REGEXP,
NO_FILES_MESSAGE,
NO_PATH_MESSAGE,
DEFAULT_OUTPUT_FORMAT_MESSAGE,
INCREMENTAL_ENABLE_MESSAGE,
COMPRESSION_EXTENSIONS,
} from './constants';
import { CompressOptions, CompressedFile } from './interfaces';
import { DeflateCompression } from './compressions/Deflate';
import { Incremental } from './Incremental';
import { Config } from './Config';
import { LogLevel } from './logger/LogLevel.enum';
/**
* Compressing files.
*/
export class Compress {
private readonly nativeFs = {
lstat: util.promisify(fs.lstat),
readdir: util.promisify(fs.readdir),
exists: util.promisify(fs.exists),
unlink: util.promisify(fs.unlink),
};
private readonly nativeStream = {
pipeline: util.promisify(stream.pipeline),
};
private readonly logger: Logger;
private readonly incremental!: Incremental;
private readonly config: Config;
private readonly options: CompressOptions;
private readonly outputPath: string | undefined;
private readonly compressionInstance:
| BrotliCompression
| GzipCompression
| DeflateCompression;
private readonly target: string;
private readonly createCompression:
| ReturnType<BrotliCompression['getCompression']>
| ReturnType<GzipCompression['getCompression']>
| ReturnType<DeflateCompression['getCompression']>;
/**
* Creates an instance of Compress.
*/
constructor(
target: string,
outputPath?: string | null,
options: CompressOptions = {} as never,
) {
this.logger = new Logger(options.verbose as boolean);
this.config = new Config();
if (!target) {
const message = NO_PATH_MESSAGE;
this.logger.log(message, LogLevel.ERROR);
throw new Error(message);
}
if (outputPath) {
this.outputPath = path.resolve(process.cwd(), outputPath);
}
if (options.incremental) {
this.incremental = new Incremental(this.config);
}
this.target = path.resolve(process.cwd(), target);
this.options = options;
this.compressionInstance = this.getCompressionInstance();
this.createCompression = this.compressionInstance.getCompression();
}
/**
* Start compressing files.
*/
async run(): Promise<string[]> {
let files: string[];
let hrtime: [number, number];
try {
if (this.outputPath) {
await Helpers.createFolders(this.outputPath);
}
if (this.options.incremental) {
this.logger.log(INCREMENTAL_ENABLE_MESSAGE, LogLevel.INFO);
await this.incremental.initCacheFolder();
await this.incremental.readConfig();
}
this.compressionLog();
const hrtimeStart = process.hrtime();
files = await this.compileFolderRecursively(this.target);
hrtime = process.hrtime(hrtimeStart);
if (this.options.incremental) {
await this.incremental.updateConfig();
await this.config.writeConfig();
}
} catch (error) {
this.logger.log(error, LogLevel.ERROR);
throw new Error(error.message);
}
const filesCount = files.length;
if (filesCount) {
this.logger.log(
`${filesCount} ${
filesCount > 1 ? 'files have' : 'file has'
} been compressed. (${Helpers.readableHrtime(hrtime)})`,
LogLevel.SUCCESS,
);
} else {
this.logger.log(NO_FILES_MESSAGE, LogLevel.WARNING);
}
return files;
}
/**
* Return compression instance.
*/
private getCompressionInstance():
| BrotliCompression
| DeflateCompression
| GzipCompression {
if (this.options.brotli) {
return new BrotliCompression(this.options, this.logger);
} else if (this.options.deflate) {
return new DeflateCompression(this.options, this.logger);
} else {
return new GzipCompression(this.options, this.logger);
}
}
/**
* Compile files in folder recursively.
*/
private async compileFolderRecursively(target: string): Promise<string[]> {
try {
const compressedFiles: string[] = [];
const isFileTarget = (await this.nativeFs.lstat(target)).isFile();
let filesList: string[];
if (isFileTarget) {
const targetParsed = path.parse(target);
target = targetParsed.dir;
filesList = [targetParsed.base];
} else {
filesList = await this.nativeFs.readdir(target);
}
for (const file of filesList) {
const filePath = path.resolve(target, file);
const fileStat = await this.nativeFs.lstat(filePath);
if (fileStat.isDirectory()) {
compressedFiles.push(
...(await this.compileFolderRecursively(filePath)),
);
} else if (
fileStat.isFile() &&
this.isValidFileExtensions(path.extname(filePath).slice(1))
) {
if (fileStat.size < this.options.threshold) {
continue;
}
const hrtimeStart = process.hrtime();
const fileInfo = await this.compressFile(
file,
target,
this.outputPath,
);
if (!fileInfo.removeCompiled && !fileInfo.isSkipped) {
compressedFiles.push(filePath);
}
if (this.options.verbose) {
const hrTimeEnd = process.hrtime(hrtimeStart);
this.logger.log(
this.getCompressedFileMsg(
file,
fileInfo as CompressedFile,
hrTimeEnd,
),
);
}
}
}
return compressedFiles;
} catch (error) {
throw error;
}
}
/**
* File compression.
*/
private async compressFile(
filename: string,
target: string,
outputDir: string | undefined,
): Promise<Partial<CompressedFile>> {
let isCached = false;
let isSkipped = false;
const inputPath = path.join(target, filename);
if (outputDir) {
const isFileTarget = (await this.nativeFs.lstat(this.target)).isFile();
target = isFileTarget
? outputDir
: path.join(outputDir, path.relative(this.target, target));
await Helpers.createFolders(target);
}
const outputPath = this.getOutputPath(target, filename);
if (this.options.skipCompressed) {
if (await this.nativeFs.exists(outputPath)) {
isSkipped = true;
return { isCached, isSkipped };
}
}
if (this.options.incremental) {
const checksum = await this.incremental.getFileChecksum(inputPath);
const { isChanged, fileId } = await this.incremental.setFile(
inputPath,
checksum,
this.compressionInstance.compressionOptions,
);
const cachedFile = path.resolve(
this.incremental.cacheFolder,
fileId as string,
);
if (isChanged) {
await this.nativeStream.pipeline(
fs.createReadStream(inputPath),
this.createCompression(),
fs.createWriteStream(outputPath),
);
await this.nativeStream.pipeline(
fs.createReadStream(outputPath),
fs.createWriteStream(cachedFile),
);
} else {
await this.nativeStream.pipeline(
fs.createReadStream(cachedFile),
fs.createWriteStream(outputPath),
);
isCached = true;
}
} else {
await this.nativeStream.pipeline(
fs.createReadStream(inputPath),
this.createCompression(),
fs.createWriteStream(outputPath),
);
}
if (this.options.verbose || this.options.removeLarger) {
const beforeSize = (await this.nativeFs.lstat(inputPath)).size;
const afterSize = (await this.nativeFs.lstat(outputPath)).size;
const removeCompiled =
this.options.removeLarger && beforeSize < afterSize;
if (removeCompiled) {
await this.nativeFs.unlink(outputPath);
}
return {
beforeSize,
afterSize,
isCached,
isSkipped,
removeCompiled,
};
}
return { isCached, isSkipped };
}
/**
* Show message with compression params.
*/
private compressionLog(): void {
const options = this.compressionInstance.readableOptions();
this.logger.log(`Compression ${options}`, LogLevel.INFO);
if (!this.options.outputFileFormat) {
this.logger.log(DEFAULT_OUTPUT_FORMAT_MESSAGE, LogLevel.INFO);
}
}
/**
* Get output path which is based on [outputFileFormat].
*/
private getOutputPath(target: string, file: string): string {
const artifactsMap = new Map<string, string | null>([
['[filename]', path.parse(file).name],
['[ext]', path.extname(file).slice(1)],
['[compressExt]', this.compressionInstance.ext],
]);
let filename = `${artifactsMap.get('[filename]')}.${artifactsMap.get(
'[ext]',
)}.${artifactsMap.get('[compressExt]')}`;
if (this.options.outputFileFormat) {
artifactsMap.set('[hash]', null);
filename = this.options.outputFileFormat.replace(
OUTPUT_FILE_FORMAT_REGEXP,
(artifact) => {
if (artifactsMap.has(artifact)) {
// Need to generate hash only if we have appropriate param
if (artifact === '[hash]') {
artifactsMap.set('[hash]', v4());
}
return artifactsMap.get(artifact) as string;
} else {
return artifact;
}
},
);
}
return `${path.join(target, filename)}`;
}
/**
* Returns if the file extension is valid.
*/
private isValidFileExtensions(ext: string): boolean {
if (COMPRESSION_EXTENSIONS.includes(ext)) {
return false;
}
const excludeExtensions = this.options.exclude;
const includeExtensions = this.options.include;
if (includeExtensions?.length) {
return includeExtensions.includes(ext);
}
if (excludeExtensions?.length) {
return !excludeExtensions.includes(ext);
}
return true;
}
/**
* Returns information message about compressed file (size, time, cache, etc.)
*/
private getCompressedFileMsg(
file: string,
fileInfo: CompressedFile,
hrtime: [number, number],
): string {
if (fileInfo.isSkipped) {
return `File ${file} has been skipped`;
}
const getSize = `${Helpers.readableSize(
fileInfo.beforeSize,
)} -> ${Helpers.readableSize(fileInfo.afterSize)}`;
return fileInfo.isCached
? `File ${file} has been retrieved from the cache ${getSize} (${Helpers.readableHrtime(
hrtime,
)})`
: `File ${file} has been compressed ${getSize} (${Helpers.readableHrtime(
hrtime,
)})`;
}
}