Search code examples
javascriptgulpdocumentation-generation

How to make gulp give unique destinations with gulp-documentation.js


I am trying to create a gulp task that will generate a README.md containing documentation taken from the doc strings within my javascript files.

The file structure in my project looks something like this

- Foo
  - foo1.js
  - foo2.js
- Bar
  - Qux
    - qux1.js
    - qux2.js
    - qux3.js
  - bar1.js
  - bar2.js
- Baz
  - baz1.js
  - baz2.js

I am trying to use gulp-documentation to generate an API.md for each folder for the .js files immediately within those folders. So that my expected file structure would look like

- Foo
  - API.md (contains docs for foo1 & foo2)
  - foo1.js
  - foo2.js
- Bar
  - Qux
    - API.md (contains docs for qux1, qux2 & qux3)
    - qux1.js
    - qux2.js
    - qux3.js
  - API.md (contains docs for bar1 & bar2)
  - bar1.js
  - bar2.js
- Baz
  - API.md (contains docs for baz1 & baz2)
  - baz1.js
  - baz2.js

I'm having problems however trying to get gulp to give unique destinations for each folder. The examples on gulp-documentation take all files and generate a single destination.

gulp.task('documentation-multiple-files', function () {
  return gulp.src(['Foo/*.js', 'Bar/*.js', 'Bar/Qux/*.js', 'Baz/*.js'])
    .pipe(gulpDocumentation('md'))
    .pipe(gulp.dest('md-documentation'));
});

After googling, I'm wondering if I should be using another library to help get result I'm looking for? Gulp-rename, gulp-tap, gulp-flatmap, and others seem to be possible ways of splitting up the destination? But I'm not sure...

Any help is appreciated!


Solution

  • const gulp = require("gulp");
    const glob = require("glob");
    const gulpDocumentation = require("gulp-documentation");
    
    // get only directories as an array
    const folders = glob.sync("./**/");
    
    gulp.task('documentation-multiple-files', function () {
    
      folders.forEach(function (folder) {
    
        return gulp.src(folder + "/**/*.js")
          .pipe(gulpDocumentation('md', { filename: 'API.md' }))
          .pipe(gulp.dest(folder));
      });
    });