2016-10-12 9 views
0
私は再帰的にJavaScriptファイル連結ディレクトリ内に含まれるが、現在のディレクトリ内のファイルは、他の含まれているディレクトリに、元掘り下げる前に追加することを確保する必要があり

兵卒、連結ディレクトリ再帰的なファイル最初

src/ 
- module1/ 
    - config.js 
    - index.js 
- module2/ 
    - index.js 
- main.js 

使い方をhereで提示ソリューションは、私は再帰的にこれを使用してファイルを取得することができた:

// get all module directories 
    grunt.file.expand("src/*").forEach(function (dir) { 
     // get the module name from the directory name 
     var dirName = dir.substr(dir.lastIndexOf('/') + 1); 

     // create a subtask for each module, find all src files 
     // and combine into a single js file per module 
     concat.main.src.push(dir + '/**/*.js'); 
    }); 

    console.log(concat.main.src); 

が、私はconcat.main.srcにチェックインする際、注文は私が必要な方法ではありません。

//console.log output 
['src/module1/**/*.js', 
'src/module2/**/*.js', 
'src/main.js'] 

私は必要な順序は次のとおりです。

['src/main.js', 
'src/module1/**/*.js', 
'src/module2/**/*.js'] 

どのように私はこれを達成することができます上の任意のアイデア?

答えて

0

おかげで多くのことを自分のモジュールを追加していきます!

私はグランツのドキュメンテーションでシャワーを浴びてより深く見直しをしましたが、興味を持っている可能性のある他の人にここで解決策を見いだしました。 grunt.file.expandの代わりにgrunt.file.recurseを使用しています。

var sourceSet = []; 
grunt.file.recurse("src/main/", function callback(abspath, rootdir, subdir, filename) { 
    sourceSet.push(abspath); 
}); 

concat.main.src = concat.main.src.concat(sourceSet.sort()); 

今すぐ完璧に動作します。

1

すぐに解決できるのは、main.jsの名前をアルファベット順に処理するapp.jsに変更することです。

main.jsを保存したい場合は、最初に配列にプッシュしてみてください。あなたは、srcのサブディレクトリのみを見て展開するファイルのグロブを変更した場合 はその後、それはmain.jsをスキップして、アレイに

concat.main.src.push('src/main.js'); 
// get all module directories 
grunt.file.expand("src/**").forEach(function (dir) { 
    // get the module name from the directory name 
    var dirName = dir.substr(dir.lastIndexOf('/') + 1); 

    // create a subtask for each module, find all src files 
    // and combine into a single js file per module 
    concat.main.src.push(dir + '/**/*.js'); 
}); 

console.log(concat.main.src); 
+0

main.jsは単なる例です。真実はn個のファイルが存在するルートディレクトリにあり、同じ動作をn個のレベルでも保証するソリューションを得たいと考えています。上記は私が必要とするものを説明するためのものですが、これが適切な解決策ではないと私は考えています。とにかくおかげさまで! – YadirHB

関連する問題