The task of creating files / folders when they do not exist in the grunt

I am trying to automate my testing and wanted to automatically create test files for any javascript created in the src folder. This breaks up the structure of my folder.

js
--src
  --sum.js
  --subtract.js
  --calculator.js
  --innerFolder
    --multiply.js
    --divide.js
--specs

So, from the above folder structure, I want to create the whole folder structure and files in my specifications, as it is only if the file in the destination folder does not exist yet : -

js
--src
  --sum.js
  --subtract.js
  --calculator.js
  --innerFolder
    --multiply.js
    --divide.js
--specs
  --sumSpec.js
  --subtractSpec.js
  --calculator.js
  --innerFolder
    --multiplySpec.js
    --divideSpec.js

My approach to solving this issue is to browse all the files / folders in the src folder: -

watch:{
  src : {
    files: ['src/**/*.*'],
    tasks:  //create files and folders on specs folders here
}

I came across a grunt.file plugin, and also reviewed the grunt-shell and make my own script to achieve this.

But I was wondering if there is an easier way to do this.

+4
1

filter:

grunt.initConfig({
  copy: {
    js: {
      expand: true,
      cwd: 'js/src',
      src: '**/*.js',
      dest: 'js/specs',
      filter: function(filepath) {
        var path = require('path');
        var dest = path.join(
          grunt.config('copy.js.dest'),
          // Remove the parent 'js/src' from filepath
          filepath.split(path.sep).slice(2).join(path.sep)
        );
        return !(grunt.file.exists(dest));
      },
    },
  },
  watch: {
    js: {
      files: ['js/src/**/*.js'],
      tasks: ['copy:js'],
    },
  },
});

, grunt copy:js.

+5

All Articles