使用Nodejs的多个用户输入

时间:2014-04-03 21:19:51

标签: javascript node.js asynchronous

我试图编写一个连续三个问题的脚本,同时在每个问题之间等待用户输入。
似乎我很难理解如何使用节点的非阻塞特性来做到这一点 这是我正在运行的代码:

var shell = require('shelljs/global'),
    utils     = require('./utils'),
    readline  = require('readline'),
    fs        = require('fs'),
    path      = require('path');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});    

setUserDefaultSettings();

function setUserDefaultSettings() {
    var defaultSettingsFile = find('DefaultSettings.json');
    var defaultSettingsJSON = [
        {
            macro: '__new_project_path__',
            question: 'Please enter the destination path of your new project: '
        },
        {
            macro: '__as_classes_path__',
            question: 'Please enter the path to your ActionScript Classes: ',
        },
        {
            macro: '__default_browser_path__',
            question: 'Please enter the path to your default browser: '

        }
    ];
    var settingsKeys = [];
    var index        = 0;

    if (!test('-f', 'UserSettings.json')) {
        cp(defaultSettingsFile, 'UserSettings.json');
    }

    var userSettingsFile = pwd() + path.sep + find('UserSettings.json');

    fs.readFile(userSettingsFile, 'utf8', function (err, data) {
        if (err) {
            echo('Error: ' + err);
            return;
        }
        data = JSON.parse(data);

        for(var attributename in data) {
            settingsKeys.push(attributename);
        }

        defaultSettingsJSON.forEach(function(key) {
            index++;
            // Check if macros have been replaced
            if (data[settingsKeys[index - 1]] === key.macro) {
                // Replace macros with user input.
                replaceSettingMacro(userSettingsFile, key.macro, key.question);
            }
        });
    });
}

function replaceSettingMacro(jsonFile, strFind, question) {
    askUserInput(question, function(strReplace) {
        sed('-i', strFind, strReplace, jsonFile);
    });
}

function askUserInput(string, callback) {
    rl.question(string, function(answer) {
        fs.exists(answer, function(exists) {
            if (exists === false) {
                echo('File ' + answer + ' not found!');
                askUserInput(string, callback);
            } else {
                callback(answer);
            }
        });
    });
}

仅询问第一个问题,因为脚本在用户输入答案时继续执行。我明白为什么会这样,但我不知道如何解决这个问题。

3 个答案:

答案 0 :(得分:2)

这看起来像是一个实现队列的合适位置。队列将一次启动一个任务,仅在上一个完成后开始下一个任务。

你的askUserInput方法中的

尝试类似:

var questionQueue = [];
function askUserInput(string, callback) {
    if(questionQueue.length == 0) {
        rl.question(string, function(answer) {
            fs.exists(answer, function(exists) {
                if (exists === false) {
                    echo('File ' + answer + ' not found!');
                    askUserInput(string, callback);
                } else {
                    callback(answer);

                    if(questionQueue.length > 0){
                        var question questionQueue.shift();
                        askUserInput(question.string, question.callback);
                    }
                }
            });
        });
    }
    else {
        questionQueue.push({string: string, callback: callback});
    }
}

另一个选项包括将回调进一步扩展到调用堆栈,并在收到前一个回调时调用下一个问题。

答案 1 :(得分:2)

我处理这两种方式:
a)使用sync-prompt - 会很容易

如果你仍然想要异步处理它,
b)在继续之前,我会使用When等承诺库并执行When.all([array of promises])

答案 2 :(得分:0)

以下是我的问题的正确解决方案,遵循@ Preston-S建议:

var questionList = [];
var macroList    = [];

setUserDefaultSettings();

function setUserDefaultSettings() {
    var defaultSettingsFile = find('DefaultSettings.json');
    var defaultSettingsJSON = [
        {
            macro: '__new_project_path__',
            question: 'Please enter the destination path of your new project: '
        },
        {
            macro: '__as_classes_path__',
            question: 'Please enter the path to your ActionScript Classes: ',
        },
        {
            macro: '__default_browser_path__',
            question: 'Please enter the path to your default browser: '

        }
    ];

    if (!test('-f', 'UserSettings.json')) {
        cp(defaultSettingsFile, 'UserSettings.json');
    }

    userSettingsFile = pwd() + path.sep + find('UserSettings.json');

    var settingsKeys     = [];
    var index            = 0;
    var canAskQuestion   = false;

    fs.readFile(userSettingsFile, 'utf8', function (err, data) {
        if (err) {
            echo('Error: ' + err);
            return;
        }
        data = JSON.parse(data);

        for(var attributename in data) {
            settingsKeys.push(attributename);
        }

        defaultSettingsJSON.forEach(function(key) {
            index++;
            // Check if macros have been replaced
            if (data[settingsKeys[index - 1]] === key.macro) {
                // Replace macros with user input.
                questionList.push(key.question);
                macroList.push(key.macro);

                if (!canAskQuestion) {
                    askQuestion(function() {
                        copyTemplate();
                    });
                    canAskQuestion = true;
                }
            } else {
                copyTemplate();
            }
        });
    });
}

function askQuestion(callback) {
    replaceSettingMacro(userSettingsFile, macroList.shift(), questionList.shift(), function() {
        if (macroList.length < 1 || questionList.length < 1) {
            callback();
        } else {
            askQuestion(callback);
        }
    });
}

function replaceSettingMacro(jsonFile, strFind, question, callback) {
    askUserInput(question, function(strReplace) {
        sed('-i', strFind, strReplace, jsonFile);
        callback();
    });
}

function askUserInput(string, callback) {
    rl.question(string, function(answer) {
        fs.exists(answer, function(exists) {
            if (exists === false) {
                echo('File ' + answer + ' not found!');
                askUserInput(string, callback);
            } else {
                callback(answer);
            }
        });
    });
}

function copyTemplate() {
    rl.close();
}