1
1
'use babel' ;
2
2
3
+ import fs from 'fs' ;
4
+ import path from 'path' ;
5
+
3
6
// Public: GrammarUtils.Nim - a module which selects the right file to run for Nim projects
4
7
export default {
5
8
// Public: Find the right file to run
@@ -9,43 +12,29 @@ export default {
9
12
// Returns the {String} filepath of file to run
10
13
11
14
projectDir ( editorfile ) {
12
- const path = require ( 'path' ) ;
13
15
return path . dirname ( editorfile ) ;
14
16
} ,
15
17
16
18
findNimProjectFile ( editorfile ) {
17
- const path = require ( 'path' ) ;
18
- const fs = require ( 'fs' ) ;
19
-
20
19
if ( path . extname ( editorfile ) === '.nims' ) {
21
20
// if we have an .nims file
22
- try {
23
- var tfile = editorfile . slice ( 0 , - 1 ) ;
24
- var stats = fs . statSync ( tfile ) ;
21
+ const tfile = editorfile . slice ( 0 , - 1 ) ;
22
+
23
+ if ( fs . existsSync ( tfile ) ) {
25
24
// it has a corresponding .nim file. so thats a config file.
26
25
// we run the .nim file instead.
27
26
return path . basename ( tfile ) ;
28
- } catch ( error ) {
29
- // it has no corresponding .nim file, it is a standalone script
30
- return path . basename ( editorfile ) ;
31
27
}
28
+ // it has no corresponding .nim file, it is a standalone script
29
+ return path . basename ( editorfile ) ;
32
30
}
33
31
34
32
// check if we are running on a file with config
35
- try {
36
- var stats = fs . statSync ( `${ editorfile } s` ) ;
33
+ if ( fs . existsSync ( `${ editorfile } s` ) ||
34
+ fs . existsSync ( `${ editorfile } .cfg` ) ||
35
+ fs . existsSync ( `${ editorfile } cfg` ) ) {
37
36
return path . basename ( editorfile ) ;
38
- } catch ( error ) { }
39
-
40
- try {
41
- var stats = fs . statSync ( `${ editorfile } .cfg` ) ;
42
- return path . basename ( editorfile ) ;
43
- } catch ( error1 ) { }
44
-
45
- try {
46
- var stats = fs . statSync ( `${ editorfile } cfg` ) ;
47
- return path . basename ( editorfile ) ;
48
- } catch ( error2 ) { }
37
+ }
49
38
50
39
// assume we want to run a project
51
40
// searching for the first file which has
@@ -59,15 +48,10 @@ export default {
59
48
const name = `${ filepath } /${ file } ` ;
60
49
if ( fs . statSync ( name ) . isFile ( ) ) {
61
50
if ( path . extname ( name ) === '.nims' ||
62
- path . extname ( name ) === '.nimcgf' ||
63
- path . extname ( name ) === '.cfg' ) {
64
- try {
65
- var tfile = name . slice ( 0 , - 1 ) ;
66
- var stats = fs . statSync ( tfile ) ;
67
- return path . basename ( tfile ) ;
68
- } catch ( error ) {
69
- console . log ( 'File does not exist.' ) ;
70
- }
51
+ path . extname ( name ) === '.nimcgf' ||
52
+ path . extname ( name ) === '.cfg' ) {
53
+ const tfile = name . slice ( 0 , - 1 ) ;
54
+ if ( fs . existsSync ( tfile ) ) return path . basename ( tfile ) ;
71
55
}
72
56
}
73
57
}
0 commit comments