Skip to content

Commit f40c4c4

Browse files
committed
Merge browser and node APIs into single entry
1 parent 858ae31 commit f40c4c4

File tree

22 files changed

+157
-739
lines changed

22 files changed

+157
-739
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
cache: "npm"
2323
- run: npm ci
2424
- run: npm run build:bin
25-
- run: npm run build:browser
25+
- run: npm run build:api
2626
- run: npm run test:eslint
2727
- run: npm run test:elm-format-validate
2828
- run: npm run test:jest

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ elm-stuff
77
guida-stuff
88

99
# Main
10-
lib/guida.node.js
11-
lib/guida.node.min.js
12-
13-
# Browser
14-
lib/guida.browser.js
15-
lib/guida.browser.min.js
10+
lib/guida.js
11+
lib/guida.min.js
1612

1713
# Command line
1814
bin/guida.js

.npmignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
bin/guida.js
33
elm-stuff
44
examples
5-
lib/guida.node.js
6-
lib/guida.browser.js
5+
lib/guida.js
76
guida-stuff
87
review
98
scripts

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ For detailed instructions on setting up your environment for contributing to Gui
6767
We aim for stability and consistency. If you’re adding features or fixing bugs, please:
6868

6969
- Write tests if applicable.
70-
- Consider all 3 outputs of the project: bin (command line), browser and node (API).
70+
- Consider both outputs of the project: bin (command line) and API (browser and node).
7171
- Make sure to test all three output targets of the project:
7272
- CLI (bin) — the command-line interface
7373
- Browser — compiled for browser usage

eslint.config.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ export default defineConfig([
88
globalIgnores([
99
"bin/guida.js",
1010
"bin/guida.min.js",
11-
"lib/guida.browser.js",
12-
"lib/guida.browser.min.js",
13-
"lib/guida.node.js",
14-
"lib/guida.node.min.js",
11+
"lib/guida.js",
12+
"lib/guida.min.js",
1513
"elm-stuff",
1614
"guida-stuff",
1715
]),
1816
{ files: ["**/*.{js,mjs,cjs}"] },
1917
{ files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
2018
{ files: ["bin/**/*.{js,mjs,cjs}"], languageOptions: { globals: globals.node } },
21-
{ files: ["lib/browser.js"], languageOptions: { globals: globals.browser } },
22-
{ files: ["lib/node.js"], languageOptions: { globals: globals.node } },
19+
{ files: ["lib/index.js"], languageOptions: { globals: { ...globals.browser, ...globals.node } } },
2320
{ files: ["try/**/*.{js,mjs,cjs}"], languageOptions: { globals: { ...globals.browser, ...globals.node } } },
2421
{ files: ["scripts/*.js"], languageOptions: { globals: globals.node } },
2522
{

lib/index.d.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Configuration object expected by the Guida runner.
3+
*/
4+
export interface GuidaConfig {
5+
// Write text or binary data to a path.
6+
writeFile(path: string, data: string | ArrayBuffer | Uint8Array | Buffer): Promise<void>;
7+
8+
// Read file contents. Can return text or binary data.
9+
readFile(path: string): Promise<string | ArrayBuffer | Uint8Array | Buffer | { buffer: ArrayBuffer }>;
10+
11+
// Read a directory and return a list of files.
12+
readDirectory(path: string): Promise<{ files: string[] }>;
13+
14+
// Create a directory.
15+
createDirectory(path: string): Promise<void>;
16+
17+
// Get details for a path (file or directory).
18+
details(path: string): Promise<{ type: 'file' | 'directory' | string; createdAt?: number }>;
19+
20+
// Environment map used by the runner.
21+
env?: Record<string, any>;
22+
}
23+
24+
export interface MakeOptions {
25+
debug?: boolean;
26+
optimize?: boolean;
27+
sourcemaps?: boolean;
28+
}
29+
30+
// The runtime returns various JSON-able responses. Keep this generic to reflect the dynamic
31+
// nature of the results coming from the embedded Elm/runner process.
32+
export type GuidaResponse = any;
33+
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+
39+
declare const _default: {
40+
make: (config: GuidaConfig, path: string, options?: MakeOptions) => Promise<GuidaResponse>;
41+
format: (config: GuidaConfig, content: string) => Promise<GuidaResponse>;
42+
install: (config: GuidaConfig, pkg: string) => Promise<GuidaResponse>;
43+
uninstall: (config: GuidaConfig, pkg: string) => Promise<GuidaResponse>;
44+
};
45+
46+
export default _default;

lib/browser.js renamed to lib/index.js

Lines changed: 32 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
const { createFs } = require("indexeddb-fs");
21
const { newServer } = require("mock-xmlhttprequest");
32
const JSZip = require("jszip");
43

54
const savedXMLHttpRequest = globalThis.XMLHttpRequest;
6-
const fs = createFs({ databaseName: "guida-fs" });
75

8-
const runGuida = function (extraEnv, args) {
6+
const runGuida = function (config, args) {
97
return new Promise((resolve) => {
108
let mVarsNextCounter = 0;
119
const mVars = {};
1210
const lockedFiles = {};
1311

14-
const env = Object.assign({
15-
GUIDA_HOME: "root/.guida",
16-
}, extraEnv);
17-
1812
const download = function (method, url) {
1913
const that = this;
2014

@@ -85,16 +79,15 @@ const runGuida = function (extraEnv, args) {
8579
server.post("writeString", async (request) => {
8680
const path = request.requestHeaders.getHeader("path");
8781

88-
await fs.writeFile(path, request.body);
82+
await config.writeFile(path, request.body);
8983
request.respond(200);
9084
});
9185

9286
server.post("read", async (request) => {
93-
const content = await fs.readFile(request.body);
87+
const content = await config.readFile(request.body);
9488
request.respond(200, null, content);
9589
});
9690

97-
9891
server.post("getArchive", (request) => {
9992
download.apply({
10093
send: ({ sha, archive }) => {
@@ -105,8 +98,7 @@ const runGuida = function (extraEnv, args) {
10598

10699
server.post("dirDoesFileExist", async (request) => {
107100
try {
108-
const stats = await fs.details(request.body);
109-
console.log("dirDoesFileExist", request.body, stats);
101+
const stats = await config.details(request.body);
110102
request.respond(200, null, stats.type === "file");
111103
} catch (_err) {
112104
request.respond(200, null, false);
@@ -126,9 +118,9 @@ const runGuida = function (extraEnv, args) {
126118
await previousPromise;
127119

128120
try {
129-
await fs.details(directory);
121+
await config.details(directory);
130122
} catch (_err) {
131-
await fs.createDirectory(directory);
123+
await config.createDirectory(directory);
132124
}
133125
}, Promise.resolve());
134126

@@ -165,13 +157,13 @@ const runGuida = function (extraEnv, args) {
165157
});
166158

167159
server.post("dirGetModificationTime", async (request) => {
168-
const stats = await fs.details(request.body);
160+
const stats = await config.details(request.body);
169161
request.respond(200, null, stats.createdAt);
170162
});
171163

172164
server.post("dirDoesDirectoryExist", async (request) => {
173165
try {
174-
const stats = await fs.details(request.body);
166+
const stats = await config.details(request.body);
175167
request.respond(200, null, stats.type === "directory");
176168
} catch (_err) {
177169
request.respond(200, null, false);
@@ -183,19 +175,19 @@ const runGuida = function (extraEnv, args) {
183175
});
184176

185177
server.post("dirListDirectory", async (request) => {
186-
const { files } = await fs.readDirectory(request.body);
178+
const { files } = await config.readDirectory(request.body);
187179
request.respond(200, null, JSON.stringify(files));
188180
});
189181

190182
server.post("binaryDecodeFileOrFail", async (request) => {
191-
const data = await fs.readFile(request.body);
183+
const data = await config.readFile(request.body);
192184
request.respond(200, null, data.buffer);
193185
});
194186

195187
server.post("write", async (request) => {
196188
const path = request.requestHeaders.getHeader("path");
197189

198-
await fs.writeFile(path, request.body);
190+
await config.writeFile(path, request.body);
199191
request.respond(200);
200192
});
201193

@@ -204,7 +196,7 @@ const runGuida = function (extraEnv, args) {
204196
});
205197

206198
server.post("envLookupEnv", (request) => {
207-
const envVar = env[request.body] ?? null;
199+
const envVar = config.env[request.body] ?? null;
208200
request.respond(200, null, JSON.stringify(envVar));
209201
});
210202

@@ -297,8 +289,6 @@ const runGuida = function (extraEnv, args) {
297289
});
298290

299291
server.setDefaultHandler((request) => {
300-
console.log("defaultHandler", request.url);
301-
302292
const headers = request.requestHeaders.getHash();
303293

304294
var xhr = new savedXMLHttpRequest();
@@ -317,63 +307,29 @@ const runGuida = function (extraEnv, args) {
317307

318308
server.install();
319309

320-
const { Elm } = require("./guida.browser.min.js");
310+
const { Elm } = require("./guida.min.js");
321311

322-
Elm.Browser.Main.init();
312+
Elm.API.Main.init();
323313
});
324-
}
325-
326-
const elmJsonContent = `{
327-
"type": "application",
328-
"source-directories": [
329-
"src"
330-
],
331-
"elm-version": "0.19.1",
332-
"dependencies": {
333-
"direct": {
334-
"elm/browser": "1.0.2",
335-
"elm/core": "1.0.5",
336-
"elm/html": "1.0.0"
337-
},
338-
"indirect": {
339-
"elm/json": "1.1.3",
340-
"elm/time": "1.0.0",
341-
"elm/url": "1.0.0",
342-
"elm/virtual-dom": "1.0.3"
343-
}
344-
},
345-
"test-dependencies": {
346-
"direct": {},
347-
"indirect": {}
348-
}
349-
}`;
314+
};
350315

351316
module.exports = {
352-
init: async (extraEnv) => {
353-
await fs.writeFile("root/elm.json", elmJsonContent);
354-
await fs.createDirectory("root/src");
355-
356-
return {
357-
make: async (content, options) => {
358-
await fs.writeFile("root/src/Main.guida", content);
359-
360-
return await runGuida(extraEnv, {
361-
command: "make",
362-
path: "src/Main.guida",
363-
debug: !!options.debug,
364-
optimize: !!options.optimize,
365-
sourcemaps: !!options.sourcemaps
366-
});
367-
},
368-
format: async (content) => {
369-
return await runGuida(extraEnv, { command: "format", content });
370-
},
371-
install: async (pkg) => {
372-
return await runGuida(extraEnv, { command: "install", pkg });
373-
},
374-
uninstall: async (pkg) => {
375-
return await runGuida(extraEnv, { command: "uninstall", pkg });
376-
}
377-
};
317+
make: async (config, path, options) => {
318+
return await runGuida(config, {
319+
command: "make",
320+
path,
321+
debug: !!options.debug,
322+
optimize: !!options.optimize,
323+
sourcemaps: !!options.sourcemaps
324+
});
325+
},
326+
format: async (config, content) => {
327+
return await runGuida(config, { command: "format", content });
328+
},
329+
install: async (config, pkg) => {
330+
return await runGuida(config, { command: "install", pkg });
331+
},
332+
uninstall: async (config, pkg) => {
333+
return await runGuida(config, { command: "uninstall", pkg });
378334
}
379335
};

lib/node.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)