2015-11-03 34 views
2

我建立一個自定義的自耕農發電機,所以當它的時間來創建文件他們正在創建一個目錄上面我的當前位置,或在..,因此,例如,如果我運行:如何爲自定義yeoman生成器文件設置目標路徑?

yo koala 

/home/diegoaguilar/koala的文件將在/home/diegoaguilar創建。我該如何告訴發生器應該複製文件的路徑?我真的認爲這將是process.cwd()或簡單地yeoman發電機從哪裏跑。

這是我得到的文件生成的代碼:

writing: { 
    app: function() { 
     this.fs.copyTpl(
     this.templatePath('_package.json'), 
     this.destinationPath('package.json'), 
     {appname: this.appname} 
    ); 
     this.fs.copy(
     this.templatePath('_bower.json'), 
     this.destinationPath('bower.json') 
    ); 
    }, 

    projectfiles: function() { 
     this.fs.copy(
     this.templatePath('editorconfig'), 
     this.destinationPath('.editorconfig') 
    ); 
     this.fs.copy(
     this.templatePath('jshintrc'), 
     this.destinationPath('.jshintrc') 
    ); 
    } 
    }, 

回答

3

首先,我覺得它更容易使用,而不是使用this.fs.copy()/this.fs.copyTpl()被從mem-fs-editor所包含的實例來yeomanthis.template(), ,但情況因人而異

無論如何,你需要設置this.sourceRoot('rel/path/to/source/root')this.destinationRoot('rel/path/to/dest/root')發電機嘗試寫,以確保您已設置了正確的模板和去之前stine上下文。 See yeoman's getting started guide on interacting with the files system from more informationthis.destinationRoot()應該相對於當前項目的根目錄進行定義(我在下面解釋),而this.sourceRoot()應該相對於生成器文件的根目錄進行定義。

您還必須考慮yeoman會嘗試找出您目前在命令行中的任何應用程序的根目錄。它通過向上導航(即/home/diegoaguilar/koala - >/home/diegoaguilar/)執行此操作,直至找到.yo-rc.json文件。 Yeoman隨後將最接近的.yo-rc.json的目錄作爲您的項目的預期根目錄運行該命令的位置。

在您的情況下,如果存在,您可能想要刪除/移動/重命名/home/diegoaguilar/.yo-rc.json。然後你可以創建你想讓你的項目存在的目錄並在那裏運行生成器。這看起來像

/home/diegoaguilar/ $> mkdir koala 
/home/diegoaguilar/ $> cd koala 
/home/diegoaguilar/koala/ $> yo koala 

如果你想要或需要離開/home/diegoaguilar/.yo-rc.json那裏,你應該設置this.destinationRoot()相對於/home/diegoaguilar/發電機的,這樣就寫入/home/diegoaguilar/koala/你會使用this.destinationRoot('koala')

+0

@diegoaguilar別處問我爲什麼喜歡'this.template()',所以我想我有答案這裏還有: 的'this.template()'是可以使用的主要優點它取代'this.fs.copy()'和'this.fs.copyTpl()',它默認提供當前的生成器實例(即'this')作爲模板上下文。如果你的文件是一個沒有'ejs'標籤的直接拷貝來模板化,那麼是否提供'this'並不重要; 'mem-fs-editor'會正確處理它。 –

+0

這可能是一個問題的唯一的地方是如果你想做一個'this.fs.copy()'並且傳遞一個進程選項到'mem-fs-editor',如[在另一個問題上的這個答案]中所討論的那樣(http ://stackoverflow.com/a/28975192/3991403)。在深入研究之後,我發現'copyTpl()'函數不允許'copy()'這樣的''process'函數完全是因爲'copyTpl()'本身實際上調用'copy()'本身已經在'process'選項中傳遞調用'ejs.render()'的函數。 –

相關問題