<commit_before>'use babel';

import ModalPanelView from './modal-panel-view';

const path = require( 'path' );
const fs = require( 'fs' );
less = require("less");

class LessCompiler {

  constructor( state, less_input_path ) {

    this.less_file_extension = atom.config.get( 'atom-compile-less.lessFileExtension' );
    this.css_file_extension = atom.config.get( 'atom-compile-less.cssFileExtension' );
    this.sourcemap_file_extension = atom.config.get( 'atom-compile-less.sourcemapFileExtension' );
    this.display_time = atom.config.get( 'atom-compile-less.successDisplayTime' );
    this.should_compress = atom.config.get( 'atom-compile-less.shouldCompressCSS' );
    this.should_notify_success = atom.config.get( 'atom-compile-less.shouldNotifySuccess' );
    this.should_create_sourcemap = atom.config.get( 'atom-compile-less.shouldCreateSourcemap' );
    this.should_inline_sourcemap = atom.config.get( 'atom-compile-less.shouldInlineSourcemap' );


    this.less_input_path = less_input_path;
    this.directory = path.dirname( this.less_input_path );
    this.input_filename = path.basename( this.less_input_path );

    this.css_output_path = this.less_input_path.replace(this.less_file_extension, this.css_file_extension);
    this.sourcemap_output_path = this.css_output_path + this.sourcemap_file_extension;

    if ( this.less_input_path == this.css_output_path ) {
      this.valid = false;
      let error_message = this.less_input_path + " is not a .less file.";
      atom.notifications.addError( error_message, {
        dismissable: true
      } );
    }
    else {
      this.valid = true;
      this.modal_panel_view = new ModalPanelView(
                                state.ModalPanelViewState,
                                this.less_input_path,
                                this.css_output_path
                              );

      this.modal_panel = atom.workspace.addModalPanel({
        item:    this.modal_panel_view.getElement(),
        visible: false
      });
    }

  }

  destroy() {
    this.modalPanel.destroy();
    this.ModalPanelView.destroy();
  }

  compile() {
    delete this.output;
    return this.valid
         ? fs.readFile(
             this.less_input_path,
             'utf-8',
             ( error, less_content ) => this.parse( error, less_content )
           )
         : false;
  }

  parse( error, less_input ) {

    if ( error )
      atom.notifications.addError(
        error,
        {
          dismissable: true
        }
      );

    else {
      let options = {};
      if ( this.should_create_sourcemap )
        options["sourceMap"] = this.should_inline_sourcemap
                          ? { sourceMapFileInline: true }
                          : {};

      less.render( less_input, options ).then( ( output ) => {
        /*
           output.css = string of css
           output.map = string of sourcemap
           output.imports = array of string filenames of the imports referenced
        */
        this.output = output;

        fs.writeFile(
           this.css_output_path,
           output.css,
           ( error ) => this.notify( error )
         );

        if ( this.should_create_sourcemap && ! this.should_inline_sourcemap )
          fs.writeFile(
             this.sourcemap_output_path,
             output.map,
             ( error ) => this.notify( error )
           );
      } ).catch( ( error ) => {
        atom.notifications.addError(
          error.message,
          {<commit_msg>added options to notify parser of filename for proper path handling (imports were broken)<commit_after>'use babel';

import ModalPanelView from './modal-panel-view';

const path = require( 'path' );
const fs = require( 'fs' );
less = require("less");

class LessCompiler {

  constructor( state, less_input_path ) {

    this.less_file_extension = atom.config.get( 'atom-compile-less.lessFileExtension' );
    this.css_file_extension = atom.config.get( 'atom-compile-less.cssFileExtension' );
    this.sourcemap_file_extension = atom.config.get( 'atom-compile-less.sourcemapFileExtension' );
    this.display_time = atom.config.get( 'atom-compile-less.successDisplayTime' );
    this.should_compress = atom.config.get( 'atom-compile-less.shouldCompressCSS' );
    this.should_notify_success = atom.config.get( 'atom-compile-less.shouldNotifySuccess' );
    this.should_create_sourcemap = atom.config.get( 'atom-compile-less.shouldCreateSourcemap' );
    this.should_inline_sourcemap = atom.config.get( 'atom-compile-less.shouldInlineSourcemap' );


    this.less_input_path = less_input_path;
    this.directory = path.dirname( this.less_input_path );
    this.input_filename = path.basename( this.less_input_path );

    this.css_output_path = this.less_input_path.replace(this.less_file_extension, this.css_file_extension);
    this.sourcemap_output_path = this.css_output_path + this.sourcemap_file_extension;

    if ( this.less_input_path == this.css_output_path ) {
      this.valid = false;
      let error_message = this.less_input_path + " is not a .less file.";
      atom.notifications.addError( error_message, {
        dismissable: true
      } );
    }
    else {
      this.valid = true;
      this.modal_panel_view = new ModalPanelView(
                                state.ModalPanelViewState,
                                this.less_input_path,
                                this.css_output_path
                              );

      this.modal_panel = atom.workspace.addModalPanel({
        item:    this.modal_panel_view.getElement(),
        visible: false
      });
    }

  }

  destroy() {
    this.modalPanel.destroy();
    this.ModalPanelView.destroy();
  }

  compile() {
    delete this.output;
    return this.valid
         ? fs.readFile(
             this.less_input_path,
             'utf-8',
             ( error, less_content ) => this.parse( error, less_content )
           )
         : false;
  }

  parse( error, less_input ) {

    if ( error )
      atom.notifications.addError(
        error,
        {
          dismissable: true
        }
      );

    else {
      let options = {
        relativeUrls: true,
        filename:     this.less_input_path
      };
      if ( this.should_create_sourcemap )
        options["sourceMap"] = this.should_inline_sourcemap
                          ? { sourceMapFileInline: true }
                          : {};

      less.render( less_input, options ).then( ( output ) => {
        /*
           output.css = string of css
           output.map = string of sourcemap
           output.imports = array of string filenames of the imports referenced
        */
        this.output = output;

        fs.writeFile(
           this.css_output_path,
           output.css,
           ( error ) => this.notify( error )
         );

        if ( this.should_create_sourcemap && ! this.should_inline_sourcemap )
          fs.writeFile(
             this.sourcemap_output_path,
             output.map,
             ( error ) => this.notify( error )
           );
      } ).catch( ( error ) => {
        atom.notifications.addError(
          error.message,
          {
{'fraction_non_alphanumeric': 0.10707296617277048, 'fraction_numerical': 0.0002795638803466592, 'mean_word_length': 2.110869565217391, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5407694', 'n_tokens_mistral': 2102, 'n_tokens_neox': 2064, 'n_words': 355}