Skip to content

Commit b31921d

Browse files
committed
Add diagnostics api method
1 parent a09f1a2 commit b31921d

File tree

8 files changed

+87
-268
lines changed

8 files changed

+87
-268
lines changed

lib/index.d.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* Configuration object expected by the Guida runner.
33
*/
44
export interface GuidaConfig {
5+
// XMLHttpRequest constructor for making HTTP requests.
6+
XMLHttpRequest: typeof XMLHttpRequest;
7+
58
// Write text or binary data to a path.
69
writeFile(path: string, data: string | ArrayBuffer | Uint8Array | Buffer): Promise<void>;
710

@@ -17,6 +20,12 @@ export interface GuidaConfig {
1720
// Get details for a path (file or directory).
1821
details(path: string): Promise<{ type: 'file' | 'directory' | string; createdAt?: number }>;
1922

23+
// Returns the current working directory.
24+
getCurrentDirectory(): Promise<string>;
25+
26+
// Returns the string path of the current user's home directory.
27+
homedir(): Promise<string>;
28+
2029
// Environment map used by the runner.
2130
env?: Record<string, any>;
2231
}
@@ -31,16 +40,12 @@ export interface MakeOptions {
3140
// nature of the results coming from the embedded Elm/runner process.
3241
export type GuidaResponse = any;
3342

34-
export function make(config: GuidaConfig, path: string, options?: MakeOptions): Promise<GuidaResponse>;
35-
export function format(config: GuidaConfig, content: string): Promise<GuidaResponse>;
36-
export function install(config: GuidaConfig, pkg: string): Promise<GuidaResponse>;
37-
export function uninstall(config: GuidaConfig, pkg: string): Promise<GuidaResponse>;
38-
3943
declare const _default: {
4044
make: (config: GuidaConfig, path: string, options?: MakeOptions) => Promise<GuidaResponse>;
4145
format: (config: GuidaConfig, content: string) => Promise<GuidaResponse>;
4246
install: (config: GuidaConfig, pkg: string) => Promise<GuidaResponse>;
4347
uninstall: (config: GuidaConfig, pkg: string) => Promise<GuidaResponse>;
48+
diagnostics: (config: GuidaConfig, args: { content: string } | { path: string }) => Promise<{ errors?: any }>;
4449
};
4550

4651
export default _default;

lib/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const { newServer } = require("mock-xmlhttprequest");
22
const JSZip = require("jszip");
33

4-
const savedXMLHttpRequest = globalThis.XMLHttpRequest;
5-
64
const runGuida = function (config, args) {
75
return new Promise((resolve) => {
86
let mVarsNextCounter = 0;
@@ -12,7 +10,7 @@ const runGuida = function (config, args) {
1210
const download = function (method, url) {
1311
const that = this;
1412

15-
const xhr = new savedXMLHttpRequest();
13+
const xhr = new config.XMLHttpRequest();
1614
xhr.open(method, url, true);
1715
xhr.responseType = "arraybuffer";
1816

@@ -108,10 +106,11 @@ const runGuida = function (config, args) {
108106
server.post("dirCreateDirectoryIfMissing", async (request) => {
109107
const { createParents, filename } = JSON.parse(request.body);
110108
let directories = [filename];
109+
let prefix = filename.startsWith("/") ? "/" : "";
111110

112111
if (createParents) {
113112
directories = filename.split('/').filter(Boolean);
114-
directories = directories.map((_, index) => directories.slice(0, index + 1).join('/'));
113+
directories = directories.map((_, index) => prefix + directories.slice(0, index + 1).join('/'));
115114
}
116115

117116
await directories.reduce(async (previousPromise, directory) => {
@@ -191,17 +190,19 @@ const runGuida = function (config, args) {
191190
request.respond(200);
192191
});
193192

194-
server.post("dirGetCurrentDirectory", (request) => {
195-
request.respond(200, null, "root");
193+
server.post("dirGetCurrentDirectory", async (request) => {
194+
const currentDir = await config.getCurrentDirectory();
195+
request.respond(200, null, currentDir);
196196
});
197197

198198
server.post("envLookupEnv", (request) => {
199199
const envVar = config.env[request.body] ?? null;
200200
request.respond(200, null, JSON.stringify(envVar));
201201
});
202202

203-
server.post("dirGetAppUserDataDirectory", (request) => {
204-
request.respond(200, null, `root/.${request.body}`);
203+
server.post("dirGetAppUserDataDirectory", async (request) => {
204+
const homedir = await config.homedir();
205+
request.respond(200, null, `${homedir}/.${request.body}`);
205206
});
206207

207208
// MVARS
@@ -291,7 +292,7 @@ const runGuida = function (config, args) {
291292
server.setDefaultHandler((request) => {
292293
const headers = request.requestHeaders.getHash();
293294

294-
var xhr = new savedXMLHttpRequest();
295+
var xhr = new config.XMLHttpRequest();
295296
xhr.open(request.method, request.url, true);
296297

297298
for (const key in headers) {
@@ -331,5 +332,8 @@ module.exports = {
331332
},
332333
uninstall: async (config, pkg) => {
333334
return await runGuida(config, { command: "uninstall", pkg });
335+
},
336+
diagnostics: async (config, args) => {
337+
return await runGuida(config, { command: "diagnostics", ...args });
334338
}
335339
};

src/API/Main.elm

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import API.Uninstall as Uninstall
77
import Builder.Reporting.Exit as Exit
88
import Compiler.Elm.Package as Pkg
99
import Compiler.Json.Encode as E
10+
import Compiler.Parse.Module as M
1011
import Compiler.Parse.Primitives as P
12+
import Compiler.Parse.SyntaxVersion as SV
13+
import Compiler.Reporting.Error as Error
14+
import Compiler.Reporting.Error.Syntax as E
15+
import Compiler.Reporting.Render.Code as Code
1116
import Json.Decode as Decode
1217
import Json.Encode as Encode
1318
import System.IO as IO
@@ -39,8 +44,8 @@ app =
3944
exitWithResponse (Encode.object [ ( "error", Encode.string (E.encodeUgly (Exit.toJson (Exit.makeToReport error))) ) ])
4045
)
4146

42-
FormatArgs path ->
43-
case Format.run path of
47+
FormatArgs content ->
48+
case Format.run content of
4449
Ok output ->
4550
exitWithResponse (Encode.object [ ( "output", Encode.string output ) ])
4651

@@ -64,6 +69,40 @@ app =
6469

6570
Err _ ->
6671
exitWithResponse (Encode.object [ ( "error", Encode.string "Invalid package..." ) ])
72+
73+
DiagnosticsArgs (DiagnosticsSourceContent src) ->
74+
case P.fromByteString (M.chompModule SV.Guida M.Application) E.ModuleBadEnd src of
75+
Ok _ ->
76+
exitWithResponse (Encode.object [])
77+
78+
Err err ->
79+
let
80+
source =
81+
Code.toSource src
82+
83+
error : Encode.Value
84+
error =
85+
E.encodeUgly (Error.reportToJson (E.toReport SV.Guida source (E.ParseError err)))
86+
|> Decode.decodeString Decode.value
87+
|> Result.withDefault Encode.null
88+
in
89+
exitWithResponse (Encode.object [ ( "errors", Encode.list identity [ error ] ) ])
90+
91+
DiagnosticsArgs (DiagnosticsSourcePath path) ->
92+
Make.run path (Make.Flags False False False)
93+
|> Task.bind
94+
(\result ->
95+
case result of
96+
Ok _ ->
97+
exitWithResponse (Encode.object [])
98+
99+
Err error ->
100+
exitWithResponse
101+
(E.encodeUgly (Exit.toJson (Exit.makeToReport error))
102+
|> Decode.decodeString Decode.value
103+
|> Result.withDefault Encode.null
104+
)
105+
)
67106
)
68107

69108

@@ -86,6 +125,12 @@ type Args
86125
| FormatArgs String
87126
| InstallArgs String
88127
| UninstallArgs String
128+
| DiagnosticsArgs DiagnosticsSource
129+
130+
131+
type DiagnosticsSource
132+
= DiagnosticsSourceContent String
133+
| DiagnosticsSourcePath String
89134

90135

91136
argsDecoder : Decode.Decoder Args
@@ -113,6 +158,14 @@ argsDecoder =
113158
Decode.map UninstallArgs
114159
(Decode.field "pkg" Decode.string)
115160

161+
"diagnostics" ->
162+
Decode.map DiagnosticsArgs
163+
(Decode.oneOf
164+
[ Decode.map DiagnosticsSourceContent (Decode.field "content" Decode.string)
165+
, Decode.map DiagnosticsSourcePath (Decode.field "path" Decode.string)
166+
]
167+
)
168+
116169
_ ->
117170
Decode.fail ("Unknown command: " ++ command)
118171
)

src/Compiler/Reporting/Error.elm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Compiler.Reporting.Error exposing
33
, Module
44
, moduleDecoder
55
, moduleEncoder
6+
, reportToJson
67
, toDoc
78
, toJson
89
)

try/app.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ const { createFs } = require("indexeddb-fs");
44
const fs = createFs({ databaseName: "guida-fs" });
55

66
const config = {
7+
XMLHttpRequest: globalThis.XMLHttpRequest,
78
env: {
8-
GUIDA_REGISTRY: "/proxy/https://package.elm-lang.org"
9+
GUIDA_REGISTRY: "https://guida-package-registry.fly.dev"
910
},
1011
writeFile: fs.writeFile,
1112
readFile: fs.readFile,
1213
details: fs.details,
1314
createDirectory: fs.createDirectory,
14-
readDirectory: fs.readDirectory
15+
readDirectory: fs.readDirectory,
16+
getCurrentDirectory: async () => "root",
17+
homedir: async () => "root"
1518
};
1619

1720
window.addEventListener("load", async () => {

0 commit comments

Comments
 (0)