@dogsbay/commands 0.2.0-beta.175 → 0.2.0-beta.177
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 +53 -1
- package/dist/commands/content.d.ts +2 -7
- package/dist/commands/content.js +2 -8
- package/dist/config-variables.d.ts +12 -0
- package/dist/config-variables.js +78 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -2
- package/dist/indexes-entry.d.ts +15 -0
- package/dist/indexes-entry.js +15 -0
- package/dist/indexes.d.ts +33 -0
- package/dist/indexes.js +158 -27
- package/dist/node-storage.d.ts +42 -0
- package/dist/node-storage.js +154 -0
- package/dist/sha256.d.ts +6 -0
- package/dist/sha256.js +6 -0
- package/dist/storage.d.ts +2 -38
- package/dist/storage.js +2 -150
- package/dist/variables.d.ts +117 -0
- package/dist/variables.js +509 -0
- package/package.json +12 -6
package/dist/storage.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Injectable workspace storage — the only I/O boundary in the engine.
|
|
3
3
|
*
|
|
4
4
|
* Commands never touch node:fs: they see a `Storage` bound to a
|
|
5
|
-
* workspace root. Implementations: `NodeStorage` (real filesystem
|
|
6
|
-
* `MemoryStorage` (tests, browser). An Electron IPC-backed
|
|
5
|
+
* workspace root. Implementations: `NodeStorage` (real filesystem,
|
|
6
|
+
* in node-storage.ts so this module stays free of Node), `MemoryStorage` (tests, browser). An Electron IPC-backed
|
|
7
7
|
* implementation slots in the same way.
|
|
8
8
|
*
|
|
9
9
|
* All paths are workspace-root-relative posix paths ("a/b.md") — no
|
|
@@ -56,151 +56,3 @@ export class MemoryStorage {
|
|
|
56
56
|
return this.files.has(normalizeStoragePath(path));
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
-
/** Directories never part of a content workspace. */
|
|
60
|
-
const IGNORED_DIRS = new Set([
|
|
61
|
-
"node_modules", ".git", ".dogsbay", "dist", ".astro", ".turbo",
|
|
62
|
-
]);
|
|
63
|
-
export class NodeStorage {
|
|
64
|
-
root;
|
|
65
|
-
containSymlinks;
|
|
66
|
-
rootReal;
|
|
67
|
-
constructor(root, options = {}) {
|
|
68
|
-
this.root = root;
|
|
69
|
-
this.containSymlinks = options.containSymlinks ?? false;
|
|
70
|
-
}
|
|
71
|
-
async fs() {
|
|
72
|
-
return import("node:fs/promises");
|
|
73
|
-
}
|
|
74
|
-
abs(path) {
|
|
75
|
-
const rel = normalizeStoragePath(path);
|
|
76
|
-
if (rel.startsWith("..")) {
|
|
77
|
-
throw new Error(`Path escapes the workspace root: ${path}`);
|
|
78
|
-
}
|
|
79
|
-
return `${this.root}/${rel}`;
|
|
80
|
-
}
|
|
81
|
-
/** `abs()` plus, when contained, a realpath check that no symlink escapes the root. */
|
|
82
|
-
async safeAbs(path) {
|
|
83
|
-
const abs = this.abs(path);
|
|
84
|
-
if (!this.containSymlinks)
|
|
85
|
-
return abs;
|
|
86
|
-
if (!(await this.isInsideRoot(abs))) {
|
|
87
|
-
throw new Error(`Path resolves outside the workspace root: ${path}`);
|
|
88
|
-
}
|
|
89
|
-
return abs;
|
|
90
|
-
}
|
|
91
|
-
async isInsideRoot(abs) {
|
|
92
|
-
const fs = await this.fs();
|
|
93
|
-
this.rootReal ??= fs.realpath(this.root).catch(() => this.root);
|
|
94
|
-
const rootReal = await this.rootReal;
|
|
95
|
-
// realpath the deepest existing ancestor (the target may not exist yet on write)
|
|
96
|
-
let probe = abs;
|
|
97
|
-
let suffix = "";
|
|
98
|
-
for (;;) {
|
|
99
|
-
try {
|
|
100
|
-
const real = await fs.realpath(probe);
|
|
101
|
-
const full = real + suffix;
|
|
102
|
-
return full === rootReal || full.startsWith(rootReal + "/");
|
|
103
|
-
}
|
|
104
|
-
catch {
|
|
105
|
-
const cut = probe.lastIndexOf("/");
|
|
106
|
-
if (cut <= 0)
|
|
107
|
-
return false;
|
|
108
|
-
suffix = probe.slice(cut) + suffix;
|
|
109
|
-
probe = probe.slice(0, cut);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
async list() {
|
|
114
|
-
const fs = await this.fs();
|
|
115
|
-
const out = [];
|
|
116
|
-
// Symlinked directories are walked (pnpm/monorepo layouts symlink
|
|
117
|
-
// content in); the realpath set breaks symlink cycles
|
|
118
|
-
const visited = new Set([await fs.realpath(this.root).catch(() => this.root)]);
|
|
119
|
-
const walk = async (dir, prefix) => {
|
|
120
|
-
let entries;
|
|
121
|
-
try {
|
|
122
|
-
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
123
|
-
}
|
|
124
|
-
catch {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
for (const entry of entries) {
|
|
128
|
-
const full = `${dir}/${entry.name}`;
|
|
129
|
-
let isDirectory = entry.isDirectory();
|
|
130
|
-
let isFile = entry.isFile();
|
|
131
|
-
if (entry.isSymbolicLink()) {
|
|
132
|
-
if (this.containSymlinks && !(await this.isInsideRoot(full)))
|
|
133
|
-
continue; // escapes the root
|
|
134
|
-
try {
|
|
135
|
-
const stat = await fs.stat(full); // follows the link
|
|
136
|
-
isDirectory = stat.isDirectory();
|
|
137
|
-
isFile = stat.isFile();
|
|
138
|
-
}
|
|
139
|
-
catch {
|
|
140
|
-
continue; // dangling symlink
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
if (isDirectory) {
|
|
144
|
-
if (IGNORED_DIRS.has(entry.name))
|
|
145
|
-
continue;
|
|
146
|
-
if (entry.isSymbolicLink()) {
|
|
147
|
-
try {
|
|
148
|
-
const real = await fs.realpath(full);
|
|
149
|
-
if (visited.has(real))
|
|
150
|
-
continue;
|
|
151
|
-
visited.add(real);
|
|
152
|
-
}
|
|
153
|
-
catch {
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
await walk(full, `${prefix}${entry.name}/`);
|
|
158
|
-
}
|
|
159
|
-
else if (isFile) {
|
|
160
|
-
out.push(`${prefix}${entry.name}`);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
await walk(this.root, "");
|
|
165
|
-
return out.sort();
|
|
166
|
-
}
|
|
167
|
-
async read(path) {
|
|
168
|
-
const fs = await this.fs();
|
|
169
|
-
return fs.readFile(await this.safeAbs(path), "utf-8");
|
|
170
|
-
}
|
|
171
|
-
async readBytes(path, options = {}) {
|
|
172
|
-
const fs = await this.fs();
|
|
173
|
-
// Contained whatever containSymlinks says: this reads binaries for a page that leaves
|
|
174
|
-
// the workspace, so a symlink out (docs/k.png -> ~/.ssh/key) must not be followed.
|
|
175
|
-
const abs = this.abs(path);
|
|
176
|
-
if (!(await this.isInsideRoot(abs)))
|
|
177
|
-
throw new Error(`Path resolves outside the workspace root: ${path}`);
|
|
178
|
-
const st = await fs.stat(abs);
|
|
179
|
-
if (!st.isFile())
|
|
180
|
-
throw new Error(`Not a file: ${path}`);
|
|
181
|
-
if (options.maxBytes !== undefined && st.size > options.maxBytes)
|
|
182
|
-
throw new Error(`File is over ${options.maxBytes} bytes: ${path}`);
|
|
183
|
-
return new Uint8Array(await fs.readFile(abs));
|
|
184
|
-
}
|
|
185
|
-
async write(path, content) {
|
|
186
|
-
const fs = await this.fs();
|
|
187
|
-
const abs = await this.safeAbs(path);
|
|
188
|
-
const dir = abs.slice(0, abs.lastIndexOf("/"));
|
|
189
|
-
await fs.mkdir(dir, { recursive: true });
|
|
190
|
-
await fs.writeFile(abs, content, "utf-8");
|
|
191
|
-
}
|
|
192
|
-
async delete(path) {
|
|
193
|
-
const fs = await this.fs();
|
|
194
|
-
await fs.unlink(await this.safeAbs(path));
|
|
195
|
-
}
|
|
196
|
-
async exists(path) {
|
|
197
|
-
const fs = await this.fs();
|
|
198
|
-
try {
|
|
199
|
-
await fs.access(await this.safeAbs(path));
|
|
200
|
-
return true;
|
|
201
|
-
}
|
|
202
|
-
catch {
|
|
203
|
-
return false;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
export interface SymbolRange {
|
|
2
|
+
file: string;
|
|
3
|
+
/** 1-based line, 0-based columns, end exclusive (as `DocTarget`). */
|
|
4
|
+
line: number;
|
|
5
|
+
from: number;
|
|
6
|
+
to: number;
|
|
7
|
+
}
|
|
8
|
+
/** A branch the definition or use sits under, outermost first, in its own file. */
|
|
9
|
+
export interface SymbolCondition {
|
|
10
|
+
branch: "if" | "elif" | "else" | "case";
|
|
11
|
+
/** The condition as written (a case: `subject == value`); none for `else`. */
|
|
12
|
+
test?: string;
|
|
13
|
+
/** The earlier branches' tests of the same block, which must all be false (elif, else, case). */
|
|
14
|
+
after?: string[];
|
|
15
|
+
/** 1-based line of the branch's tag. */
|
|
16
|
+
line: number;
|
|
17
|
+
}
|
|
18
|
+
export interface SymbolContext {
|
|
19
|
+
/** `site`: the config's top-level attributes; `source`: one source's; `file`: a page or include. */
|
|
20
|
+
scope: "site" | "source" | "file";
|
|
21
|
+
/** The variant a source's attribute belongs to (`Variant.id`). */
|
|
22
|
+
source?: string;
|
|
23
|
+
}
|
|
24
|
+
export type DefinitionVia = "config" | "set" | "with" | "param";
|
|
25
|
+
export interface SymbolDefinition extends SymbolRange {
|
|
26
|
+
kind: "variable";
|
|
27
|
+
/** As written (`product-title`); the entry's name is the lookup form (`product_title`). */
|
|
28
|
+
name: string;
|
|
29
|
+
via: DefinitionVia;
|
|
30
|
+
/** The value as written: the YAML scalar, the set's or with's expression. None for a param or a block set. */
|
|
31
|
+
value?: string;
|
|
32
|
+
conditions: SymbolCondition[];
|
|
33
|
+
context: SymbolContext;
|
|
34
|
+
/**
|
|
35
|
+
* Whether it applies in each variant (`Variant.id`): its conditions evaluated against that
|
|
36
|
+
* variant's config attributes. `null`: a condition reads a name the config does not give but a
|
|
37
|
+
* `{% set %}` does, so the files alone cannot tell. Absent when the workspace has no variants.
|
|
38
|
+
*/
|
|
39
|
+
variants?: Record<string, boolean | null>;
|
|
40
|
+
/** Set inside a `{% for %}`: the value lasts for the loop body only. */
|
|
41
|
+
inLoop?: true;
|
|
42
|
+
}
|
|
43
|
+
export type UseForm = "output" | "condition" | "expression";
|
|
44
|
+
export interface SymbolUse extends SymbolRange {
|
|
45
|
+
kind: "variable";
|
|
46
|
+
/** As written, with any `.field` path (`page.title`). */
|
|
47
|
+
name: string;
|
|
48
|
+
/** `output`: printed by `{{ }}`; `condition`: an if/elif/case test; `expression`: anything else (a set's value…). */
|
|
49
|
+
form: UseForm;
|
|
50
|
+
/** Filters applied in the same `{{ }}` (`upper`, `default`). */
|
|
51
|
+
filters?: string[];
|
|
52
|
+
/** The whole tag is `{{ name }}`: a value only, no filter or expression. */
|
|
53
|
+
plain: boolean;
|
|
54
|
+
conditions: SymbolCondition[];
|
|
55
|
+
}
|
|
56
|
+
export interface SymbolEntry {
|
|
57
|
+
kind: "variable";
|
|
58
|
+
/** The lookup form: hyphens as underscores, as the build reads them. */
|
|
59
|
+
name: string;
|
|
60
|
+
definitions: SymbolDefinition[];
|
|
61
|
+
uses: SymbolUse[];
|
|
62
|
+
}
|
|
63
|
+
/** One build of the content: a `content.sources` entry, with the site's attributes under its own. */
|
|
64
|
+
export interface Variant {
|
|
65
|
+
/** The source's `namespace`, else `source-<i>`; `default` when the config lists no sources. */
|
|
66
|
+
id: string;
|
|
67
|
+
namespace?: string;
|
|
68
|
+
/** The source's folder, workspace-relative (`""`: the root). */
|
|
69
|
+
path: string;
|
|
70
|
+
attributes: Record<string, unknown>;
|
|
71
|
+
}
|
|
72
|
+
export interface FileVariables {
|
|
73
|
+
definitions: SymbolDefinition[];
|
|
74
|
+
uses: SymbolUse[];
|
|
75
|
+
}
|
|
76
|
+
/** The lookup form of a name: its root, hyphens as underscores (the build's `hyphenToUnderscore`). */
|
|
77
|
+
export declare const variableKey: (name: string) => string;
|
|
78
|
+
interface Ident {
|
|
79
|
+
name: string;
|
|
80
|
+
start: number;
|
|
81
|
+
end: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The root names an expression reads, with their offsets in `expr`. Not a name after `.` (a
|
|
85
|
+
* field), `|` (a filter), `is` (a test), before `(` (a function) or a keyword argument's `=`, nor
|
|
86
|
+
* inside a string. `hyphens`: content mode, where `product-title` is one name.
|
|
87
|
+
*/
|
|
88
|
+
export declare const expressionNames: (expr: string, hyphens: boolean) => {
|
|
89
|
+
names: Ident[];
|
|
90
|
+
filters: string[];
|
|
91
|
+
};
|
|
92
|
+
export interface ScanOptions {
|
|
93
|
+
/**
|
|
94
|
+
* A template page (`template: jinja`): Jinja's grammar. Otherwise content mode, as the build
|
|
95
|
+
* reads an ordinary page: a hyphen belongs to a name, `{{ }}` holds a name only, no `for` /
|
|
96
|
+
* `with` / comments (the build masks every `{#`), and the front matter is rendered too.
|
|
97
|
+
*/
|
|
98
|
+
template: boolean;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The definitions and uses in one page or include. `masked` is `source` with code blanked to
|
|
102
|
+
* spaces (`blankCodeRegions`: same length, `{minja}` fences kept); `bodyStart` is where the front
|
|
103
|
+
* matter ends.
|
|
104
|
+
*/
|
|
105
|
+
export declare function scanFileVariables(file: string, source: string, masked: string, bodyStart: number, options: ScanOptions): FileVariables;
|
|
106
|
+
/**
|
|
107
|
+
* A partial's declared `params:` (`params: [platform, cli?]` or a block list), as definitions in its
|
|
108
|
+
* front matter. `frontmatter` is the block's text, starting at offset 0 of the file.
|
|
109
|
+
*/
|
|
110
|
+
export declare function scanParams(file: string, source: string, frontmatterEnd: number): SymbolDefinition[];
|
|
111
|
+
/**
|
|
112
|
+
* Does a definition apply in a variant: its conditions against the variant's attributes. A name the
|
|
113
|
+
* attributes lack is undefined (falsy, as the build reads it) unless `setNames` has it, which makes
|
|
114
|
+
* the answer `null` (a `{% set %}` elsewhere may give it).
|
|
115
|
+
*/
|
|
116
|
+
export declare function appliesIn(conditions: SymbolCondition[], attributes: Record<string, unknown>, setNames: Set<string>): boolean | null;
|
|
117
|
+
export {};
|