-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
118 lines (107 loc) · 2.9 KB
/
cli.js
File metadata and controls
118 lines (107 loc) · 2.9 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
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
/* eslint-disable no-console */
import fs from 'fs';
import http from 'http';
import https from 'https';
import path from 'path';
import url from 'url';
import chalk from 'chalk';
import expandTilde from 'expand-tilde';
import minimist from 'minimist';
import nesify from '.';
const argv = minimist(process.argv.slice(2));
const {
srcUrl = '',
srcFile = '',
outSrcFile = path.join(process.cwd(), 'out', `nesify-src-img${path.extname(srcUrl || srcFile)}`),
outFile = path.join(process.cwd(), 'out', 'nesify.png'),
...restArgs
} = argv;
/**
* Reads an image URL and converts it to a Buffer
*
* @param {string} imageUrl - URL of the image to read
* @return {Promise} resolves with the Buffer
*/
const readImageToBuffer = imageUrl => new Promise((resolve, reject) => {
const options = {
...url.parse(imageUrl),
method: 'GET',
headers: {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36',
},
};
(url.parse(imageUrl).protocol === 'https:' ? https : http)
// .get(imageUrl, (response) => {
.request(options, (response) => {
if (response.statusCode !== 200) {
reject('Non-200 response code');
return;
}
const data = [];
response
.on('data', (chunk) => {
data.push(chunk);
})
.on('end', () => {
setTimeout(() => {
resolve(Buffer.concat(data));
}, 3000);
})
.on('error', (err) => {
reject(err);
});
})
.end();
});
/**
* Reads an image from disk and converts it to a buffer
*
* @param {string} filePath - path to the file
* @return {[type]} [description]
*/
const readFileToBuffer = filePath => new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
// Read the input URL and convert it inline
let srcBuffer;
if (srcUrl) {
console.log(`Reading image URL from ${chalk.magenta(srcUrl)}`);
srcBuffer = readImageToBuffer(srcUrl);
} else if (srcFile) {
console.log(`Reading image file from ${chalk.magenta(srcFile)}`);
srcBuffer = readFileToBuffer(expandTilde(srcFile));
} else {
srcBuffer = Promise.reject(`Please specify a ${chalk.cyan(srcUrl)} or ${chalk.cyan(srcFile)}`);
}
srcBuffer
.then(buffer => new Promise((resolve) => {
fs.writeFile(outSrcFile, buffer, (err) => {
if (err) {
console.error(err);
}
resolve(buffer);
});
}))
.then((buffer) => {
const canvas = nesify(buffer, {
...restArgs,
log: (...args) => {
console.log(...args);
},
});
console.log(`Writing output to ${chalk.blue(outFile)}`);
fs.writeFile(outFile, canvas.toBuffer(), (err) => {
if (err) {
console.error('err', err);
}
});
})
.catch((err) => {
console.error(err);
});