Search code examples
yeomanyeoman-generator

Rename a newly-created directory using Yeoman (mem-fs-editor)


I'm trying to rename a folder in the destination directory. The directory structure inside my templates folder looks like this:

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

I'm trying to rename the widget directory (in destinationPath) to a name that the user enters during the prompting stage. Here's how I'm attempting this:

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)
        );
    }
})

The call to copyTpl is correctly scaffolding and templating the app from the templatePath to the destinationPath. However, when the fs.move operation is called, I get the following error message:

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)

From what I understand from the Yeoman file system documenation, all actions on the virtual file system are synchronous, so the app/widget directory should exist before the mem-fs-editor instance attempts to move it.

Is there a different way I should be renaming the directory?

I'm using Yeoman 1.8.4 on Windows 8.1 with node 5.6.0.


Solution

  • I didn't figure out this specific issue, but I was able to accomplish what I was after by using the gulp-rename plugin as a transform stream:

    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);
    },
    

    I've also opened up a GitHub issue to follow up with this behavior here: https://github.com/yeoman/yo/issues/455