How should I move or delete files in a Yeoman Generator?
Asked Answered
R

3

11

I'm building a generator that in part includes scaffolding from another project created with exec. Depending on user input I need to move or delete parts of this scaffolding.

Right now I'm doing it with node's fs.child_process.spawn and shelljs, but seeing as the Yo generator has mkdir, write, template, and copy, I'm wondering if there's a Yo way to move or delete files and directories.

Reflective answered 28/4, 2013 at 2:25 Comment(0)
A
5

I just use rimraf like this:

MyGenerator.prototype.removeDir = function removeDir () {
    var cb = this.async(),
        self = this;

    rimraf('path/to/dir', function () {
        self.log.info('Removing dir');
        cb();
    });
};

Remember to add rimraf as a dependency in your package.json file. Not sure if there's a built-in function for this but this one's been working fine for me so far.

Alforja answered 9/6, 2013 at 14:49 Comment(2)
That's a good way too. Upvoted you but letting this remain unanswered until there's a Yeoman api way of doing it.Reflective
There should be a built in function, I'm wondering why it's not documented though specially since RC 1.0 is out now.Alforja
S
2

Yeoman now supports this via the fs API, which is an in memory filesystem implementation.

this.fs.move('source/file', 'dest/file'); this.fs.copy('source', 'dest');

File System Docs

Situs answered 14/10, 2015 at 7:32 Comment(0)
A
2

Still not documented, but this is the delete method (works for me):

this.fs.delete('file/to/delete');

Link: Yeoman issue 1505

Apocope answered 16/11, 2015 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.