Skip to content

Commit

Permalink
delete output files only
Browse files Browse the repository at this point in the history
  • Loading branch information
Last-Order committed Feb 22, 2022
1 parent 35ad36d commit 30f47d3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/core/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class ArchiveDownloader extends Downloader {
logger.info("End of merging.");
logger.info("Starting cleaning temporary files.");
try {
await deleteDirectory(this.tempPath);
await deleteDirectory(this.tempPath, this.outputFileList);
} catch (e) {
logger.warning(
`Fail to delete temporary files, please delete manually or execute "minyami --clean" later.`
Expand Down
4 changes: 3 additions & 1 deletion src/core/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Downloader extends EventEmitter {
m3u8: Playlist;
/** 输出目录 */
outputPath: string = "./output.ts";
/** 输出文件列表 */
outputFileList: string[];
/** 并发数量 */
threads: number = 5;

Expand Down Expand Up @@ -270,7 +272,7 @@ class Downloader extends EventEmitter {
async clean() {
try {
logger.info("Starting cleaning temporary files.");
await system.deleteDirectory(this.tempPath);
await system.deleteDirectory(this.tempPath, this.outputFileList);
} catch (e) {
logger.warning(
`Fail to delete temporary files, please delete manually or execute "minyami --clean" later.`
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from "path";
import Erii from "erii";
import ArchiveDownloader from "./core/archive";
import LiveDownloader from "./core/live";
import { exec, deleteDirectory } from "./utils/system";
import { exec, forceDeleteDirectory } from "./utils/system";
import logger from "./utils/log";
import { timeStringToSeconds } from "./utils/time";
import ProxyAgent from "./utils/agent";
Expand Down Expand Up @@ -123,7 +123,7 @@ Erii.bind(
() => {
for (const file of fs.readdirSync(path.resolve(os.tmpdir()))) {
if (file.startsWith("minyami_")) {
deleteDirectory(path.resolve(os.tmpdir(), `./${file}`));
forceDeleteDirectory(path.resolve(os.tmpdir(), `./${file}`));
}
}
fs.writeFileSync(path.resolve(__dirname, "../tasks.json"), "[]");
Expand Down
28 changes: 23 additions & 5 deletions src/utils/system.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
const util = require("util");
import * as path from "path";
import * as fs from "fs";
import logger from "./log";

export const exec = util.promisify(require("child_process").exec);

export const sleep = (deley) => new Promise((resolve) => setTimeout(resolve, deley));

export const deleteDirectory = (path) => {
if (process.platform === "win32") {
return exec(`rd /s /q "${path}"`);
} else {
return exec(`rm -rf "${path}"`);
export const deleteDirectory = (directoryPath: string, fileList: string[] = []) => {
for (const filename of fileList) {
try {
fs.unlinkSync(path.resolve(directoryPath, filename));
} catch (e) {
logger.debug(e);
logger.warning(`Delete ${path.resolve(directoryPath, filename)} failed, ignored`);
}
}
if (fs.readdirSync(directoryPath).length === 0) {
fs.rmdirSync(directoryPath);
}
};

export const forceDeleteDirectory = (directoryPath: string) => {
const fileList = fs.readdirSync(directoryPath);
for (const filename of fileList) {
fs.unlinkSync(path.resolve(directoryPath, filename));
}
fs.rmdirSync(directoryPath);
};

0 comments on commit 30f47d3

Please sign in to comment.