0

Within my gulpfile.js I have the following two functions that are making use of rendering gulp-nun-jucks for 2 different views...


function genNunJucks(cb) {
    return src(paths.views.src)
        .pipe(nunjucksRender({
            path: ['src/views/'], // String or Array
            ext: '.html',
            inheritExtension: false,
            envOptions: {
                watch: true
            },
            manageEnv: manageEnvironment,
            loaders: null
        }))
        .pipe(htmlbeautify({
            indentSize: 2,
            "eol": "\n",
            "indent_level": 0,
            "preserve_newlines": false
        }))
        .pipe(dest(paths.views.dest))
    cb();
}

function genNunJucks2(cb) {
    return src(paths.views.src2)
        .pipe(nunjucksRender({
            path: ['src/views/'], // String or Array
            ext: '.html',
            inheritExtension: false,
            envOptions: {
                watch: true
            },
            manageEnv: manageEnvironment,
            loaders: null
        }))
        .pipe(htmlbeautify({
            indentSize: 2,
            "eol": "\n",
            "indent_level": 0,
            "preserve_newlines": false
        }))
        .pipe(dest(paths.views.dest2))
    cb();
}

When it comes to working within the return area of gulp processes, how can I combine these two functions to be one function that does the same job? You will notice that my path.views.src(x) is the reason I am doing duplicating the process.

Perhaps there is there some kind of lopping process that I can apply that looks through a full array of paths.views.src:['','','']?

Many thanks on any tips

klewis
  • 7,459
  • 15
  • 58
  • 102
  • 2
    Create a function that accepts params for the things that change? – Dave Newton Aug 04 '23 at 21:22
  • 1
    Unrelated: this code returns before running the callback. Really don’t need the callback if that’s the case. – Dave Newton Aug 04 '23 at 21:23
  • my learning for gulp `return src()` statements is still very young. I hear you , but don't visually see how yet... – klewis Aug 04 '23 at 21:27
  • 2
    `paths.views.src2` appears to be a global for example (as does others) - pass those to the function as variables - any time you use globals like this consider that a mental flag to re-factor to use a function parameter. Example: `function genNunJucks(cb, src, dest) {` – Mark Schultheiss Aug 04 '23 at 21:29
  • I think I get it now! with all the suggestions. Simply make a reusable function that calls different parameter values, for a situation like this. – klewis Aug 04 '23 at 21:37

1 Answers1

1

You can make use of closures and a form of currying for this, and create a function that gets src as parameter and returns the genNunJucks that you want to use:

function createGenNumChunksFunction(src) {
  return function genNunJucks(cb) {
    return src(src)
      .pipe(nunjucksRender({
        path: ['src/views/'], // String or Array
        ext: '.html',
        inheritExtension: false,
        envOptions: {
          watch: true
        },
        manageEnv: manageEnvironment,
        loaders: null
      }))
      .pipe(htmlbeautify({
        indentSize: 2,
        "eol": "\n",
        "indent_level": 0,
        "preserve_newlines": false
      }))
      .pipe(dest(paths.views.dest))
    cb();
  }
}

And instead of passing genNunJucks at the place where you use it right now you can pass createGenNumChunksFunction(paths.views.src) and createGenNumChunksFunction(paths.views.src2)

Here is a simplified version that can be executed in the snipped and illustrates how it works:

function createGenNumChunksFunction(src) {
  return function genNunJucks() {
    console.log(src)
  }
}

function callPassedFunction(cb) {
  console.log('function will be called')
  cb();
}


callPassedFunction(createGenNumChunksFunction('src1'))
callPassedFunction(createGenNumChunksFunction('src2'))

An additional note - which is probably not relevant for your case - if you want to use the genNunJucks directly without the need to "create" it first, you could keep it outside of the createGenNumChunksFunction.

function genNunJucks(src) {
  console.log(src)
}

function createGenNumChunksFunction(src) {
  return function() {
    return genNunJucks(src)
  }
}

function callPassedFunction(cb) {
  console.log('function will be called')
  cb();
}


callPassedFunction(createGenNumChunksFunction('src1'))
callPassedFunction(createGenNumChunksFunction('src2'))
genNunJucks('src3')
t.niese
  • 39,256
  • 9
  • 74
  • 101