NodeJS FTP漫游

时间:2018-07-10 13:02:44

标签: javascript node.js ecmascript-6 ftp generator

我已经开始使用this FTP library重写以前是Python的个人项目,但是我需要一个walk函数。我试过写一个,它应该在理论上可行,但实际上却不是。

const FTP = require('ftp');

class MyFTP extends FTP {

    * walk() {
        const self = this;

        this.pwd((err, cwd) => {
            this.list(function*(err, _list) {
                const top = cwd;
                let list = _list;

                let root = cwd;
                let dirs = list.filter(o => o.type === 'd').map(o => o.name);
                let files = list.filter(o => o.type === '-').map(o => o.name);

                yield {root, dirs, files};

                dirs.forEach(dir => {
                    root = `${top}/${dir}`;
                    this.cwd(root, function*() {
                        yield* self.walk();
                    });
                });
            });
        });
    }

}

// For good measure
Object.assign(MyFTP.prototype, FTP.prototype);

module.exports = MyFTP;

我已经将Python的os.walk的行为用作模型,但是很明显我在错误地使用了生成器函数,因为我没有在测试中得到任何输出(请参见下文)。感谢您提供任何帮助,建议或其他替代方法的建议,以使我想要的效果发挥作用。

const FTP = require('./MyFTP');
const opts = require('./server-options');

const ftp = new FTP();

ftp.on('ready', function() {
    const root = 'A VALID FTP PATH';
    ftp.cwd(root, err => {

        for (let o of ftp.walk()) {
            console.log(o);
        }

        ftp.end();
        return;
    });
});

ftp.connect(sco);

0 个答案:

没有答案