MCPcopy Create free account
hub / github.com/343dev/optimizt / processFile

Function processFile

optimize.js:74–131  ·  view source on GitHub ↗
({
	filePath,
	config,
	progressBarContainer,
	progressBar,
	totalSize,
	isLossless,
})

Source from the content-addressed store, hash-verified

72}
73
74async function processFile({
75 filePath,
76 config,
77 progressBarContainer,
78 progressBar,
79 totalSize,
80 isLossless,
81}) {
82 try {
83 const fileBuffer = await fs.promises.readFile(filePath.input);
84 const processedFileBuffer = await processFileByFormat({ fileBuffer, config, isLossless });
85
86 const fileSize = fileBuffer.length;
87 const processedFileSize = processedFileBuffer.length;
88
89 totalSize.before += fileSize;
90 totalSize.after += Math.min(fileSize, processedFileSize);
91
92 const ratio = calculateRatio(fileSize, processedFileSize);
93
94 const isOptimized = ratio > 0;
95 const isChanged = !fileBuffer.equals(processedFileBuffer);
96 const isSvg = path.extname(filePath.input).toLowerCase() === '.svg';
97
98 if (!isOptimized && (!isChanged || !isSvg)) {
99 logProgressVerbose(getRelativePath(filePath.output), {
100 description: `${(isChanged ? 'File size increased' : 'Nothing changed')}. Skipped`,
101 progressBarContainer,
102 });
103
104 return;
105 }
106
107 await fs.promises.mkdir(path.dirname(filePath.output), { recursive: true });
108 await fs.promises.writeFile(filePath.output, processedFileBuffer);
109
110 const before = formatBytes(fileSize);
111 const after = formatBytes(processedFileSize);
112
113 logProgress(getRelativePath(filePath.output), {
114 type: isOptimized ? LOG_TYPES.SUCCESS : LOG_TYPES.WARNING,
115 description: `${before} → ${after}. Ratio: ${ratio}%`,
116 progressBarContainer,
117 });
118 } catch (error) {
119 if (error.message) {
120 logProgress(getRelativePath(filePath.output), {
121 type: LOG_TYPES.ERROR,
122 description: (error.message || '').trim(),
123 progressBarContainer,
124 });
125 } else {
126 progressBarContainer.log(error);
127 }
128 } finally {
129 progressBar.increment();
130 }
131}

Callers 1

optimizeFunction · 0.70

Calls 6

calculateRatioFunction · 0.90
logProgressVerboseFunction · 0.90
getRelativePathFunction · 0.90
formatBytesFunction · 0.90
logProgressFunction · 0.90
processFileByFormatFunction · 0.85

Tested by

no test coverage detected