Skip to content

Commit 5bd0f08

Browse files
committed
Add chalk library
1 parent e191132 commit 5bd0f08

File tree

8 files changed

+73
-46
lines changed

8 files changed

+73
-46
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"prettier": "prettier --write ."
1616
},
1717
"dependencies": {
18+
"chalk": "4.1.2",
1819
"jsdom": "^21.1.0",
1920
"x-crawl": "link:"
2021
},

pnpm-lock.yaml

+2-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

publish/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
}
2727
},
2828
"dependencies": {
29+
"chalk": "4.1.2",
2930
"jsdom": "^21.1.0",
3031
"@types/jsdom": "^20.0.1"
3132
}

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path'
33
import { JSDOM } from 'jsdom'
44

55
import { batchRequest, syncBatchRequest, request } from './request'
6-
import { isArray, isString, isUndefined } from './utils'
6+
import { isArray, isString, isUndefined, log, logError } from './utils'
77

88
import {
99
IXCrawlBaseConifg,
@@ -151,7 +151,7 @@ export default class XCrawl {
151151

152152
fs.createWriteStream(filePath, 'binary').write(data, (err) => {
153153
if (err) {
154-
console.log(`File save error at id ${id}: ${err.message}`)
154+
log(logError(`File save error at id ${id}: ${err.message}`))
155155
} else {
156156
const fileInfo: IFileInfo = {
157157
fileName,

src/request.ts

+42-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ import http, { Agent, RequestOptions } from 'node:http'
22
import { Agent as httpsAgent } from 'https'
33
import Url, { URL } from 'node:url'
44

5-
import { isNumber, isUndefined, random, sleep } from './utils'
5+
import {
6+
isNumber,
7+
isUndefined,
8+
log,
9+
logError,
10+
logNumber,
11+
logSuccess,
12+
random,
13+
sleep
14+
} from './utils'
615

716
import {
817
IIntervalTime,
@@ -88,13 +97,15 @@ async function useSleepByBatch(
8897
? intervalTime
8998
: random(intervalTime.max, intervalTime.min)
9099

91-
console.log(
92-
`Request ${id} needs to sleep for ${timeout} milliseconds before sending`
100+
log(
101+
`Request ${logNumber(id)} needs to sleep for ${logNumber(
102+
timeout + 'ms'
103+
)} milliseconds before sending`
93104
)
94105

95106
await sleep(timeout)
96107
} else {
97-
console.log(`Request ${id} does not need to sleep, send immediately`)
108+
log(`Request ${logNumber(id)} does not need to sleep, send immediately`)
98109
}
99110
}
100111

@@ -148,7 +159,9 @@ export async function batchRequest(
148159
const isHaveIntervalTime = !isUndefined(intervalTime)
149160
const isNumberIntervalTime = isNumber(intervalTime)
150161

151-
console.log(`Begin execution, mode: async, total: ${requestConifgs.length} `)
162+
log(
163+
`Begin execution, mode: async, total: ${logNumber(requestConifgs.length)} `
164+
)
152165

153166
const requestQueue: Promise<IRequestResItem | string>[] = []
154167

@@ -176,7 +189,7 @@ export async function batchRequest(
176189
requestQueue.push(requestItem)
177190
}
178191

179-
console.log('All requests have been sent!')
192+
log(logSuccess('All requests have been sent!'))
180193

181194
const res = await Promise.all(requestQueue)
182195

@@ -192,9 +205,13 @@ export async function batchRequest(
192205
success.push(item)
193206
})
194207

195-
error.forEach((message) => {
196-
console.log(message)
197-
})
208+
error.forEach((message) => log(logError(message)))
209+
210+
log(
211+
`total: ${logNumber(requestConifgs.length)}, success: ${logSuccess(
212+
success.length
213+
)}, error: ${logError(error.length)}`
214+
)
198215

199216
return success
200217
}
@@ -206,9 +223,13 @@ export async function syncBatchRequest(
206223
const isHaveIntervalTime = !isUndefined(intervalTime)
207224
const isNumberIntervalTime = isNumber(intervalTime)
208225

209-
console.log(`Begin execution, mode: sync, total: ${requestConifgs.length} `)
226+
log(
227+
`Begin execution, mode: sync, total: ${logNumber(requestConifgs.length)} `
228+
)
210229

211230
let id = 0
231+
let successTotal = 0
232+
let errorTotal = 0
212233
const requestRes: IRequestResItem[] = []
213234
for (const requestConifg of requestConifgs) {
214235
id++
@@ -223,13 +244,21 @@ export async function syncBatchRequest(
223244
try {
224245
const requestResItem = await request(requestConifg)
225246
requestRes.push({ id, ...requestResItem })
226-
console.log(`Request ${id} is an success`)
247+
log(logSuccess(`Request ${logNumber(id)} is an success`))
248+
successTotal++
227249
} catch (error: any) {
228-
console.log(`Request ${id} is an error: ${error.message}`)
250+
log(logError(`Request ${id} is an error: ${error.message}`))
251+
errorTotal++
229252
}
230253
}
231254

232-
console.log('All requests are over!')
255+
log(logSuccess('All requests are over!'))
256+
257+
log(
258+
`total: ${logNumber(requestConifgs.length)}, success: ${logSuccess(
259+
successTotal
260+
)}, error: ${logError(errorTotal)}`
261+
)
233262

234263
return requestRes
235264
}

src/utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import chalk from 'chalk'
2+
13
export function sleep(timeout: number) {
24
return new Promise((resolve) => setTimeout(resolve, timeout))
35
}
@@ -12,6 +14,11 @@ export function random(max: number, min = 0) {
1214
return res
1315
}
1416

17+
export const log = console.log
18+
export const logNumber = chalk.hex('#a57fff')
19+
export const logSuccess = chalk.green
20+
export const logError = chalk.red
21+
1522
export function isUndefined(value: any): value is undefined {
1623
return typeof value === 'undefined'
1724
}

test/start/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/start/index.ts

+17-24
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@ const testXCrawl = new XCrawl({
77
max: 3000,
88
min: 2000
99
},
10-
mode: 'sync'
10+
mode: 'async'
1111
})
1212

13-
// testXCrawl
14-
// .fetchData({
15-
// requestConifg: [
16-
// { url: 'http://localhost:3001/home' },
17-
// { url: 'http://localhost:9001/api/home/wonderfulplace' },
18-
// { url: 'http://localhost:9001/api/home/goodprice' },
19-
// { url: 'http://localhost:3001/home' },
20-
// { url: 'http://localhost:9001/ai/home/goodprice' }
21-
// ]
22-
// })
23-
// .then((res) => {
24-
// console.log(res)
25-
// })
13+
testXCrawl
14+
.fetchData({
15+
requestConifg: [
16+
{ url: 'http://localhost:3001/home' },
17+
{ url: 'http://localhost:9001/api/home/wonderfulplace' },
18+
{ url: 'http://localhost:9001/api/home/goodprice' },
19+
{ url: 'http://localhost:3001/home' },
20+
{ url: 'http://localhost:9001/ai/home/goodprice' }
21+
]
22+
})
23+
.then((res) => {
24+
// console.log(res)
25+
})
26+
27+
// testXCrawl.fetchHTML({ url: 'https://www.bilibili.com/' }).then((res) => {
28+
// const { jsdom } = res.data
2629

27-
// testXCrawl.fetchHTML({ url: 'https://www.bilibili.com/' }).then((jsdom) => {
2830
// const document = jsdom.window.document
2931
// const imgBoxEl = document.querySelectorAll('.bili-video-card__cover')
3032

@@ -52,12 +54,3 @@ const testXCrawl = new XCrawl({
5254
// console.log(res)
5355
// })
5456
// })
55-
56-
testXCrawl.fetchHTML('https://cn.bing.com').then((res) => {
57-
const { jsdom } = res.data
58-
})
59-
60-
testXCrawl.fetchHTML('https://docs.github.com/zh/get-started').then((res) => {
61-
const { jsdom } = res.data
62-
console.log(jsdom.window.document.querySelector('title')?.textContent)
63-
})

0 commit comments

Comments
 (0)