Skip to content

Commit 7a7087e

Browse files
committed
Added catch-them-all to npm scripts
This closes #4
1 parent bf50ee8 commit 7a7087e

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "1.0.0",
44
"description": "A simple example to demonstrate how to use callback in JavaScript using image element",
55
"scripts": {
6-
"start": "live-server"
6+
"start": "live-server",
7+
"catch-them-all": "node scripts/catch-them-all.js"
78
},
89
"repository": {
910
"type": "git",

scripts/catch-them-all.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const http = require('http')
22
const fs = require('fs')
3+
const path = require('path')
4+
5+
function getImagePath(pokemonNumber) {
6+
return path.join(__dirname, `../images/pokemons/${pokemonNumber}.png`)
7+
}
38

49
function catchPokemon(pokemonNumber) {
510
const options = {
@@ -10,25 +15,21 @@ function catchPokemon(pokemonNumber) {
1015

1116
http.get(options, function(response) {
1217
if (response.headers['content-type'] != 'image/png') {
13-
return undefined
18+
return
1419
}
1520

1621
let chunks = []
17-
response.on('data', (chunk) => chunks.push(chunk))
22+
response.on('data', chunk => chunks.push(chunk))
1823

1924
response.on('end', () => {
2025
const buffer = Buffer.concat(chunks)
21-
fs.writeFile(
22-
`../images/pokemons/${pokemonNumber}.png`,
23-
buffer,
24-
(error) =>{
25-
if (error) {
26-
console.log(`Missed Pokemon #${pokemonNumber}`)
27-
return
28-
}
29-
console.log(`Caught Pokemon #${pokemonNumber}`)
26+
fs.writeFile(getImagePath(pokemonNumber), buffer, error => {
27+
if (error) {
28+
console.log(`Missed Pokemon #${pokemonNumber}`)
29+
return
3030
}
31-
)
31+
console.log(`Caught Pokemon #${pokemonNumber}`)
32+
})
3233
})
3334
})
3435
}

0 commit comments

Comments
 (0)