1
1
{Task , BufferedProcess } = require ' atom'
2
2
{View } = require ' atom-space-pen-views'
3
3
GeneratorListView = require ' ./generator-list-view'
4
+ path = require ' path'
5
+ fs = require ' fs'
4
6
5
7
module .exports =
6
8
class EmberCliHelperView extends View
@@ -9,19 +11,23 @@ class EmberCliHelperView extends View
9
11
@ div class : ' ember-cli-helper tool-panel panel-bottom native-key-bindings' , =>
10
12
@ div class : ' ember-cli-btn-group' , =>
11
13
@ div class : ' block' , =>
12
- @ button outlet : ' server' , click : ' startServer' , class : ' btn btn-sm inline-block-tight' , ' Server'
13
- @ button outlet : ' test' , click : ' startTesting' , class : ' btn btn-sm inline-block-tight' , ' Test'
14
- @ button outlet : ' generate' , click : ' showGeneratorList' , class : ' btn btn-sm inline-block-tight' , ' Generate'
15
- @ button outlet : ' exit' , click : ' stopProcess' , class : ' btn btn-sm inline-block-tight' , ' Exit'
16
- @ button outlet : ' hide' , click : ' toggle' , class : ' btn btn-sm inline-block-tight btn-right' , ' Close'
17
- @ button outlet : ' mini' , click : ' minimize' , class : ' btn btn-sm inline-block-tight btn-right' , ' Minimize'
14
+ @ button outlet : ' switch' , click : ' switchFile' , class : ' btn btn-sm inline-block-tight' , ' c/t'
15
+ @ button outlet : ' route' , click : ' switchRoute' , class : ' btn btn-sm inline-block-tight' , ' r'
16
+ @ button outlet : ' server' , click : ' startServer' , class : ' btn btn-sm inline-block-tight' , ' Server'
17
+ @ button outlet : ' test' , click : ' startTesting' , class : ' btn btn-sm inline-block-tight' , ' Test'
18
+ @ button outlet : ' generate' , click : ' showGeneratorList' , class : ' btn btn-sm inline-block-tight' , ' Generate'
19
+ @ button outlet : ' exit' , click : ' stopProcess' , class : ' btn btn-sm inline-block-tight' , ' Exit'
20
+ @ button outlet : ' hide' , click : ' toggle' , class : ' btn btn-sm inline-block-tight btn-right' , ' Close'
21
+ @ button outlet : ' mini' , click : ' minimize' , class : ' btn btn-sm inline-block-tight btn-right' , ' Minimize'
18
22
@ div outlet : ' panel' , class : ' panel-body padded hidden' , =>
19
23
@ ul outlet : ' messages' , class : ' list-group'
20
24
21
25
initialize : ->
22
26
# Register Commands
23
27
atom .commands .add ' atom-text-editor' ,
24
28
" ember-cli-helper:toggle " : => @ toggle ()
29
+ " ember-cli-helper:switch-file " : => @ switchFile ()
30
+ " ember-cli-helper:switch-route " : => @ switchRoute ()
25
31
" ember-cli-helper:generate-file " : => @ showGeneratorList ()
26
32
27
33
# Add the path to the Node executable to the $PATH
@@ -66,6 +72,106 @@ class EmberCliHelperView extends View
66
72
minimize : ->
67
73
@panel .toggleClass ' hidden'
68
74
75
+ getPathComponents : ->
76
+ separator = path .sep
77
+ editor = atom .workspace .getActivePaneItem ()
78
+ file = editor ? .buffer .file
79
+ fullPath = file ? .getPath ()
80
+ fileName = file ? .getBaseName ()
81
+
82
+ # must have a file with an extension under /app/*
83
+ return [] if fileName .indexOf (' .' ) == - 1 || fullPath .split (separator).indexOf (' app' ) == - 1
84
+
85
+ extension = path .extname (fileName)
86
+
87
+ # TODO: choose only the last "app" folder
88
+ pathUntilApp = fullPath .split (separator+ ' app' + separator)[0 ] + separator + ' app' + separator
89
+ pathInApp = fullPath .split (separator+ ' app' + separator)[1 ].split (separator)
90
+ pathInApp .pop ()
91
+
92
+ [pathUntilApp, pathInApp, fileName, extension]
93
+
94
+ switchFile : ->
95
+ [pathUntilApp , paths , fileName , extension ] = @ getPathComponents ()
96
+ return unless pathUntilApp
97
+
98
+ separator = path .sep
99
+ goodPaths = []
100
+
101
+ # script to template
102
+ if extension == ' .coffee' || extension == ' .js'
103
+ newFileName = fileName .replace (/ \. (js| coffee)$ / , ' .hbs' )
104
+
105
+ # components/*.js -> templates/components/*.hbs
106
+ if paths[0 ] == ' components'
107
+ goodPaths .push [" templates" ].concat (paths).concat ([newFileName]).join (separator)
108
+
109
+ # controllers/*.js -> templates/*.hbs
110
+ # routes/*.js -> templates/*.hbs
111
+ else if paths[0 ] == ' controllers' || paths[0 ] == ' routes'
112
+ paths .shift ()
113
+ goodPaths .push [" templates" ].concat (paths).concat ([newFileName]).join (separator)
114
+
115
+ # template to script
116
+ else if extension == ' .hbs'
117
+ newFileNameJs = fileName .replace (/ \. hbs$ / , ' .js' )
118
+ newFileNameCoffee = fileName .replace (/ \. hbs$ / , ' .coffee' )
119
+
120
+ # templates/components/*.hbs -> components/*.js
121
+ if paths[0 ] == ' templates' && paths[1 ] == ' components'
122
+ paths .shift ()
123
+ goodPaths .push paths .concat ([newFileNameJs]).join (separator)
124
+ goodPaths .push paths .concat ([newFileNameCoffee]).join (separator)
125
+
126
+ # templates/xyz/*.hbz -> controllers/
127
+ else
128
+ paths .shift ()
129
+ goodPaths .push [" controllers" ].concat (paths).concat ([newFileNameJs]).join (separator)
130
+ goodPaths .push [" controllers" ].concat (paths).concat ([newFileNameCoffee]).join (separator)
131
+
132
+ @ openBestMatch (pathUntilApp, goodPaths)
133
+
134
+ switchRoute : ->
135
+ [pathUntilApp , paths , fileName , extension ] = @ getPathComponents ()
136
+ return unless pathUntilApp
137
+
138
+ separator = path .sep
139
+ goodPaths = []
140
+
141
+ newFileNameJs = fileName .replace (/ \. (js| coffee| hbs)$ / , ' .js' )
142
+ newFileNameCoffee = fileName .replace (/ \. (js| coffee| hbs)$ / , ' .coffee' )
143
+
144
+ # script to template
145
+ if extension == ' .coffee' || extension == ' .js'
146
+ # routes/*.js -> controllers/*.js
147
+ if paths[0 ] == ' routes'
148
+ paths .shift ()
149
+ goodPaths .push [" controllers" ].concat (paths).concat ([newFileNameJs]).join (separator)
150
+ goodPaths .push [" controllers" ].concat (paths).concat ([newFileNameCoffee]).join (separator)
151
+
152
+ # controllers/*.js -> routes/*.js
153
+ else if paths[0 ] == ' controllers'
154
+ paths .shift ()
155
+ goodPaths .push [" routes" ].concat (paths).concat ([newFileNameJs]).join (separator)
156
+ goodPaths .push [" routes" ].concat (paths).concat ([newFileNameCoffee]).join (separator)
157
+
158
+ # template to script
159
+ else if extension == ' .hbs'
160
+ # templates/(!components/)*.hbs -> routes/*.js
161
+ if paths[0 ] == ' templates' && paths[1 ] != ' components'
162
+ paths .shift ()
163
+ goodPaths .push [" routes" ].concat (paths).concat ([newFileNameJs]).join (separator)
164
+ goodPaths .push [" routes" ].concat (paths).concat ([newFileNameCoffee]).join (separator)
165
+
166
+ @ openBestMatch (pathUntilApp, goodPaths)
167
+
168
+ openBestMatch : (pathUntilApp , goodPaths ) ->
169
+ legitPaths = goodPaths .filter (pathToTry) =>
170
+ fs .existsSync (pathUntilApp + pathToTry)
171
+
172
+ bestPath = legitPaths[0 ] || goodPaths[0 ]
173
+ if bestPath
174
+ atom .workspace .open (pathUntilApp + bestPath)
69
175
70
176
startServer : ->
71
177
@ runCommand ' Ember CLI Server Started' .fontcolor (" green" ), ' server'
0 commit comments