NodeJS 脚本在控制台中显示每行更新

6by*_*tes 6 command-line-interface node.js

我有一个用 NodeJS 编写的小命令行程序来处理给定目录中的所有文本文件。由于 Node 是异步的,脚本将读取所有文件,处理它们并输出如下内容

Converting file: example-file-1.txt
Converting file: example-file-2.txt
Converting file: example-file-3.txt
Converting file: example-file-4.txt
File example-file-3.txt converted!
File example-file-1.txt converted!
File example-file-2.txt converted!
File example-file-4.txt converted!
Run Code Online (Sandbox Code Playgroud)

这不是很漂亮,“转换后的”消息不按顺序排列,因为文件大小不同并且以不同的速度完成处理。
我想看到的是这样的

File example-file-1.txt: DONE! // processing of this file has finished
File example-file-2.txt: converting... // this file is still processing
File example-file-3.txt: DONE!
File example-file-4.txt: converting...
Run Code Online (Sandbox Code Playgroud)

每行最右边的部分应该随着进度动态更新。

我在这里真正要问的是如何在控制台中显示几行消息,我可以随着脚本的进展更新这些消息?

6by*_*tes 0

我最终使用回调中的简单递归函数以同步方式处理所有文件。使用Ora库在终端窗口中显示每个文件的进度。

var files = [];

// get all "txt" files from a given directory and store them in an array "files"
function getFiles(dir) {
    var filesRaw = fs.readdirSync(dir);
    for (var i in filesRaw) {
        var name = dir === '.' ? filesRaw[i] : dir + '/' + filesRaw[i];
        var ext = path.extname(name);
        if (!fs.statSync(name).isDirectory()) {
            if (ext == '.txt') {
                files.push(path.resolve(process.cwd(), name));
            }
        }
    }
    // only after the directory is read, run the function that starts processing those files
    recode(files);
}

function recode() {
    if (files.length > 0) {
        var fileName = files.shift();
        // this is the terminal spinner library
        const spinner = ora(`File ${path.basename(fileName)} processing...`).start();
        var fileSingle = fs.readFileSync(fileName);
        var data = new Buffer(fileSingle, "ascii");
        var translated = encoding.convert(data, "UTF-8", "CP1250");
        var converted = iconvlite.encode(translated, "utf8").toString();

        fs.writeFile(fileName, converted, function(err) {
            if (err) {
                spinner.fail(`File ${path.basename(fileName)} failed.`);
                console.log(err);
            } else {
                spinner.succeed(`File ${path.basename(fileName)} DONE!`);
            }
            // process another file from the files array only when the previous file is finished and saved to disk
            recode(files);
        });
    } else {
        console.log('No more files to process or all files done.');
    }
}
Run Code Online (Sandbox Code Playgroud)