Code Monkey home page Code Monkey logo

Comments (4)

JamieMason avatar JamieMason commented on September 28, 2024

I ran into the same issue and — given the age of this issue — modified the task as follows to support all plugins;

I hope this is of use to someone.

/*
 * grunt-esformatter
 *
 * Copyright (c) 2013 Jörn Zaefferer, contributors
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

  grunt.registerMultiTask('esformatter', 'Format JS files', gruntEsformatter);

  function gruntEsformatter() {

    var esformatter = require('esformatter');
    var options = this.options();

    (options.plugins || []).forEach(registerPlugin);

    this.files.forEach(formatFileList);

    function formatFileList(file) {
      file.src
        .filter(assertFile)
        .forEach(formatFile);
    }

    function registerPlugin(name) {
      esformatter.register(require(name));
    }

    function assertFile(filePath) {
      if (grunt.file.exists(filePath)) {
        return true;
      }
      grunt.log.warn('Source file "' + filePath + '" not found.');
      return false;
    }

    function formatFile(file) {

      var formatted;
      var firstLine;
      var content = grunt.file.read(file);

      if (options.skipHashbang) {
        firstLine = content.match(/^#!.+\n/);
        content = content.replace(firstLine, "");
      }

      try {
        formatted = esformatter.format(content, options);
      } catch (e) {
        grunt.log.error('Exception while formatting ' + file);
        grunt.log.error(e.stack);
        return;
      }

      if (options.skipHashbang && firstLine) {
        formatted = firstLine + formatted;
      }

      grunt.file.write(file, formatted);
      grunt.log.writeln('File ' + file + ' formatted.');

    }

  }

};

/cc @jzaefferer @sedge

from grunt-esformatter.

jzaefferer avatar jzaefferer commented on September 28, 2024

A pull request would help.

from grunt-esformatter.

JamieMason avatar JamieMason commented on September 28, 2024

Of course, this is ordinarily what I'd do but I held back after seeing this comment from @sedge, that there is work from him to come in this area.

Thanks all.

from grunt-esformatter.

jzaefferer avatar jzaefferer commented on September 28, 2024

Good point, I forgot about that PR. Since its been several months, I think you could take this over. Also supporting esformatter plugins in general makes much more sense than supporting one specific plugin.

from grunt-esformatter.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.