Node.js readline仅让我回答第一组问题,然后无限期挂起。 (附有可复制的样本!)

我有这个程序:

  1. 递归地查找存储库(请参见recurse()
  2. 在每个存储库中,查找我定期更新的特定点文件(请参见update()
  3. 提示用户确认每次更新(请参见confirm()
function confirm(prompt,defaultOption = true) {
    console.warn("At confirm");
    return new Promise(function(resolve,reject) {
        console.warn("Question asked");

        readline.question(prompt,async function(answer) {
            console.warn("Question answered");

            if (/^y(es)?$/i.test(answer) || (answer === "" && defaultOption === true)) {
                resolve(true);
            } else if (/^n(o)?$/i.test(answer) || (answer === "" && defaultOption === false)) {
                resolve(false);
            } else {
                resolve(await confirm(prompt,defaultOption));
            }
        });
    });
}

async function update(directory = baseDirectory) {
    console.warn("At update");

    for (const file of [[".eslintrc.json"],["tsconfig.json"],[".vscode","extensions.json"],"settings.json"]]) {
        if (fs.existsSync(path.join(directory,file[file.length - 1]))) {
            console.warn("Before confirm");

            console.log("==== " + directory + " ====");

            if (argv["yes"] === true || (await confirm(directory + "\tOverwrite `" + file[file.length - 1] + "`? [Y/n] ")) === true) {
                fs.copyfilesync(path.join(kerplowDirectory,...file),path.join(directory,...file));
            }

            console.warn("After confirm");
        }
    }
}

(function recurse(directory = baseDirectory) {
    for (const file of fs.readdirSync(directory)) {
        if (fs.statSync(path.join(directory,file)).isDirectory()) {
            if (file === ".git") {
                console.warn("Before update");

                update(directory);

                console.warn("After update");
            }

            if (file !== "node_modules") {
                recurse(path.join(directory,file));
            }
        }
    }
})();

问题是双重的:

  1. readline反复问第一个问题
  2. 它只会询问第一组问题,然后完全停止响应输入

示例输出:

At update
Before confirm
==== .bash_logout ====
At confirm
Question asked
.bash_logout    Overwrite `.eslintrc.json`? [YAfter update
Before update
At update
Before confirm
==== .bashrc ====
At confirm
Question asked
.bash_logout    Overwrite `.eslintrc.json`? [YAfter update
Before update
At update
Before confirm
==== .cache ====
At confirm
Question asked
.bash_logout    Overwrite `.eslintrc.json`? [YAfter update
Before update
At update
Before confirm
==== .config ====
At confirm
Question asked
.bash_logout    Overwrite `.eslintrc.json`? [YAfter update
Before update
At update
Before confirm
==== .npm ====
At confirm
Question asked
.bash_logout    Overwrite `.eslintrc.json`? [YAfter update
Before update
At update
Before confirm
==== .profile ====
At confirm
Question asked
.bash_logout    Overwrite `.eslintrc.json`? [YAfter update
Before update
At update
Before confirm
==== .upm ====
At confirm
Question asked
.bash_logout    Overwrite `.eslintrc.json`? [YAfter update
Before update
At update
Before confirm
==== config.json ====
At confirm
Question asked
.bash_logout    Overwrite `.eslintrc.json`? [YAfter update
Before update
At update
Before confirm
==== index.js ====
At confirm
Question asked
.bash_logout    Overwrite `.eslintrc.json`? [YAfter update

Question answered
After confirm
Before confirm
==== .bash_logout ====
At confirm
Question asked
.bash_logout    Overwrite `tsconfig.json`? [Y/n]
Question answered
After confirm
Before confirm
==== .bash_logout ====
At confirm
Question asked
.bash_logout    Overwrite `extensions.json`? [Y/n]
Question answered
After confirm
Before confirm
==== .bash_logout ====
At confirm
Question asked
.bash_logout    Overwrite `settings.json`? [Y/n]
Question answered
After confirm


<indefinite hang>

REPL:

https://repl.it/repls/LuminousRapidNaturaldocs

oliverleaf 回答:Node.js readline仅让我回答第一组问题,然后无限期挂起。 (附有可复制的样本!)

已尝试修改文件并解决问题。我已经复制了代表的代码。

问题很少:

  • 解决后,您需要从readline.question返回。正因为如此,第一个问题才进入循环
  • 递归还需要异步,以便一次要求提示输入一个文件
const fs = require("fs");
const readline = require("readline").createInterface({
  "input": process.stdin,"output": process.stdout
});

function confirm(prompt,defaultOption = true) {
    console.warn("At confirm");

    return new Promise(function(resolve,reject) {
        console.warn("Question asked");

        readline.question(prompt,async function(answer) {
            console.warn("Question answered");

            if (/^y(es)?$/i.test(answer) || (answer === "" && defaultOption === true)) {
                resolve(true);
                return
            } else if (/^n(o)?$/i.test(answer) || (answer === "" && defaultOption === false)) {
                resolve(false);
                return
            } else {
                resolve(await confirm(prompt,defaultOption));
            }
        });
    });
}

async function update(directory) {
    console.warn("At update");

    for (const file of [[".eslintrc.json"],["tsconfig.json"],[".vscode","extensions.json"],"settings.json"]]) {
    //  if (fs.existsSync(path.join(directory,file[file.length - 1]))) {
            console.warn("Before confirm");

            console.log("==== " + directory + " ====");

            if (/* argv["yes"] === true || */ (await confirm(directory + "\tOverwrite `" + file[file.length - 1] + "`? [Y/n] ")) === true) {
    //          fs.copyFileSync(path.join(kerplowDirectory,...file),path.join(directory,...file));
                // break;
            }

            console.warn("After confirm");
    //  }
    }
    // return
}
console.log(fs.readdirSync(process.cwd()));

(async function recurse(directory) {
    for (const file of fs.readdirSync(directory)) {
    //  if (fs.statSync(path.join(directory,file)).isDirectory()) {
    //      if (file === ".git") {
                console.warn("Before update");

                await update(file);

                console.warn("After update");
    //      }

    //      if (file !== "node_modules") {
    //          recurse(path.join(directory,file));
    //      }
    //  }
    }
})(process.cwd());

我现在得到的输出是:

[ '.eslintrc.json','id.js','index.js','tsconfig.json' ]
Before update
At update
Before confirm
==== .eslintrc.json ====
At confirm
Question asked
.eslintrc.json  Overwrite `.eslintrc.json`? [Y/nY <= Screen waits here for Y/N
Question answered
After confirm
Before confirm
==== .eslintrc.json ====
At confirm
Question asked
.eslintrc.json  Overwrite `tsconfig.json`? [Y/nY <= Screen waits here for Y/N
Question answered
After confirm
Before confirm
==== .eslintrc.json ====
At confirm
Question asked
.eslintrc.json  Overwrite `extensions.json`? [Y/nY <= Screen waits here for Y/N
Question answered
After confirm
Before confirm
==== .eslintrc.json ====
At confirm
Question asked
.eslintrc.json  Overwrite `settings.json`? [Y/nY <= Screen waits here for Y/N

And it repeats for other files in the local directory

让我知道是否有帮助。如有任何疑问,请回复。

本文链接:https://www.f2er.com/2956135.html

大家都在问