1
- namespace phasereditor2d . ide . core . code {
1
+ namespace phasereditor2d . ide . core . code {
2
2
3
3
import io = colibri . core . io ;
4
4
5
- export function getImportPath ( file : io . FilePath , importFile : io . FilePath ) : string {
5
+ export function isCopiedLibraryFile ( file : io . FilePath ) {
6
+
7
+ if ( file . isRoot ( ) ) {
8
+
9
+ return false ;
10
+ }
11
+
12
+ const name = "library.txt" ;
13
+
14
+ if ( file . isFolder ( ) ) {
15
+
16
+ if ( file . getFile ( name ) ) {
17
+
18
+ return true ;
19
+ }
20
+
21
+ } else if ( file . getName ( ) === name || file . getSibling ( name ) ) {
22
+
23
+ return true ;
24
+ }
25
+
26
+ return isCopiedLibraryFile ( file . getParent ( ) ) ;
27
+ }
28
+
29
+ export function isNodeLibraryFile ( file : io . FilePath ) {
30
+
31
+ if ( file . isFolder ( ) && file . getName ( ) === "node_modules" ) {
32
+
33
+ return true ;
34
+ }
35
+
36
+ if ( file . isRoot ( ) ) {
37
+
38
+ return false ;
39
+ }
40
+
41
+ return isNodeLibraryFile ( file . getParent ( ) ) ;
42
+ }
43
+
44
+ export function findNodeModuleName ( file : io . FilePath ) : string {
45
+
46
+ if ( file . isRoot ( ) || file . getParent ( ) . isRoot ( ) ) {
47
+
48
+ return null ;
49
+ }
50
+
51
+ const parentName = file . getParent ( ) . getName ( ) ;
52
+ const fileName = file . getName ( ) ;
53
+
54
+ // try case node_modules/<current-files>
55
+
56
+ if ( parentName === "node_modules" ) {
57
+
58
+ return fileName ;
59
+ }
60
+
61
+ const parentParentName = file . getParent ( ) . getParent ( ) . getName ( ) ;
62
+
63
+ // try case node_modules/@org /<current-file>
64
+
65
+ if ( parentName . startsWith ( "@" ) && parentParentName === "node_modules" ) {
66
+
67
+ return parentName + "/" + fileName ;
68
+ }
69
+
70
+ return findNodeModuleName ( file . getParent ( ) ) ;
71
+ }
72
+
73
+ export function getImportPath ( file : io . FilePath , importFile : io . FilePath ) : { importPath : string , asDefault : boolean } {
74
+
75
+ const nodeModule = findNodeModuleName ( importFile ) ;
76
+
77
+ if ( nodeModule ) {
78
+
79
+ return { importPath : nodeModule , asDefault : false } ;
80
+ }
6
81
7
82
const parent = file . getParent ( ) ;
8
83
const parentPath = parent . getFullName ( ) ;
@@ -12,15 +87,18 @@ namespace phasereditor2d.ide.core.code {
12
87
13
88
if ( parent === importFile . getParent ( ) ) {
14
89
15
- return "./" + importFile . getNameWithoutExtension ( ) ;
90
+ return { importPath : "./" + importFile . getNameWithoutExtension ( ) , asDefault : true } ;
16
91
}
17
92
18
93
if ( importFilePath . startsWith ( parentPath + "/" ) ) {
19
94
20
- return "./" + importFileElements . slice ( parentElements . length ) . join ( "/" ) ;
95
+ return {
96
+ importPath : "./" + importFileElements . slice ( parentElements . length ) . join ( "/" ) ,
97
+ asDefault : true
98
+ } ;
21
99
}
22
100
23
- while ( parentElements . length > 0 ) {
101
+ while ( parentElements . length > 0 ) {
24
102
25
103
const parentFirst = parentElements . shift ( ) ;
26
104
const importFileFirst = importFileElements . shift ( ) ;
@@ -29,11 +107,14 @@ namespace phasereditor2d.ide.core.code {
29
107
30
108
importFileElements . unshift ( importFileFirst ) ;
31
109
32
- return "../" . repeat ( parentElements . length + 1 ) + importFileElements . join ( "/" ) ;
110
+ return {
111
+ importPath : "../" . repeat ( parentElements . length + 1 ) + importFileElements . join ( "/" ) ,
112
+ asDefault : true
113
+ } ;
33
114
}
34
115
}
35
116
36
- return "" ;
117
+ return { importPath : "" , asDefault : true } ;
37
118
}
38
119
39
120
function isAlphaNumeric ( c : string ) {
0 commit comments