使用Yeoman重命名新创建的目录(mem-fs-editor)

时间:2016-06-15 15:33:29

标签: yeoman yeoman-generator

我正在尝试重命名目标目录中的文件夹。我的templates文件夹中的目录结构如下所示:

root/
├── generators
│   └── app
│       ├── templates
│       │   └── app
│       │       └── widget
│       │           ├── widget.controller.ts
│       │           ├── widget.service.ts
│       │           └── widget.module.ts
│       └── index.js
└── .yo-rc.json

我正在尝试将widget目录(在destinationPath中)重命名为用户在prompting阶段输入的名称。以下是我尝试这个的方法:

module.exports = generators.Base.extend({
    copyAppTemplate: function () {
        this.fs.copyTpl(this.templatePath('**/*'), this.destinationPath('.'), this.props);

        this.fs.move(
            this.destinationPath('app/widget'),
            this.destinationPath('app/' + this.props.widgetName)
        );
    }
})

copyTpl的调用正确支持并将应用从templatePath模板化到destinationPath。但是,当调用fs.move操作时,我收到以下错误消息:

PS C:\Users\username\code\generator-dashboard-widget-test> yo dashboard-widget
? Your widget's name: (generator-dashboard-widget-test)
? Your widget's name: generator-dashboard-widget-test

events.js:154
    throw er; // Unhandled 'error' event
    ^
AssertionError: Trying to copy from a source that does not exist: C:\Users\username\code\generator-dashboard-widget-test\app\widget
    at EditionInterface.exports._copySingle (C:\Users\username\code\generator-dashboard-widget\node_modules\mem-fs-editor\lib\actions\copy.js:45:3)
    at EditionInterface.exports.copy (C:\Users\username\code\generator-dashboard-widget\node_modules\mem-fs-editor\lib\actions\copy.js:23:17)
    at EditionInterface.module.exports [as move] (C:\Users\username\code\generator-dashboard-widget\node_modules\mem-fs-editor\lib\actions\move.js:4:8)
    at module.exports.generators.Base.extend.copyAppTemplate (C:\Users\username\code\generator-dashboard-widget\generators\app\index.js:54:17)
    at Object.<anonymous> (C:\Users\username\code\generator-dashboard-widget\node_modules\yeoman-generator\lib\base.js:431:23)
    at C:\Users\username\code\generator-dashboard-widget\node_modules\run-async\index.js:26:25
    at C:\Users\username\code\generator-dashboard-widget\node_modules\run-async\index.js:25:19
    at C:\Users\username\code\generator-dashboard-widget\node_modules\yeoman-generator\lib\base.js:432:9
    at processImmediate [as _immediateCallback] (timers.js:383:17)

根据我对Yeoman file system documenation的理解,虚拟文件​​系统上的所有操作都是同步的,因此在app/widget实例尝试移动它之前,mem-fs-editor目录应该存在。

我应该以不同的方式重命名目录吗?

我在Windows 8.1上使用Yeoman 1.8.4,节点为5.6.0。

1 个答案:

答案 0 :(得分:0)

我没有弄清楚这个具体问题,但我能够通过使用gulp-rename插件作为转换流来完成我的目标:

copyAppTemplate: function () {
    var _this = this;

    // move a file like "app/widget/widget.controller.ts" to 
    // "app/my-widget-name/my-widget-name.controller.ts"
    this.registerTransformStream(rename(function (path) {
        path.dirname = path.dirname.replace('widget', _this.props.widgetName);
        path.basename = path.basename.replace('widget', _this.props.widgetName);
        return path;
    }));

    this.fs.copyTpl(this.templatePath('**/*'), this.destinationPath('.'), this.props);
},

我还打开了一个GitHub问题来跟进此行为:https://github.com/yeoman/yo/issues/455