@@ -3,13 +3,18 @@ module Compiler.Outline exposing
3
3
, AppOutline
4
4
, PkgOutline
5
5
, VersionConstraint(..)
6
+ --
7
+ , findSourceFiles
8
+ --
6
9
, jsonDecoder
7
10
, toJson
8
11
)
9
12
10
13
14
+ import Bytes
11
15
import SemanticVersion exposing (SemanticVersion)
12
16
import SemanticVersionRange exposing (SemanticVersionRange)
17
+ import FileSystem
13
18
import FileSystem.Path as Path exposing (Path)
14
19
import Json.Decode as Decode exposing (Decoder)
15
20
import Json.Encode as Encode
@@ -18,6 +23,7 @@ import Compiler.ModuleName as ModuleName exposing (ModuleName)
18
23
import Compiler.License as License exposing (License)
19
24
import Compiler.Platform as Platform exposing (Platform)
20
25
import Dict exposing (Dict)
26
+ import Task exposing (Task)
21
27
22
28
23
29
type Outline
@@ -58,6 +64,69 @@ type VersionConstraint a
58
64
| LocalPath Path
59
65
60
66
67
+ findSourceFiles : FileSystem.Permission -> Outline -> Path -> Task FileSystem.Error (Array { moduleName : String, source : String })
68
+ findSourceFiles fsPerm outline outlinePath =
69
+ let
70
+ sourceDirs =
71
+ when outline is
72
+ App appOutline ->
73
+ appOutline.sourceDirectories
74
+
75
+ Pkg _ ->
76
+ [ Path.fromPosixString "src"
77
+ ]
78
+ in
79
+ Array.map (\srcDir -> Path.append srcDir outlinePath) sourceDirs
80
+ |> Array.map (findSourceFilesHelp fsPerm Path.empty)
81
+ |> Task.sequence
82
+ |> Task.map Array.flatten
83
+ |> Task.andThen (\paths -> Task.sequence <| Array.map (readSourceFile fsPerm) paths)
84
+
85
+
86
+ findSourceFilesHelp : FileSystem.Permission -> Path -> Path -> Task FileSystem.Error (Array { absolute : Path, relative : Path })
87
+ findSourceFilesHelp fsPerm local root =
88
+ FileSystem.listDirectory fsPerm root
89
+ |> Task.andThen
90
+ (\paths ->
91
+ paths
92
+ |> Array.mapAndKeepJust
93
+ (\entry ->
94
+ when entry.entityType is
95
+ FileSystem.Directory ->
96
+ Just <| findSourceFilesHelp fsPerm (Path.append entry.path local) (Path.append entry.path root)
97
+
98
+ FileSystem.File ->
99
+ if entry.path.extension == "js" || entry.path.extension == "gren" then
100
+ Just <| Task.succeed [ { absolute = Path.append entry.path root, relative = Path.append entry.path local } ]
101
+
102
+ else
103
+ Nothing
104
+
105
+ _ ->
106
+ Nothing
107
+ )
108
+ |> Task.sequence
109
+ |> Task.map Array.flatten
110
+ )
111
+
112
+
113
+ readSourceFile : FileSystem.Permission -> { absolute : Path, relative : Path } -> Task FileSystem.Error { moduleName : String, source : String }
114
+ readSourceFile fsPerm { absolute, relative } =
115
+ FileSystem.readFile fsPerm absolute
116
+ |> Task.map
117
+ (\source ->
118
+ { moduleName =
119
+ { relative | extension = "" }
120
+ |> Path.toPosixString
121
+ |> String.replace "/" "."
122
+ , source =
123
+ -- TODO: Error
124
+ Bytes.toString source
125
+ |> Maybe.withDefault ""
126
+ }
127
+ )
128
+
129
+
61
130
jsonDecoder : Decoder Outline
62
131
jsonDecoder =
63
132
Decode.field "type" Decode.string
0 commit comments