From 8b1f14e15ea54e1d566ff8afa21dd12439cda37d Mon Sep 17 00:00:00 2001 From: zonemeen Date: Wed, 21 Feb 2024 10:58:31 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=8F=90=E5=8D=87=E6=9C=AA=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=AE=8C=E5=85=A8=E6=AD=8C=E6=9B=B2=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=B8=85=E7=90=86=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/download.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/download.ts b/src/download.ts index 853bbfd1a..8b20f0746 100644 --- a/src/download.ts +++ b/src/download.ts @@ -4,7 +4,8 @@ import prettyBytes from 'pretty-bytes' import { red, green } from 'colorette' import { pipeline } from 'node:stream/promises' import { join, basename } from 'node:path' -import { existsSync, mkdirSync, createWriteStream, unlinkSync } from 'node:fs' +import { unlink } from 'node:fs/promises' +import { existsSync, mkdirSync, createWriteStream } from 'node:fs' import lyricDownload from './services/lyric' import type { SongInfo } from './types' @@ -120,14 +121,12 @@ const download = (songs: SongInfo[]) => { }) const exitEventTypes = ['exit', 'SIGINT', 'SIGHUP', 'SIGBREAK', 'SIGTERM'] - exitEventTypes.forEach((type) => { - process.on(type, () => { - for (const path of unfinishedPathMap.keys()) { - if (existsSync(path)) unlinkSync(path) - } + for (let i = 0; i < exitEventTypes.length; i++) { + process.on(exitEventTypes[i], () => { + Promise.all([...unfinishedPathMap.keys()].map((path) => unlink(path))) process.exit() }) - }) + } return Promise.all(songs.map((song, index) => downloadSong(song, index))) } export default download