-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (45 loc) · 1.55 KB
/
index.js
File metadata and controls
53 lines (45 loc) · 1.55 KB
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
'use strict';
const fs = require('fs');
const util = require('util');
const readFileAsync = util.promisify(fs.readFile);
const writeFileAsync = util.promisify(fs.writeFile);
const Bitmap = require('./lib/bitmap.js');
// filepath and transforms to perform
const [file, transform] = process.argv.slice(2);
performTransform(file, transform);
/**
* Performs transforms on file
*
* @param {string} file, file path for original BMP
* @param {...string} transforms transformations to perform
*/
function performTransform(file, transform) {
let bitmap = new Bitmap(file);
if (!file || !transform) {
console.error('Error, missing required arguments. Please provide a .bmp file and transform to apply.');
console.log(`Available transforms include: \n - ${Object.keys(bitmap.transforms).join('\n - ')}`);
return;
}
readFileAsync(file)
.then((buffer) => {
bitmap.parse(buffer);
if (bitmap.type !== 'BM') {
console.error('Sorry, this file format is not supported.');
return;
}
// file buffer with transform applied
let transformPerformed = bitmap.transform(transform);
if (transformPerformed) {
let updatedFilename = bitmap.filePath.replace(/\.bmp/, `.${transform}.bmp`);
return writeFileAsync(updatedFilename, bitmap.buffer)
.then(() => {
console.log(`Transformed bitmap created in '${updatedFilename}'`);
})
.catch(() => {});
}
})
.catch((err) => {
console.error('Error performing transformation. \n');
throw err;
});
}