]> arthur.barton.de Git - atom-ax-pipe.git/blob - lib/pipe.coffee
cd to atom.project.path before executing commands. Resolve #1.
[atom-ax-pipe.git] / lib / pipe.coffee
1 {Range} = require 'atom'
2 {spawn} = require 'child_process'
3 CommandView = require './command-view'
4
5 history = []
6
7 module.exports =
8   activate: ->
9     atom.workspaceView.command 'pipe:run', => @run()
10
11   run: ->
12     editor = atom.workspace.getActiveEditor()
13     view = atom.workspaceView.getActiveView()
14     return if not editor?
15
16     new CommandView history, (commandString) ->
17       if not commandString
18         view.focus()
19         return
20
21       history.push commandString
22       if history.length > 300
23         history.shift()
24
25       range = editor.getSelectedBufferRange()
26       stdout = ''
27       stderr = ''
28
29       commandString = "cd #{atom.project.path} && #{commandString}"
30       proc = spawn process.env.SHELL, ["-l", "-c", commandString]
31
32       proc.stdout.on 'data', (text) ->
33         stdout += text
34
35       proc.stderr.on 'data', (text) ->
36         stderr += text
37
38       proc.on 'close', (code) ->
39         editor.setTextInBufferRange(range, stderr || stdout)
40         editor.setSelectedBufferRange(new Range(range.start, range.start))
41         view.focus()
42
43       proc.stdin.write(editor.getSelectedText())
44       proc.stdin.end()