ionic tutorial - Checking for errors in your javascript files in before_prepare using jshint in ionicframework - ionic framework - ionic 2 - ionic creator - ionic development



#!/usr/bin/env node

var fs = require('fs');
var path = require('path');
var jshint = require('jshint').JSHINT;
var async = require('async');

var foldersToProcess = [
  'js'
];

foldersToProcess.forEach(function(folder) {
  processFiles("www/" + folder);
});

function processFiles(dir, callback) {
  var errorCount = 0;
  fs.readdir(dir, function(err, list) {
    if (err) {
      console.log('processFiles err: ' + err);
      return;
    }
    async.eachSeries(list, function(file, innercallback) {
      file = dir + '/' + file;
      fs.stat(file, function(err, stat) {
        if (!stat.isDirectory()) {
          if (path.extname(file) === ".js") {
            lintFile(file, function(hasError) {
              if (hasError) {
                errorCount++;
              }
              innercallback();
            });
          } else {
            innercallback();
          }
        } else {
          processFiles(file);
        }
      });
    }, function(error) {
      if (errorCount > 0) {
        process.exit(1);
      }
    });
  });
}

function lintFile(file, callback) {
  console.log("Linting " + file);
  fs.readFile(file, function(err, data) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }
    if (jshint(data.toString())) {
      console.log('File ' + file + ' has no errors.');
      console.log('-----------------------------------------');
      callback(false);
    } else {
      console.error('Errors in file ' + file);
      var out = jshint.data(),
          errors = out.errors;
      for (var j = 0; j < errors.length; j++) {
        console.error(errors[j].line + ':' + errors[j].character + ' -> ' + errors[j].reason + ' -> ' + errors[j].evidence);
      }
      console.error('-----------------------------------------');
      setTimeout(function() {
        callback(true);
      }, 10);
    }
  });
}
Click below button to copy the code. From wikitechy - ionic tutorial - ionic framework tutorial - team

Related Searches to Checking for errors in your javascript files in before_prepare using jshint in ionicframework