trebol-dev 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -4
- package/app/app.trb +570 -0
- package/app/demo.trb +36 -0
- package/app/favicon.svg +10 -0
- package/app/history.trb +43 -0
- package/app/model.trb +611 -0
- package/app/move-sample.trb +21 -0
- package/app/move.trb +129 -0
- package/app/options.trb +404 -0
- package/app/palette.trb +145 -0
- package/app/props.trb +304 -0
- package/app/sample.trb +16 -0
- package/app/samples.trb +46 -0
- package/app/trebol-code.js +13 -0
- package/bin/trebol-dev.js +55 -0
- package/package.json +44 -6
- package/server/files.js +111 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// trebol-dev [folder] [--port n]: the designer for the Trebol project in the folder (the current
|
|
3
|
+
// one by default), in the browser. It runs Trebol's own dev server on the designer's app, with
|
|
4
|
+
// the file API of server/files.js, and leaves it running until Ctrl+C.
|
|
5
|
+
|
|
6
|
+
import { spawn } from 'node:child_process';
|
|
7
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
8
|
+
import { dirname, join, resolve } from 'node:path';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
|
|
11
|
+
const USAGE = 'usage: trebol-dev [folder] [--port n]';
|
|
12
|
+
const here = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
13
|
+
|
|
14
|
+
let folder;
|
|
15
|
+
let port = '5180';
|
|
16
|
+
const args = process.argv.slice(2);
|
|
17
|
+
for (let i = 0; i < args.length; i++) {
|
|
18
|
+
const a = args[i];
|
|
19
|
+
if (a === '--port' && /^\d+$/.test(args[i + 1] ?? '')) port = args[++i];
|
|
20
|
+
else if (a === '--version' || a === '-v') {
|
|
21
|
+
console.log(JSON.parse(readFileSync(join(here, 'package.json'), 'utf8')).version);
|
|
22
|
+
process.exit(0);
|
|
23
|
+
} else if (folder === undefined && !a.startsWith('-')) folder = a;
|
|
24
|
+
else {
|
|
25
|
+
console.error(USAGE);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const project = resolve(folder ?? '.');
|
|
30
|
+
if (!existsSync(project)) {
|
|
31
|
+
console.error(`trebol-dev: no folder ${project}`);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The folder of the trebol package this one depends on. */
|
|
36
|
+
function trebolRoot() {
|
|
37
|
+
let dir = dirname(fileURLToPath(import.meta.resolve('trebol/core')));
|
|
38
|
+
while (!existsSync(join(dir, 'package.json')) || JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')).name !== 'trebol') {
|
|
39
|
+
const up = dirname(dir);
|
|
40
|
+
if (up === dir) throw new Error('trebol-dev: cannot find the trebol package');
|
|
41
|
+
dir = up;
|
|
42
|
+
}
|
|
43
|
+
return dir;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const trebol = trebolRoot();
|
|
47
|
+
// A checkout of trebol runs its sources; an installed one, its built command.
|
|
48
|
+
const cli = existsSync(join(trebol, 'tools', 'cli.ts')) ? join(trebol, 'tools', 'cli.ts') : join(trebol, 'dist', 'cli.js');
|
|
49
|
+
const child = spawn(process.execPath, ['--disable-warning=ExperimentalWarning', cli, 'dev', join(here, 'app'), '--port', port, '--api', join(here, 'server', 'files.js')], {
|
|
50
|
+
stdio: 'inherit',
|
|
51
|
+
env: { ...process.env, TREBOL_DEV_PROJECT: project },
|
|
52
|
+
});
|
|
53
|
+
console.log(`trebol-dev: designing ${project}`);
|
|
54
|
+
child.on('exit', code => process.exit(code ?? 0));
|
|
55
|
+
for (const s of ['SIGINT', 'SIGTERM']) process.on(s, () => child.kill(s));
|
package/package.json
CHANGED
|
@@ -1,13 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trebol-dev",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "The visual designer for Trebol: lays out screens with Trebol's faces and writes the Trebol code back, keeping yours as you wrote it.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
|
-
"
|
|
6
|
+
"type": "module",
|
|
7
|
+
"homepage": "https://trebol-lang.com/",
|
|
7
8
|
"repository": {
|
|
8
9
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/acasadom/trebol.git"
|
|
10
|
+
"url": "git+https://github.com/acasadom/trebol-dev.git"
|
|
10
11
|
},
|
|
11
|
-
"
|
|
12
|
-
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/acasadom/trebol-dev/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"trebol",
|
|
17
|
+
"designer",
|
|
18
|
+
"devtools"
|
|
19
|
+
],
|
|
20
|
+
"bin": {
|
|
21
|
+
"trebol-dev": "bin/trebol-dev.js"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"bin",
|
|
25
|
+
"server",
|
|
26
|
+
"app",
|
|
27
|
+
"!app/*.test.trb",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=22.18"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "node --test && trebol test app",
|
|
36
|
+
"start": "node bin/trebol-dev.js",
|
|
37
|
+
"build:editor": "esbuild editor/trebol-code.js --bundle --minify --format=esm --target=es2022 --legal-comments=eof --outfile=app/trebol-code.js"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"trebol": "^0.3.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@codemirror/commands": "^6.11.1",
|
|
44
|
+
"@codemirror/language": "^6.13.1",
|
|
45
|
+
"@codemirror/search": "^6.7.2",
|
|
46
|
+
"@codemirror/state": "^6.7.6",
|
|
47
|
+
"@codemirror/view": "^6.43.15",
|
|
48
|
+
"@lezer/highlight": "^1.2.5",
|
|
49
|
+
"esbuild": "^0.28.2"
|
|
50
|
+
}
|
|
13
51
|
}
|
package/server/files.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// The designer's window on the project: lists its .trb files, reads one and writes one back.
|
|
2
|
+
// `trebol dev --api` calls handleApi for every request; the project folder comes in
|
|
3
|
+
// TREBOL_DEV_PROJECT. Only this machine reaches the server, and only a page of the designer
|
|
4
|
+
// itself may ask: the Host must be a local name (no DNS rebinding) and requests carry the
|
|
5
|
+
// X-Trebol-Dev header, which a page of another site cannot send without the server agreeing.
|
|
6
|
+
|
|
7
|
+
import { readFile, writeFile, readdir, realpath } from 'node:fs/promises';
|
|
8
|
+
import { basename, dirname, extname, relative, resolve, sep } from 'node:path';
|
|
9
|
+
|
|
10
|
+
/** Folders never listed nor read: tools' output and other people's code. */
|
|
11
|
+
const SKIP = new Set(['node_modules', 'dist', '_site', '_build']);
|
|
12
|
+
/** The largest file the designer writes, in bytes. */
|
|
13
|
+
const MOST = 4 * 1024 * 1024;
|
|
14
|
+
|
|
15
|
+
/** The project folder. */
|
|
16
|
+
export function projectRoot() {
|
|
17
|
+
return resolve(process.env.TREBOL_DEV_PROJECT ?? '.');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Whether a request comes from a page of the designer. */
|
|
21
|
+
export function trusted(req) {
|
|
22
|
+
const host = req.headers.host ?? '';
|
|
23
|
+
if (!/^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/.test(host)) return false;
|
|
24
|
+
const origin = req.headers.origin;
|
|
25
|
+
if (origin !== undefined && origin !== `http://${host}`) return false;
|
|
26
|
+
return req.headers['x-trebol-dev'] === '1';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const hidden = part => part.startsWith('.') || SKIP.has(part);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The path of the project file `name` names, or null when it is not a .trb file inside the
|
|
33
|
+
* project, outside hidden and skipped folders, links included. `name` uses / between folders.
|
|
34
|
+
*/
|
|
35
|
+
export async function fileOf(root, name) {
|
|
36
|
+
if (typeof name !== 'string' || name === '' || extname(name) !== '.trb') return null;
|
|
37
|
+
const parts = name.split('/');
|
|
38
|
+
if (parts.some(p => p === '' || p === '..' || hidden(p))) return null;
|
|
39
|
+
const path = resolve(root, ...parts);
|
|
40
|
+
const realRoot = await realpath(root);
|
|
41
|
+
const realDir = await realpath(dirname(path)).catch(() => null);
|
|
42
|
+
if (realDir === null || !(realDir === realRoot || realDir.startsWith(realRoot + sep))) return null;
|
|
43
|
+
const realFile = await realpath(path).catch(() => null);
|
|
44
|
+
if (realFile !== null && !realFile.startsWith(realRoot + sep)) return null;
|
|
45
|
+
return path;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Every .trb file of the project, as paths with / from its folder, in order. */
|
|
49
|
+
export async function listFiles(root) {
|
|
50
|
+
const out = [];
|
|
51
|
+
const walk = async dir => {
|
|
52
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);
|
|
53
|
+
for (const e of entries) {
|
|
54
|
+
if (hidden(e.name)) continue;
|
|
55
|
+
const path = resolve(dir, e.name);
|
|
56
|
+
if (e.isDirectory()) await walk(path);
|
|
57
|
+
else if (e.isFile() && extname(e.name) === '.trb') out.push(relative(root, path).split(sep).join('/'));
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
await walk(root);
|
|
61
|
+
return out.sort();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function send(res, status, body, type = 'text/plain; charset=utf-8') {
|
|
65
|
+
res.writeHead(status, { 'content-type': type, 'cache-control': 'no-store' });
|
|
66
|
+
res.end(body);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function bodyOf(req) {
|
|
70
|
+
const chunks = [];
|
|
71
|
+
let size = 0;
|
|
72
|
+
for await (const c of req) {
|
|
73
|
+
size += c.length;
|
|
74
|
+
if (size > MOST) return null;
|
|
75
|
+
chunks.push(c);
|
|
76
|
+
}
|
|
77
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Answers /api/files and /api/file; gives false for every other request. */
|
|
81
|
+
export async function handleApi(req, res, url) {
|
|
82
|
+
if (url.pathname !== '/api/files' && url.pathname !== '/api/file') return false;
|
|
83
|
+
if (!trusted(req)) {
|
|
84
|
+
send(res, 403, 'Only the designer may ask for the files of the project.');
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
const root = projectRoot();
|
|
88
|
+
if (url.pathname === '/api/files') {
|
|
89
|
+
if (req.method !== 'GET') return send(res, 405, 'GET only'), true;
|
|
90
|
+
send(res, 200, JSON.stringify({ name: basename(root), files: await listFiles(root) }), 'application/json');
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
const path = await fileOf(root, url.searchParams.get('path'));
|
|
94
|
+
if (path === null) {
|
|
95
|
+
send(res, 400, 'Not a .trb file of the project.');
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
if (req.method === 'GET') {
|
|
99
|
+
const text = await readFile(path, 'utf8').catch(() => null);
|
|
100
|
+
if (text === null) send(res, 404, 'No such file.');
|
|
101
|
+
else send(res, 200, text);
|
|
102
|
+
} else if (req.method === 'PUT') {
|
|
103
|
+
const text = await bodyOf(req);
|
|
104
|
+
if (text === null) return send(res, 413, 'Too big.'), true;
|
|
105
|
+
await writeFile(path, text, 'utf8');
|
|
106
|
+
send(res, 200, 'Saved.');
|
|
107
|
+
} else {
|
|
108
|
+
send(res, 405, 'GET or PUT only');
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
}
|