 
 module.exports =
 
+  config:
+    executeOnSave:
+      title: 'Execute on save'
+      description: 'Execute sorting CSS property on save.'
+      type: 'boolean'
+      default: false
+
   activate: (state) ->
-    atom.commands.add 'atom-workspace', 'csscomb:execute', => @execute()
+    atom.commands.add 'atom-workspace', 'csscomb:execute', () => @execute()
+    @editorObserver = atom.workspace.observeTextEditors (editor) =>
+      editor.getBuffer().onWillSave () =>
+        if (@isExecuteOnSave())
+          @execute()
 
   deactivate: ->
+    @editorObserver.dispose()
 
-  getExecPath: ->
-    "ATOM_SHELL_INTERNAL_RUN_AS_NODE=1 '#{process.execPath}'"
-
-  getNodePath: ->
-    atom.config.get('csscomb.nodepath')
+  isExecuteOnSave: ->
+    atom.config.get('atom-csscomb.executeOnSave')
 
   execute: ->
     editor = atom.workspace.getActiveTextEditor()

        sorted = csscomb.processString(selected, {
          syntax: syntax
        })
        if isCSS
          sorted = CSSBeautify(sorted, {
            indent: '  '
          })
        editor.setTextInBufferRange(editor.getSelectedBufferRange(), sorted)
      catch e
        console.log(e)


fs          = require('fs')
path        = require('path')

CSSBeautify = require('cssbeautify')
CSSComb     = require('csscomb')

