2013-03-13 61 views
3

我想從根文件夾複製一個空目錄到新的初始化項目中。我想這樣做,爲新項目提供初始目錄結構。grunt-init:如何複製或創建一個空目錄?

我的文件夾中,例如空目錄「我的模板/根/」,但不會被複制:

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

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

我嘗試這樣做:

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

但比grunt- init崩潰:

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

我該怎麼做,以獲得一個空目錄到目標文件夾?

我使用[email protected][email protected]

回答

3

尼克Mitchinson的回答啓發,我用這個解決方法:

// 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')); 

更新:

替代解決方案是.gitkeep文件添加到空目錄。比目錄不再更多,並將與filesToCopy()列出。

有關.gitkeep更多詳情請看:https://stackoverflow.com/a/7229996/496587

2

嘗試採取一看這個this article

的部分有關創建一個目錄爲:

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

然而,在閱讀整個這很可能是一件好事。

+1

Your given link is dead :( – Aajahid 2014-08-31 12:58:09

0

我創造我想要得到複製的空文件夾內的空__empty_file.txt文件。 裏面我template.js文件我做到以下幾點:這與我們(__empty_file.txt)通過遍歷文件對象由init.filesToCopy(道具)返回匹配

  1. 存儲文件到陣列。
  2. 使用grunt.file.mkdir用於上述陣列中的每個項目創建目錄,並移除其通過init.filesToCopy返回的文件的參考文件。這必須在調用init.copyAndProcess之前調用。
相關問題