Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 8x 8x 8x 8x 8x 9x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 13x 13x 13x 13x 13x 13x 9x 9x 13x 13x 5x 5x 5x 8x 8x 8x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 4x 4x 1x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 6x 6x 7x 7x 7x 7x 7x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 78x 25x 25x 25x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 48x 48x 48x 48x 48x 48x 45x 45x 3x 3x 3x 48x 2x 2x 2x 36x 10x 10x 10x 8x 8x 48x 48x 48x 48x 48x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 15x 15x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 8x 8x 1x 1x 1x 1x 1x 1x 6x 3x 3x | /**
* Text-level merge helpers.
*
* Generators that write *shared* files — the platform bootstrap that lists test
* groups, the schema registry that orders tables, the Playwright spec that holds
* one `describe` per entity — must extend what is already there rather than
* replace it, or adding an entity silently drops the one added before it.
*
* These helpers are deliberately narrow: each one recognises a single, stable
* shape that a kukum template emits, and returns `undefined` when it does not
* find that shape so the caller can fall back to a plain overwrite and warn
* instead of corrupting the file. They are pure string transforms, so they are
* cheap to unit test and carry no parser dependency.
*/
import type {PrimitiveHost} from './engine.ts';
export const escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const unquote = (value: string): string => value.trim().replace(/^['"`]|['"`]$/g, '');
/**
* Append a string to an array literal, e.g.
*
* watch: {test: ['test.widget']} → watch: {test: ['test.widget', 'test.other']}
*
* Used to register a generated test group in the platform's
* `integration.watch.test` list. Idempotent: an existing entry is left as-is.
*/
export function appendStringEntry(
source: string,
property: string,
value: string,
): string | undefined {
const pattern = new RegExp(`(${escapeRegExp(property)}\\s*:\\s*\\[)([\\s\\S]*?)(\\])`);
const match = pattern.exec(source);
if (!match) return undefined;
const [whole, open, body, close] = match;
const entries = body
.split(',')
.map(entry => entry.trim())
.filter(Boolean);
if (entries.map(unquote).includes(value)) return source;
const next = [...entries, `'${value}'`].join(', ');
return `${source.slice(0, match.index)}${open}${next}${close}${source.slice(match.index + whole.length)}`;
}
/**
* Add a folder to a realm `browser.ts` children declaration.
*
* A realm lists the `./meta` folders it loads **twice**: once as
* `import.meta.glob([...])` patterns for the browser bundle, and once as plain
* directories for the non-window branch of the same ternary (the server platform
* discovers layer folders itself, but the browser one loads nothing it is not
* told about). A folder that has to be there in both is added to both lists:
*
* import.meta.glob(['./meta/model', './browser'])
* → import.meta.glob(['./meta/model', './browser', './meta/fixture'])
* : ['./meta/model', './browser']
* → : ['./meta/model', './browser', './meta/fixture']
*
* A glob list (one holding a `*` pattern) gains a pattern, a directory list the
* folder itself. Arrays are found by their entries rather than by the property in
* front of them, because the declarations differ per realm — a literal
* `import.meta.glob(` has no property, and the else branch of the ternary has no
* name of its own. What every such list shares is a `./meta` entry, so that is
* the anchor; an array of something else is left untouched.
*
* The existing layout is preserved (a multi-line list stays multi-line, one
* entry per line), so a spliced file is what prettier would have written.
* Idempotent, and `undefined` when no `./meta` list is found — the caller warns
* and points at the array by hand instead of rewriting a file it does not
* understand.
*/
export function appendChildFolder(source: string, folder: string): string | undefined {
// The folder's top-level namespace (`./meta` for `./meta/fixture`): the part
// every children list has in common, whatever it lists beside it.
const anchor = folder.split('/').slice(0, 2).join('/');
let found = false;
let changed = false;
const next = source.replace(/\[([^[\]]*)\]/g, whole => {
const body = whole.slice(1, -1);
const entries = body
.split(',')
.map(entry => entry.trim())
.filter(Boolean);
if (!entries.some(entry => unquote(entry).startsWith(anchor))) return whole;
found = true;
// A glob list takes the pattern, a directory list the folder itself.
const entry = entries.some(value => value.includes('*')) ? `${folder}/**/*.ts` : folder;
if (entries.map(unquote).includes(entry)) return whole;
changed = true;
return `[${withEntry(body, entry)}]`;
});
if (!found) return undefined;
return changed ? next : source;
}
/**
* Insert `'entry'` after the last element of an array literal body, keeping the
* body's own layout: the indentation of the last entry and the whitespace before
* the closing bracket. A single-line body gains `, 'entry'`.
*/
function withEntry(body: string, entry: string): string {
const trimmed = body.replace(/\s+$/, '');
const trailing = body.slice(trimmed.length);
if (!trimmed.includes('\n')) {
return `${trimmed}${trimmed.endsWith(',') ? ' ' : ', '}'${entry}'`;
}
const lastLine = trimmed.slice(trimmed.lastIndexOf('\n') + 1);
const indent = /^\s*/.exec(lastLine)?.[0] ?? '';
const comma = trimmed.endsWith(',') ? '' : ',';
return `${trimmed}${comma}\n${indent}'${entry}',${trailing}`;
}
/**
* Insert an entry into an object literal, e.g.
*
* tables: {'shop.order': 1} → tables: {'shop.order': 1, 'shop.entry': 2}
*
* Used to register a table in `meta/db/db.ts` without dropping the tables other
* entities already registered. Idempotent.
*/
export function insertMapEntry(
source: string,
property: string,
key: string,
value: string | number,
): string | undefined {
const pattern = new RegExp(`(${escapeRegExp(property)}\\s*:\\s*\\{)([\\s\\S]*?)(\\})`);
const match = pattern.exec(source);
if (!match) return undefined;
const [whole, open, body, close] = match;
const entries = body
.split(',')
.map(entry => entry.trim())
.filter(Boolean);
const keys = entries.map(entry => unquote(entry.split(':')[0] ?? ''));
if (keys.includes(key)) return source;
const rendered = typeof value === 'number' ? String(value) : `'${value}'`;
const next = [...entries, `'${key}': ${rendered}`].join(', ');
return `${source.slice(0, match.index)}${open}${next}${close}${source.slice(match.index + whole.length)}`;
}
/** True when `source` already declares a top-level YAML key. */
export function hasYamlKey(source: string, key: string): boolean {
return new RegExp(`^${escapeRegExp(key)}\\s*:`, 'm').test(source);
}
/**
* Append comma-separated values to a YAML scalar, e.g.
*
* e2eManage: e2eWidgetAdd,e2eWidgetFind
* → e2eManage: e2eWidgetAdd,e2eWidgetFind,e2eGadgetAdd,e2eGadgetFind
*
* Used to grant a newly added entity's capabilities to a role. Also matches
* indented keys. Idempotent.
*/
export function appendCommaListValue(
source: string,
key: string,
values: string[],
): string | undefined {
const pattern = new RegExp(`^(\\s*${escapeRegExp(key)}\\s*:\\s*)(\\S.*)$`, 'm');
const match = pattern.exec(source);
if (!match) return undefined;
const [whole, prefix, body] = match;
const existing = body
.split(',')
.map(value => value.trim())
.filter(Boolean);
const missing = values.filter(value => !existing.includes(value));
if (!missing.length) return source;
const next = [...existing, ...missing].join(',');
return `${source.slice(0, match.index)}${prefix}${next}${source.slice(match.index + whole.length)}`;
}
/**
* Declare a comma-separated YAML scalar, creating it inside `section` when the
* document does not have it yet.
*
* A realm's RBAC seed grows in two directions: a new entity adds actions to the
* capabilities the seed already declares (`appendCommaListValue`), and a new
* capability or realm-scoped role has to be *declared* first — a realm scaffolded
* before the realm roles existed has no `Party Admin:` line to append to. The
* entry is written after the last line of its section, with the indentation its
* siblings use, so a composed file reads as if it had been written by hand.
*
* Returns `undefined` only when the document has no such section and no `role:`
* style block can be appended at all — the caller then warns instead of guessing.
*/
export function ensureCommaListValue(
source: string,
section: string,
key: string,
values: string[],
): string | undefined {
if (new RegExp(`^\\s*${escapeRegExp(key)}\\s*:`, 'm').test(source)) {
return appendCommaListValue(source, key, values);
}
const lines = source.split('\n');
const header = new RegExp(`^${escapeRegExp(section)}\\s*:\\s*$`);
const headerIndex = lines.findIndex(line => header.test(line));
if (headerIndex === -1) return undefined;
// The section runs to the next top-level key (a non-indented, non-comment
// line); its last entry is where a new sibling belongs.
let last = headerIndex;
for (let index = headerIndex + 1; index < lines.length; index++) {
const line = lines[index];
if (line.trim() === '' || line.trimStart().startsWith('#')) continue;
if (!/^\s/.test(line)) break;
last = index;
}
const indent = /^(\s*)/.exec(lines[last])?.[1] || ' ';
const entry = `${indent}${key}: ${values.join(',')}`;
lines.splice(last + 1, 0, entry);
return lines.join('\n');
}
/**
* Read a shared template artifact that is not TypeScript, so it never carries
* the generated marker (YAML seeds, for example).
*/
export function readPlainSource(
host: PrimitiveHost,
root: string,
path: string,
): string | undefined {
const absolute = host.join(root, path);
if (!host.existsSync(absolute)) return undefined;
return String(host.readFileSync(absolute, {encoding: 'utf-8'}));
}
/**
* Append a top-level block to a YAML document, keeping the existing content (and
* its comments) untouched. Idempotent: a document that already declares `key` is
* returned unchanged.
*/
export function appendYamlBlock(source: string, key: string, block: string): string {
if (hasYamlKey(source, key)) return source;
return `${source.replace(/\s*$/, '')}\n${block.replace(/\s*$/, '')}\n`;
}
/** True when a Playwright spec already contains `test.describe('title'`. */
export function hasDescribeBlock(source: string, title: string): boolean {
return new RegExp(`test\\.describe\\(\\s*['"\`]${escapeRegExp(title)}['"\`]`).test(source);
}
/**
* Append a Playwright `test.describe` block to a spec, before the trailing
* newline. Idempotent: an existing block with the same title is left alone.
*/
export function appendDescribeBlock(source: string, title: string, block: string): string {
if (hasDescribeBlock(source, title)) return source;
return `${source.replace(/\s*$/, '')}\n\n${block.replace(/\s*$/, '')}\n`;
}
|