eliscript 0.0.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/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
diagnosticFromError,
|
|
8
|
+
loadCompiler,
|
|
9
|
+
renderDiagnostic,
|
|
10
|
+
} from "./bun.mjs";
|
|
11
|
+
import { readProjectConfiguration } from "./project-cli.mjs";
|
|
12
|
+
import {
|
|
13
|
+
defaultWatchInterval,
|
|
14
|
+
maximumWatchInterval,
|
|
15
|
+
minimumWatchInterval,
|
|
16
|
+
ProjectWatcher,
|
|
17
|
+
} from "./watch.mjs";
|
|
18
|
+
|
|
19
|
+
const usage = `Usage: eliscript-watch (--root DIRECTORY | --config FILE) [OPTIONS]
|
|
20
|
+
|
|
21
|
+
Options:
|
|
22
|
+
--root DIRECTORY Observe regular .eli files below DIRECTORY
|
|
23
|
+
--config FILE Resolve and observe a versioned project configuration
|
|
24
|
+
--interval MS Poll every ${defaultWatchInterval} ms by default (${minimumWatchInterval}-${maximumWatchInterval})
|
|
25
|
+
--json Emit one versioned JSON event per line
|
|
26
|
+
-h, --help Show this help
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
export function parseArguments(arguments_) {
|
|
30
|
+
const argumentsList = [...arguments_];
|
|
31
|
+
let root;
|
|
32
|
+
let configuration;
|
|
33
|
+
let interval = defaultWatchInterval;
|
|
34
|
+
let json = false;
|
|
35
|
+
while (argumentsList.length > 0) {
|
|
36
|
+
const argument = argumentsList.shift();
|
|
37
|
+
if (argument === "-h" || argument === "--help") return { help: true };
|
|
38
|
+
if (argument === "--root") {
|
|
39
|
+
if (argumentsList.length === 0) throw new Error("--root requires a directory");
|
|
40
|
+
root = argumentsList.shift();
|
|
41
|
+
} else if (argument === "--config") {
|
|
42
|
+
if (argumentsList.length === 0) throw new Error("--config requires a file");
|
|
43
|
+
configuration = argumentsList.shift();
|
|
44
|
+
} else if (argument === "--interval") {
|
|
45
|
+
if (argumentsList.length === 0) throw new Error("--interval requires milliseconds");
|
|
46
|
+
const text = argumentsList.shift();
|
|
47
|
+
if (!/^[0-9]+$/u.test(text)) throw new Error("--interval requires an integer");
|
|
48
|
+
interval = Number(text);
|
|
49
|
+
} else if (argument === "--json") {
|
|
50
|
+
json = true;
|
|
51
|
+
} else {
|
|
52
|
+
throw new Error(`unknown option: ${argument}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if ((root === undefined) === (configuration === undefined)) {
|
|
56
|
+
throw new Error("choose exactly one of --root or --config");
|
|
57
|
+
}
|
|
58
|
+
return { root, configuration, interval, json };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function writeEvent(event, json) {
|
|
62
|
+
if (json) {
|
|
63
|
+
process.stdout.write(`${JSON.stringify(event)}\n`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (event.event === "ready") {
|
|
67
|
+
process.stdout.write(`watching ${event.root}\n`);
|
|
68
|
+
} else if (event.event === "error") {
|
|
69
|
+
process.stdout.write(`watch error: ${renderDiagnostic(event.diagnostic)}\n`);
|
|
70
|
+
} else {
|
|
71
|
+
for (const change of event.changes) {
|
|
72
|
+
process.stdout.write(`${change.kind} ${change.path ?? change.file}\n`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function watchOptions(parsed, options) {
|
|
78
|
+
if (parsed.configuration === undefined) {
|
|
79
|
+
return { root: parsed.root, configuration: null, interval: parsed.interval };
|
|
80
|
+
}
|
|
81
|
+
const compiler = await loadCompiler(options.moduleDirectory);
|
|
82
|
+
const request = await readProjectConfiguration(parsed.configuration, compiler);
|
|
83
|
+
return {
|
|
84
|
+
root: request.root,
|
|
85
|
+
configuration: request.configuration,
|
|
86
|
+
interval: parsed.interval,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function execute(arguments_, options = {}) {
|
|
91
|
+
const parsed = parseArguments(arguments_);
|
|
92
|
+
if (parsed.help) return { help: true };
|
|
93
|
+
const watcher = await ProjectWatcher.create(await watchOptions(parsed, options));
|
|
94
|
+
return { watcher, json: parsed.json };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function main(arguments_ = process.argv.slice(2)) {
|
|
98
|
+
const execution = await execute(arguments_);
|
|
99
|
+
if (execution.help) {
|
|
100
|
+
process.stdout.write(usage);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const { watcher, json } = execution;
|
|
104
|
+
let stop;
|
|
105
|
+
const stopped = new Promise((resolveStopped) => {
|
|
106
|
+
stop = resolveStopped;
|
|
107
|
+
});
|
|
108
|
+
const handleSignal = () => stop();
|
|
109
|
+
process.once("SIGINT", handleSignal);
|
|
110
|
+
process.once("SIGTERM", handleSignal);
|
|
111
|
+
try {
|
|
112
|
+
watcher.start((event) => writeEvent(event, json));
|
|
113
|
+
await stopped;
|
|
114
|
+
} finally {
|
|
115
|
+
process.removeListener("SIGINT", handleSignal);
|
|
116
|
+
process.removeListener("SIGTERM", handleSignal);
|
|
117
|
+
await watcher.close();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const isMain = process.argv[1] !== undefined &&
|
|
122
|
+
resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
|
123
|
+
if (isMain) {
|
|
124
|
+
main().catch((error) => {
|
|
125
|
+
const diagnostic = diagnosticFromError(error);
|
|
126
|
+
console.error(`eliscript-watch: ${renderDiagnostic(diagnostic)}`);
|
|
127
|
+
process.exitCode = 1;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readdir, readFile, realpath, stat } from "node:fs/promises";
|
|
3
|
+
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
|
|
5
|
+
export const watchEventFormat = "eliscript-watch-event";
|
|
6
|
+
export const watchEventVersion = 1;
|
|
7
|
+
export const defaultWatchInterval = 250;
|
|
8
|
+
export const minimumWatchInterval = 25;
|
|
9
|
+
export const maximumWatchInterval = 60_000;
|
|
10
|
+
|
|
11
|
+
function freeze(value) {
|
|
12
|
+
return Object.freeze(value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function watchError(message, filename = undefined) {
|
|
16
|
+
const error = new Error(message);
|
|
17
|
+
error.eliscriptDiagnostic = {
|
|
18
|
+
format: "eliscript-diagnostic",
|
|
19
|
+
version: 1,
|
|
20
|
+
code: "ELI-W0001",
|
|
21
|
+
severity: "error",
|
|
22
|
+
phase: "project-watch",
|
|
23
|
+
message,
|
|
24
|
+
...(filename ? { location: { file: filename } } : {}),
|
|
25
|
+
};
|
|
26
|
+
return error;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function containedPath(file, root) {
|
|
30
|
+
const fromRoot = relative(root, file);
|
|
31
|
+
return fromRoot === "" ||
|
|
32
|
+
(fromRoot !== ".." && !fromRoot.startsWith(`..${sep}`) &&
|
|
33
|
+
!isAbsolute(fromRoot));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function digestBytes(value) {
|
|
37
|
+
return createHash("sha256").update(value).digest("hex");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function snapshotDigest(files) {
|
|
41
|
+
const hash = createHash("sha256");
|
|
42
|
+
for (const [file, digest] of [...files].sort(([left], [right]) =>
|
|
43
|
+
left.localeCompare(right))) {
|
|
44
|
+
hash.update(file);
|
|
45
|
+
hash.update("\0");
|
|
46
|
+
hash.update(digest);
|
|
47
|
+
hash.update("\0");
|
|
48
|
+
}
|
|
49
|
+
return hash.digest("hex");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function canonicalDirectory(directory) {
|
|
53
|
+
const expanded = resolve(directory);
|
|
54
|
+
try {
|
|
55
|
+
if (!(await stat(expanded)).isDirectory()) {
|
|
56
|
+
throw watchError("watch root is not a directory", expanded);
|
|
57
|
+
}
|
|
58
|
+
return await realpath(expanded);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error?.eliscriptDiagnostic) throw error;
|
|
61
|
+
throw watchError("watch root does not exist", expanded);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function canonicalConfiguration(filename) {
|
|
66
|
+
if (filename === null || filename === undefined) return null;
|
|
67
|
+
const expanded = resolve(filename);
|
|
68
|
+
try {
|
|
69
|
+
if (!(await stat(expanded)).isFile()) {
|
|
70
|
+
throw watchError("watch configuration is not a file", expanded);
|
|
71
|
+
}
|
|
72
|
+
return await realpath(expanded);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (error?.eliscriptDiagnostic) throw error;
|
|
75
|
+
throw watchError("watch configuration does not exist", expanded);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function watchInterval(value) {
|
|
80
|
+
const interval = value ?? defaultWatchInterval;
|
|
81
|
+
if (!Number.isSafeInteger(interval) || interval < minimumWatchInterval ||
|
|
82
|
+
interval > maximumWatchInterval) {
|
|
83
|
+
throw watchError(
|
|
84
|
+
`watch interval must be an integer between ${minimumWatchInterval} and ` +
|
|
85
|
+
`${maximumWatchInterval} milliseconds`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return interval;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function readDigest(filename) {
|
|
92
|
+
try {
|
|
93
|
+
return digestBytes(await readFile(filename));
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error?.code === "ENOENT" || error?.code === "ENOTDIR") return null;
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function collectSources(directory, files) {
|
|
101
|
+
let entries;
|
|
102
|
+
try {
|
|
103
|
+
entries = await readdir(directory, { withFileTypes: true });
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (error?.code === "ENOENT" || error?.code === "ENOTDIR") return;
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
entries.sort((left, right) => left.name.localeCompare(right.name));
|
|
109
|
+
for (const entry of entries) {
|
|
110
|
+
if (entry.isSymbolicLink()) continue;
|
|
111
|
+
const filename = join(directory, entry.name);
|
|
112
|
+
if (entry.isDirectory()) {
|
|
113
|
+
await collectSources(filename, files);
|
|
114
|
+
} else if (entry.isFile() && entry.name.endsWith(".eli")) {
|
|
115
|
+
const digest = await readDigest(filename);
|
|
116
|
+
if (digest !== null) files.set(filename, digest);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function projectSnapshot(root, configuration) {
|
|
122
|
+
const files = new Map();
|
|
123
|
+
await collectSources(root, files);
|
|
124
|
+
if (configuration !== null && !files.has(configuration)) {
|
|
125
|
+
const digest = await readDigest(configuration);
|
|
126
|
+
if (digest !== null) files.set(configuration, digest);
|
|
127
|
+
}
|
|
128
|
+
return freeze({ files, digest: snapshotDigest(files) });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function changesBetween(previous, current, root) {
|
|
132
|
+
const files = new Set([...previous.files.keys(), ...current.files.keys()]);
|
|
133
|
+
return [...files].sort().flatMap((file) => {
|
|
134
|
+
const before = previous.files.get(file);
|
|
135
|
+
const after = current.files.get(file);
|
|
136
|
+
if (before === after) return [];
|
|
137
|
+
const kind = before === undefined
|
|
138
|
+
? "create"
|
|
139
|
+
: after === undefined ? "delete" : "modify";
|
|
140
|
+
return [freeze({
|
|
141
|
+
file,
|
|
142
|
+
...(containedPath(file, root) ? { path: relative(root, file) } : {}),
|
|
143
|
+
kind,
|
|
144
|
+
})];
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function eventRecord(sequence, event, root, fields = {}) {
|
|
149
|
+
return freeze({
|
|
150
|
+
format: watchEventFormat,
|
|
151
|
+
version: watchEventVersion,
|
|
152
|
+
sequence,
|
|
153
|
+
event,
|
|
154
|
+
root,
|
|
155
|
+
...fields,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export class ProjectWatcher {
|
|
160
|
+
static async create(options = {}) {
|
|
161
|
+
if (!options || typeof options !== "object") {
|
|
162
|
+
throw watchError("watch options must be an object");
|
|
163
|
+
}
|
|
164
|
+
if (typeof options.root !== "string" || options.root.length === 0) {
|
|
165
|
+
throw watchError("watch root must be a non-empty string");
|
|
166
|
+
}
|
|
167
|
+
const root = await canonicalDirectory(options.root);
|
|
168
|
+
const configuration = await canonicalConfiguration(options.configuration);
|
|
169
|
+
const interval = watchInterval(options.interval);
|
|
170
|
+
const snapshot = await projectSnapshot(root, configuration);
|
|
171
|
+
return new ProjectWatcher(root, configuration, interval, snapshot);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
constructor(root, configuration, interval, snapshot) {
|
|
175
|
+
this.root = root;
|
|
176
|
+
this.configuration = configuration;
|
|
177
|
+
this.interval = interval;
|
|
178
|
+
this.snapshot = snapshot;
|
|
179
|
+
this.sequence = 0;
|
|
180
|
+
this.timer = null;
|
|
181
|
+
this.scanning = false;
|
|
182
|
+
this.scanPromise = null;
|
|
183
|
+
this.closed = false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
ready() {
|
|
187
|
+
return eventRecord(0, "ready", this.root, {
|
|
188
|
+
changes: freeze([]),
|
|
189
|
+
digest: this.snapshot.digest,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async scan() {
|
|
194
|
+
if (this.closed) throw watchError("watch session is closed", this.root);
|
|
195
|
+
const next = await projectSnapshot(this.root, this.configuration);
|
|
196
|
+
const changes = changesBetween(this.snapshot, next, this.root);
|
|
197
|
+
this.snapshot = next;
|
|
198
|
+
if (changes.length === 0) return null;
|
|
199
|
+
this.sequence += 1;
|
|
200
|
+
return eventRecord(this.sequence, "change", this.root, {
|
|
201
|
+
changes: freeze(changes),
|
|
202
|
+
digest: next.digest,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
error(error) {
|
|
207
|
+
this.sequence += 1;
|
|
208
|
+
const diagnostic = error?.eliscriptDiagnostic ?? {
|
|
209
|
+
format: "eliscript-diagnostic",
|
|
210
|
+
version: 1,
|
|
211
|
+
code: "ELI-W0002",
|
|
212
|
+
severity: "error",
|
|
213
|
+
phase: "project-watch",
|
|
214
|
+
message: error instanceof Error ? error.message : String(error),
|
|
215
|
+
location: { file: this.root },
|
|
216
|
+
};
|
|
217
|
+
return eventRecord(this.sequence, "error", this.root, {
|
|
218
|
+
diagnostic: freeze(diagnostic),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
start(onEvent) {
|
|
223
|
+
if (typeof onEvent !== "function") {
|
|
224
|
+
throw watchError("watch event handler must be a function");
|
|
225
|
+
}
|
|
226
|
+
if (this.closed) throw watchError("watch session is closed", this.root);
|
|
227
|
+
if (this.timer !== null) return this;
|
|
228
|
+
this.timer = setInterval(async () => {
|
|
229
|
+
if (this.scanning || this.closed) return;
|
|
230
|
+
this.scanning = true;
|
|
231
|
+
this.scanPromise = this.scan();
|
|
232
|
+
try {
|
|
233
|
+
const event = await this.scanPromise;
|
|
234
|
+
if (!this.closed && event !== null) onEvent(event);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
if (!this.closed) onEvent(this.error(error));
|
|
237
|
+
} finally {
|
|
238
|
+
this.scanning = false;
|
|
239
|
+
this.scanPromise = null;
|
|
240
|
+
}
|
|
241
|
+
}, this.interval);
|
|
242
|
+
onEvent(this.ready());
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async close() {
|
|
247
|
+
if (this.timer !== null) clearInterval(this.timer);
|
|
248
|
+
this.timer = null;
|
|
249
|
+
this.closed = true;
|
|
250
|
+
if (this.scanPromise !== null) {
|
|
251
|
+
try {
|
|
252
|
+
await this.scanPromise;
|
|
253
|
+
} catch {
|
|
254
|
+
// A closing watcher suppresses the final recoverable scan failure.
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export async function snapshotProject(options) {
|
|
261
|
+
const watcher = await ProjectWatcher.create(options);
|
|
262
|
+
try {
|
|
263
|
+
return watcher.ready();
|
|
264
|
+
} finally {
|
|
265
|
+
await watcher.close();
|
|
266
|
+
}
|
|
267
|
+
}
|