2016-11-24 66 views
0

我有這個文件的結構:合併的文件,只有當他們進入更深兩個級別

    • Folder1中

      • Subfolder1.1
        • Subfolder1 .1.1
          • file_1.1.1.1.js
          • file_1.1.1.2.js
        • file_1.1.1.js
        • file_1.1.2.js
      • Subfolder1.2
    • 文件夾2

    • Folder3

我試圖完成以下任務,一飲而盡,將帶根目錄,在這種情況下,根文件夾,並生成以下結構:

    • Folder1中

      • Subfolder1.1
        • Subfolder1.1.1.min.js
        • file_1.1.1.min.js
        • file_1.1.2.min.js
      • Subfolder1.2
    • 文件夾2

    • 文件夾3

正如你所看到的,是直接在第二級的文件,例如Subfolder1.1,只是縮小。所有比兩層更深的文件將被連接並在包含它們的二級文件夾後命名。

這是否有可能完成吞嚥,如果是這樣,任何人都可以給我一個線索如何做到這一點?

回答

0

好吧,我能做到這一點,這裏是代碼:

gulp.task('task', 
function() { 
    // The input root dir 
    var root = 'root_in'; 

    // The output root dir 
    var rootOut = 'root_out' 

    // first get all the folders in the in the root directory 
    var folders = fs.readdirSync(root) 
     .filter(function(file) { 
      return fs.statSync(path.join(root, file)).isDirectory(); 
     }); 

    return folders.map(function(folder) { 
     // get the files inside each folder 
     var files = fs.readdirSync(path.join(root, folder)); 
     files.map(function(file) { 

      // in case it is a directory, concat all the files 
      if (fs.statSync(path.join(root, folder, file)).isDirectory()) { 
       return gulp.src(path.join(root, folder, file, '/**/*.js')) 
        .pipe(uglify()) 
        .pipe(gulp_concat(file + '.js')) 
        .pipe(gulp.dest(path.join(root, folder))) 
      } 
      // if it is a regular file, just uglify it and output 
      else { 
       return gulp.src(path.join(root, folder, file)) 
        .pipe(uglify()) 
        .pipe(gulp.dest(path.join(rootOut, folder))); 
      } 
     }); 
    }); 
});