forked from atom-community/atom-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnim.js
62 lines (54 loc) · 1.82 KB
/
nim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use babel';
import fs from 'fs';
import path from 'path';
// Public: GrammarUtils.Nim - a module which selects the right file to run for Nim projects
export default {
// Public: Find the right file to run
//
// * `file` A {String} containing the current editor file
//
// Returns the {String} filepath of file to run
projectDir(editorfile) {
return path.dirname(editorfile);
},
findNimProjectFile(editorfile) {
if (path.extname(editorfile) === '.nims') {
// if we have an .nims file
const tfile = editorfile.slice(0, -1);
if (fs.existsSync(tfile)) {
// it has a corresponding .nim file. so thats a config file.
// we run the .nim file instead.
return path.basename(tfile);
}
// it has no corresponding .nim file, it is a standalone script
return path.basename(editorfile);
}
// check if we are running on a file with config
if (fs.existsSync(`${editorfile}s`) ||
fs.existsSync(`${editorfile}.cfg`) ||
fs.existsSync(`${editorfile}cfg`)) {
return path.basename(editorfile);
}
// assume we want to run a project
// searching for the first file which has
// a config file with the same name and
// run this instead of the one in the editor
// tab
const filepath = path.dirname(editorfile);
const files = fs.readdirSync(filepath);
files.sort();
for (const file of files) {
const name = `${filepath}/${file}`;
if (fs.statSync(name).isFile()) {
if (path.extname(name) === '.nims' ||
path.extname(name) === '.nimcgf' ||
path.extname(name) === '.cfg') {
const tfile = name.slice(0, -1);
if (fs.existsSync(tfile)) return path.basename(tfile);
}
}
}
// just run what we got
return path.basename(editorfile);
},
};