Skip to content

Commit 5371f3a

Browse files
committed
Run tests
1 parent 0255819 commit 5371f3a

File tree

1 file changed

+70
-86
lines changed

1 file changed

+70
-86
lines changed

cli.js

Lines changed: 70 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const meow = require('meow');
2121
const mkdirp = require('mkdirp');
2222
const ora = require('ora');
2323
const ProgressBar = require('progress');
24-
const screenshot = require('screenshot-stream');
2524
const puppeteer = require('puppeteer');
2625
const some = require('lodash/some');
2726
const typeis = require('type-is');
@@ -37,7 +36,8 @@ readline.emitKeypressEvents(process.stdin);
3736
/* ------ */
3837

3938
// Help text
40-
const cli = meow(`
39+
const cli = meow(
40+
`
4141
Usage
4242
# Pass in any number of image files (globs allows), image urls, or website urls:
4343
$ dframe <image>
@@ -61,29 +61,29 @@ const cli = meow(`
6161
$ dframe cat.png --frame "iPhone 6" --frame "iPhone 7"
6262
$ dframe cat.png --frame "iphone 6","iphone 7"
6363
`,
64-
{
65-
flags: {
66-
help: {
67-
alias: 'h'
64+
{
65+
flags: {
66+
help: {
67+
alias: 'h',
68+
},
69+
delay: {
70+
default: 0,
71+
},
72+
devices: {
73+
default: false,
74+
},
75+
output: {
76+
type: 'string',
77+
alias: 'o',
78+
default: '.',
79+
},
80+
debug: {
81+
type: 'boolean',
82+
alias: 'd',
83+
default: false,
84+
},
6885
},
69-
delay: {
70-
default: 0
71-
},
72-
devices: {
73-
default: false
74-
},
75-
output: {
76-
type: 'string',
77-
alias: 'o',
78-
default: '.'
79-
},
80-
debug: {
81-
type: 'boolean',
82-
alias: 'd',
83-
default: false
84-
}
8586
}
86-
}
8787
);
8888

8989
const framesRepo = cli.pkg.devDependencies['deviceframe-frames'];
@@ -119,7 +119,7 @@ if (cli.flags.frame) {
119119
distance: 100,
120120
maxPatternLength: 32,
121121
minMatchCharLength: 1,
122-
keys: ['name']
122+
keys: ['name'],
123123
});
124124

125125
const results = [];
@@ -164,7 +164,7 @@ Promise.resolve()
164164

165165
/* ------------------------------------------------------------------------- */
166166

167-
function init () {
167+
function init() {
168168
debug('Init...');
169169
// Add shadow suffix on to frame name
170170
frameData.forEach(frame => {
@@ -184,7 +184,7 @@ function init () {
184184
});
185185
}
186186

187-
function confirmInputs () {
187+
function confirmInputs() {
188188
const urls = cli.input.filter(f => isUrl(f));
189189

190190
// Find image files to frame from input
@@ -196,7 +196,7 @@ function confirmInputs () {
196196
});
197197
}
198198

199-
function chooseFrames () {
199+
function chooseFrames() {
200200
if (frames) return Promise.resolve(frames);
201201

202202
debug('Choosing frames');
@@ -209,7 +209,7 @@ function chooseFrames () {
209209
let frames = [];
210210
let prompt = null;
211211

212-
function prompter () {
212+
function prompter() {
213213
prompt = inquirer.prompt({
214214
type: 'autocomplete',
215215
name: 'frames',
@@ -220,7 +220,7 @@ function chooseFrames () {
220220
return Promise.resolve(
221221
frameData.map(f => f.name.toLowerCase()).filter(name => name.indexOf(input) !== -1)
222222
);
223-
}
223+
},
224224
});
225225

226226
prompt.then(answers => {
@@ -244,7 +244,7 @@ function chooseFrames () {
244244
});
245245
}
246246

247-
function frameImages (files, urls, frames) {
247+
function frameImages(files, urls, frames) {
248248
let promises = files.map(file => {
249249
return frames.map(frame => {
250250
return frameIt(file, frame);
@@ -271,7 +271,7 @@ function frameImages (files, urls, frames) {
271271
// });
272272
// }
273273

274-
function globImageFiles (inputs) {
274+
function globImageFiles(inputs) {
275275
if (!inputs || inputs.length === 0) return Promise.resolve([]);
276276

277277
const ps = inputs.map(file => {
@@ -281,7 +281,7 @@ function globImageFiles (inputs) {
281281
return Promise.all(ps).then(results => flatten(results));
282282
}
283283

284-
function downloadFrames (frames) {
284+
function downloadFrames(frames) {
285285
debug('Downloading frames');
286286
// console.log('frames', frames);
287287
const promises = [];
@@ -303,14 +303,14 @@ function downloadFrames (frames) {
303303
return Promise.all(promises).then(results => flatten(results));
304304
}
305305

306-
function downloadFrame (frame) {
306+
function downloadFrame(frame) {
307307
debug(`Downloading frame ${frame.url}`);
308308

309309
const bar = new ProgressBar(`Downloading frame ${chalk.green(frame.name)} [:bar] :rate/bps :percent :etas`, {
310310
complete: '=',
311311
incomplete: ' ',
312312
width: 20,
313-
total: 100
313+
total: 100,
314314
});
315315

316316
bar.tick();
@@ -323,8 +323,8 @@ function downloadFrame (frame) {
323323
return new Promise((resolve, reject) => {
324324
got.stream(frame.url, {
325325
headers: {
326-
'user-agent': `deviceframe/${cli.pkg.version} (${cli.pkg.repo})`
327-
}
326+
'user-agent': `deviceframe/${cli.pkg.version} (${cli.pkg.repo})`,
327+
},
328328
})
329329
.on('end', () => {
330330
bar.tick(100);
@@ -343,7 +343,7 @@ function downloadFrame (frame) {
343343
});
344344
}
345345

346-
function frameIt (img, frameConf) {
346+
function frameIt(img, frameConf) {
347347
debug('Framing images');
348348

349349
// TODO: use filenamify here?
@@ -379,64 +379,48 @@ function frameIt (img, frameConf) {
379379
const h = Math.floor(frameConf.frame.height / 1);
380380
const dim = [w, h].join('x');
381381

382-
const spinner = ora(`Screenshoting ${imgUrl} at ${dim}`).start();
382+
const spinner = ora(`ing ${imgUrl} at ${dim}`).start();
383383

384384
// TODO: need to dynamically choose device user-agent from a list, or store them with the frames
385-
const ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1';
386-
/*
387-
const stream = screenshot(imgUrl, dim, { crop: false, userAgent: ua, delay: cli.flags.delay })
388-
.on('error', err => {
389-
spinner.fail();
390-
error(err);
391-
})
392-
.on('end', () => spinner.succeed());
393-
394-
const bufPromise = getStream.buffer(stream);
395-
*/
396-
397-
// console.log('width', w, h, frameConf);
398-
399-
let delay = 1000 || cli.flags.delay * 1000;
385+
const delay = 1000 || cli.flags.delay * 1000;
400386
const bufPromise = new Promise((resolve, reject) => {
401387
puppeteer.launch().then(browser => {
402388
browser.newPage()
403389
.then(page => {
404390
page.goto(imgUrl);
405391

406-
page.on('load', () => {
407-
// console.log('page loaded');
408-
409-
setTimeout(() => {
410-
page.setViewport({'width': w, 'height': h, 'deviceScaleFactor': pixelRatio });
411-
page.screenshot().then((buffer) => {
412-
// console.log('screenshoted');
413-
414-
spinner.succeed();
415-
browser.close();
416-
417-
resolve(buffer);
418-
}).catch((err) => {
419-
// console.log('errrou', err);
420-
421-
spinner.fail();
422-
browser.close();
423-
424-
reject(err);
425-
});
426-
}, delay);
427-
});
392+
const shot = () => {
393+
page.setViewport({
394+
width: w,
395+
height: h,
396+
deviceScaleFactor: pixelRatio,
397+
});
398+
page.screenshot().then(buffer => {
399+
spinner.succeed();
400+
browser.close();
401+
402+
resolve(buffer);
403+
}).catch(err => {
404+
spinner.fail();
405+
browser.close();
406+
407+
reject(err);
408+
});
409+
};
410+
411+
const onload = () => {
412+
setTimeout(shot, delay);
413+
};
414+
415+
page.on('load', onload);
428416
})
429-
.catch((err) => {
417+
.catch(err => {
430418
spinner.fail();
431419
reject(err);
432420
});
433421
});
434422
});
435423

436-
// bufPromise.then(buf => {
437-
// fs.writeFileSync('test.png', buf, { encoding: 'binary' });
438-
// });
439-
440424
return Promise.all([bufPromise, w, h]);
441425
})
442426
.then(([buf, w, h]) => new Promise((resolve, reject) => {
@@ -463,10 +447,10 @@ function frameIt (img, frameConf) {
463447

464448
return Promise.all([
465449
Jimp.read(framePath),
466-
Jimp.read(imgData)
450+
Jimp.read(imgData),
467451
]);
468452
})
469-
// Resize largest image to fit the other
453+
// Resize largest image to fit the other
470454
.then(([frame, jimg]) => {
471455
debug('Resizing frame/image');
472456

@@ -509,8 +493,8 @@ function frameIt (img, frameConf) {
509493

510494
jimg.cover(newFrameWidth, newFrameHeight);
511495
} else {
512-
// Source image is bigger, size it down to frame
513-
// Resize frame so shortest dimension matches
496+
// Source image is bigger, size it down to frame
497+
// Resize frame so shortest dimension matches
514498
let rH = jimg.bitmap.height;
515499
let rW = jimg.bitmap.width;
516500
if (rH > rW) {
@@ -547,12 +531,12 @@ function frameIt (img, frameConf) {
547531
// // TODO: write settings to ~/.deviceframe/settings.json
548532
// }
549533

550-
function error (msg, usage) {
534+
function error(msg, usage) {
551535
if (usage) console.log(cli.help);
552536
console.error('\n' + logSymbols.error + ' ' + chalk.red(msg));
553537
process.exit(1);
554538
}
555539

556-
function debug (...args) {
540+
function debug(...args) {
557541
if (cli.flags.debug) console.log(chalk.green(...args));
558542
}

0 commit comments

Comments
 (0)