最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - grunt-init: How can I copy or create an empty directory? - Stack Overflow

programmeradmin6浏览0评论

I want copy an empty directory from my root folder into the new initialized project. I want do this, to provide an initial directory structure for new projects.

I have such empty directories in the folder 'my-template/root/' but that will not be copied by:

    // Files to copy (and process).
    var files = init.filesToCopy(props);

    // Actually copy (and process) files.
    init.copyAndProcess(files, props);

I tried this:

    // to copy also the empty directories
    init.copy('myDir1/');
    init.copy('myDir2/');

But than grunt-init crashes with:

Error: Unable to read "grunt-init-example/root/myDir1/" file (Error code: EISDIR).

What have I to do, to get an empty directory to the destination folder?

I use [email protected] and [email protected].

I want copy an empty directory from my root folder into the new initialized project. I want do this, to provide an initial directory structure for new projects.

I have such empty directories in the folder 'my-template/root/' but that will not be copied by:

    // Files to copy (and process).
    var files = init.filesToCopy(props);

    // Actually copy (and process) files.
    init.copyAndProcess(files, props);

I tried this:

    // to copy also the empty directories
    init.copy('myDir1/');
    init.copy('myDir2/');

But than grunt-init crashes with:

Error: Unable to read "grunt-init-example/root/myDir1/" file (Error code: EISDIR).

What have I to do, to get an empty directory to the destination folder?

I use [email protected] and [email protected].

Share Improve this question edited Mar 14, 2013 at 8:24 TLindig asked Mar 13, 2013 at 17:36 TLindigTLindig 50.2k3 gold badges30 silver badges32 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Inspired by the answer of Nick Mitchinson, I use this workaround:

// module dependencies
var join = require("path").join;
// empty directories will not be copied, so we need to create them manual
grunt.file.mkdir( join(init.destpath(), 'myDir1') );
grunt.file.mkdir( join(init.destpath(), 'myDir2') );

Update:

Alternative solution is to add .gitkeep files to the empty directories. Than the directories are no more empty and will be listed with filesToCopy().

For more details about .gitkeep look here: https://stackoverflow./a/7229996/496587

Try taking a look this this article

The part relevant to creating a directory is:

var root = path.normalize(__dirname+"/relative/path/you/want");
grunt.file.mkdir(root);

however, reading the whole this would probably be good.

I create an empty __empty_file.txt file inside the empty folders which I want to get copied. Inside my template.js file I do the following:

  1. Store files which matches with our (__empty_file.txt) by iterating over files object returned by init.filesToCopy(props) to an array.
  2. Create directories using grunt.file.mkdir for each item in the above array and remove this file from files reference which was returned by init.filesToCopy. This has to be called before the call to init.copyAndProcess.
发布评论

评论列表(0)

  1. 暂无评论