@0001-labs/idv1 3.0.0 → 3.1.0
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 +3 -2
- package/bin/idv1.mjs +13 -2
- package/package.json +1 -1
- package/src/idv1.ts +28 -1
- package/src/util/explorer.css +1 -0
- package/src/util/explorer.ts +8 -8
package/README.md
CHANGED
|
@@ -48,15 +48,16 @@ NNsvLL Service
|
|
|
48
48
|
npx idv1 directory # the vocabulary, and whether it is healthy
|
|
49
49
|
npx idv1 health # exits 1 with each problem
|
|
50
50
|
npx idv1 resolve co12Ca # the shape, taxonomy and namespace a code reads as
|
|
51
|
+
npx idv1 hardcoded src # exits 1 with each quoted -NS-code written into source
|
|
51
52
|
```
|
|
52
53
|
|
|
53
|
-
The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory` and `
|
|
54
|
+
The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory`, `runDirectoryCommand` and `hardcodedCodes`. `recordHealth(directory, records, released)` checks an app's coded records against its `.id`: each code parses, fits its taxonomy pattern, sits in a declared namespace and is unique, and a record's code matches the one it was released with. Run both before every release, so codes are read from records and never typed into code.
|
|
54
55
|
|
|
55
56
|
The package ships TypeScript source. Bundlers and `tsx` read it directly. Plain Node 22.18 or later imports `@0001-labs/idv1/register` first, then dynamically imports the package.
|
|
56
57
|
|
|
57
58
|
## Explorer utility
|
|
58
59
|
|
|
59
|
-
Import `createExplorer` and `mountExplorer` from `@0001-labs/idv1/explorer`, and `@0001-labs/idv1/explorer.css` for the browser view. Supply `ExplorerData`: namespace names, shapes, taxonomies, identifiers, nodes and links. The utility owns drill-down, counts, identifier resolution, taxonomy highlighting, keyboard navigation and URL history.
|
|
60
|
+
Import `createExplorer` and `mountExplorer` from `@0001-labs/idv1/explorer`, and `@0001-labs/idv1/explorer.css` for the browser view. Supply `ExplorerData`: namespace names, shapes, taxonomies, identifiers, nodes and links. The root lists the shapes by their plural names, then namespaces, taxonomies and identifiers. The utility owns drill-down, counts, identifier resolution, taxonomy highlighting, keyboard navigation and URL history.
|
|
60
61
|
|
|
61
62
|
Optional `collections` carry source records and lazy `load` functions. Records may link to host details with `href`. The utility contains no application records or network requests. `createExplorer(data)` returns `entries(path)`, `title(path)`, `stepName(path, depth)` and `load(path)`. `mountExplorer(element, data)` renders it and returns a cleanup function. Theme it with `--idv1-explorer-*` CSS variables; the host owns loading and authorization.
|
|
62
63
|
|
package/bin/idv1.mjs
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
|
|
3
3
|
import { dirname, join, resolve } from 'node:path'
|
|
4
4
|
import '../register.mjs'
|
|
5
|
-
const { runDirectoryCommand } = await import('../src/idv1.ts')
|
|
5
|
+
const { runDirectoryCommand, hardcodedCodes } = await import('../src/idv1.ts')
|
|
6
6
|
|
|
7
7
|
const [command, ...args] = process.argv.slice(2)
|
|
8
|
+
if (command === 'hardcoded') {
|
|
9
|
+
const files = {}
|
|
10
|
+
const walk = path => {
|
|
11
|
+
if (statSync(path).isDirectory()) { for (const name of readdirSync(path)) if (!name.startsWith('.') && !['node_modules', 'dist', 'build'].includes(name)) walk(join(path, name)) }
|
|
12
|
+
else if (/\.(c|m)?(j|t)sx?$|\.swift$/.test(path)) files[path] = readFileSync(path, 'utf8')
|
|
13
|
+
}
|
|
14
|
+
for (const path of args.length ? args : ['.']) walk(path)
|
|
15
|
+
const found = hardcodedCodes(files)
|
|
16
|
+
console.log(found.length ? found.map(({ path, line, code }) => `${path}:${line} ${code}`).join('\n') : 'no codes written into source')
|
|
17
|
+
process.exit(found.length ? 1 : 0)
|
|
18
|
+
}
|
|
8
19
|
let dir = resolve(process.cwd())
|
|
9
20
|
while (!existsSync(join(dir, '.id')) && dirname(dir) !== dir) dir = dirname(dir)
|
|
10
21
|
const file = join(dir, '.id')
|
package/package.json
CHANGED
package/src/idv1.ts
CHANGED
|
@@ -47,6 +47,33 @@ export function directoryHealth(directory: Directory): string[] {
|
|
|
47
47
|
return problems
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
export type CodeRecord = { key: string; code: string; namespace?: string; pattern?: string }
|
|
51
|
+
|
|
52
|
+
export function recordHealth(directory: Directory, records: CodeRecord[], released: CodeRecord[] = []): string[] {
|
|
53
|
+
const problems: string[] = []
|
|
54
|
+
const owners = new Map<string, string>()
|
|
55
|
+
const before = new Map(released.map(record => [record.key, record]))
|
|
56
|
+
for (const { key, code, namespace, pattern } of records) {
|
|
57
|
+
const qualified = `-${namespace ?? ''}-${code}`
|
|
58
|
+
if (!parseCode(code)) problems.push(`${key}: ${code} is not a code`)
|
|
59
|
+
else if (pattern && taxonomyOf(directory, code)?.pattern.replace(/\s/g, '') !== pattern.replace(/\s/g, '')) problems.push(`${key}: ${code} does not fit ${pattern}`)
|
|
60
|
+
if (namespace && !directory.namespaces.some(entry => entry.shorthand === namespace)) problems.push(`${key}: namespace ${namespace} is not in .id`)
|
|
61
|
+
if (owners.has(qualified)) problems.push(`${key}: ${qualified} already belongs to ${owners.get(qualified)}`)
|
|
62
|
+
owners.set(qualified, key)
|
|
63
|
+
const old = before.get(key)
|
|
64
|
+
if (old && (old.code !== code || (old.namespace ?? '') !== (namespace ?? ''))) problems.push(`${key}: released as -${old.namespace ?? ''}-${old.code}, now ${qualified}; a code never changes`)
|
|
65
|
+
}
|
|
66
|
+
return problems
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const QUOTED_CODE = /(['"`])-([A-Z]{2})-([A-Za-z0-9]{6}(?:-[A-Za-z0-9]{6})*)\1/g
|
|
70
|
+
|
|
71
|
+
export function hardcodedCodes(files: Record<string, string>): { path: string; line: number; code: string }[] {
|
|
72
|
+
return Object.entries(files).flatMap(([path, text]) => [...text.matchAll(QUOTED_CODE)]
|
|
73
|
+
.filter(match => match[3].split(JUNCTION_SEPARATOR).every(part => parseCode(part)))
|
|
74
|
+
.map(match => ({ path, line: text.slice(0, match.index).split('\n').length, code: `-${match[2]}-${match[3]}` })))
|
|
75
|
+
}
|
|
76
|
+
|
|
50
77
|
export function resolveCode(directory: Directory, value: string): Resolved[] {
|
|
51
78
|
const qualified = /^-([A-Z]{2})-(.+)$/.exec(value)
|
|
52
79
|
return (qualified ? qualified[2] : value).split(JUNCTION_SEPARATOR).map(id => {
|
|
@@ -85,6 +112,6 @@ export function runDirectoryCommand(command: string | undefined, args: string[],
|
|
|
85
112
|
case 'health': return { code: problems.length ? 1 : 0, out: problems.length ? problems.join('\n') : 'healthy' }
|
|
86
113
|
case 'resolve': return args.length ? { code: 0, out: args.map(value => describeResolved(directory, resolveCode(directory, value))).join('\n\n') } : { code: 2, out: 'idv1 resolve <code | junction | -NS-code>' }
|
|
87
114
|
case 'directory': case undefined: return { code: 0, out: describeDirectory(directory, problems) }
|
|
88
|
-
default: return { code: 2, out: 'idv1 directory | health | resolve <code>' }
|
|
115
|
+
default: return { code: 2, out: 'idv1 directory | health | resolve <code> | hardcoded [path…]' }
|
|
89
116
|
}
|
|
90
117
|
}
|
package/src/util/explorer.css
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
.explorer-row { display: grid; grid-template-columns: calc(8 * var(--cell)) minmax(0, 1fr) auto calc(4 * var(--cell)); column-gap: calc(var(--unit) / 2); margin: 0; color: inherit; text-decoration: none; }
|
|
6
6
|
.explorer-step, .explorer-title, .explorer-row > span, p.explorer-row { box-sizing: border-box; display: block; height: var(--unit); padding-top: calc(var(--unit) / 5); line-height: calc(var(--unit) * 4 / 5); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: inherit; text-decoration: none; }
|
|
7
7
|
.explorer-row > .explorer-mono { font-family: var(--idv1-explorer-mono, monospace); font-variant-numeric: tabular-nums; white-space: pre; text-overflow: clip; letter-spacing: calc(var(--cell) - 1ch); padding-inline-start: calc((var(--cell) - 1ch) / 2); box-shadow: inset 0 0 0 1px var(--rule); background-image: linear-gradient(to right, var(--rule) 2px, transparent 2px), linear-gradient(to right, color-mix(in oklab, var(--ink) 9%, transparent) 1px, transparent 1px); background-size: var(--unit) 100%, var(--cell) 100%; }
|
|
8
|
+
.explorer-gap { margin-block-start: var(--unit); }
|
|
8
9
|
.explorer-title { margin-inline-start: auto; }
|
|
9
10
|
.explorer-dim, .explorer-step:not(:last-of-type) { color: color-mix(in oklab, var(--ink) 55%, transparent); }
|
|
10
11
|
.explorer :is(a:hover, a:focus-visible) { background-color: color-mix(in oklab, var(--ink) 14%, transparent); outline: none; }
|
package/src/util/explorer.ts
CHANGED
|
@@ -18,7 +18,7 @@ export type ExplorerData = {
|
|
|
18
18
|
type Node = ExplorerNode;
|
|
19
19
|
type Taxonomy = ExplorerTaxonomy;
|
|
20
20
|
type Identifier = ExplorerIdentifier;
|
|
21
|
-
export type ExplorerEntry = { path: string[]; id: string; label: string; kind: string; count: number; href?: string };
|
|
21
|
+
export type ExplorerEntry = { path: string[]; id: string; label: string; kind: string; count: number; href?: string; gap?: true };
|
|
22
22
|
type Entry = ExplorerEntry;
|
|
23
23
|
|
|
24
24
|
export function createExplorer(data: ExplorerData) {
|
|
@@ -56,8 +56,9 @@ export function createExplorer(data: ExplorerData) {
|
|
|
56
56
|
const target = entry && 'on' in entry && entry.on ? `-${entry.namespace}-${entry.on}` : id;
|
|
57
57
|
return [...nodes.values()].filter(node => node.qualifiedId === target);
|
|
58
58
|
};
|
|
59
|
-
const lists: Record<string, string> = {
|
|
59
|
+
const lists: Record<string, string> = { namespaces: 'Namespaces', taxonomies: 'Taxonomies', identifiers: 'Identifiers' };
|
|
60
60
|
const shapeName = (name: string) => shapes.find(kind => kind.name === name)?.label ?? name;
|
|
61
|
+
const plural = (word: string) => word === word.toUpperCase() ? word : word.replace(/([^aeiou])y$/i, '$1ie').replace(/(s|x|ch|sh)$/i, '$1e') + 's';
|
|
61
62
|
const ofShape = (name: string) => [...nodes.values()].filter(node => node.shape === name);
|
|
62
63
|
const scope = (path: string[]) => { const at = path.at(-1); return !at || lists[at] ? data.namespace : at.length === 2 ? at : at.includes(':') ? '' : nodes.get(at)?.namespace ?? ''; };
|
|
63
64
|
const idIn = (id: string, namespace: string) => id.length > 2 && id.slice(1, 3) === namespace ? id.slice(4) : short(id);
|
|
@@ -86,8 +87,8 @@ export function createExplorer(data: ExplorerData) {
|
|
|
86
87
|
function entries(path: string[]): Entry[] {
|
|
87
88
|
const at = path.at(-1);
|
|
88
89
|
if (!at) return [
|
|
89
|
-
{ path: [
|
|
90
|
-
{ path: ['namespaces'], id: '', label: 'Namespaces', kind: '', count: namespaces().length },
|
|
90
|
+
...shapes.map(kind => ({ path: [`sh:${kind.name}`], id: kind.pattern, label: plural(kind.label), kind: '', count: ofShape(kind.name).length })),
|
|
91
|
+
{ path: ['namespaces'], id: '', label: 'Namespaces', kind: '', count: namespaces().length, gap: true as const },
|
|
91
92
|
{ path: ['taxonomies'], id: '', label: 'Taxonomies', kind: '', count: taxonomy.length },
|
|
92
93
|
{ path: ['identifiers'], id: '', label: 'Identifiers', kind: '', count: identifiers.length },
|
|
93
94
|
...recordEntries([], data.collections ?? []),
|
|
@@ -96,7 +97,6 @@ export function createExplorer(data: ExplorerData) {
|
|
|
96
97
|
if (at === 'namespaces') return namespaces().map(namespace => ({
|
|
97
98
|
path: [namespace], id: namespace, label: owner(namespace)?.name ?? namespace, kind: 'Namespace', count: topLevel(namespace).length,
|
|
98
99
|
}));
|
|
99
|
-
if (at === 'shapes') return shapes.map(kind => ({ path: [`sh:${kind.name}`], id: kind.pattern, label: kind.label, kind: 'Shape', count: ofShape(kind.name).length }));
|
|
100
100
|
if (at === 'taxonomies') return taxonomy.map(entry => ({
|
|
101
101
|
path: [`tx:${entry.tag}`], id: entry.pattern, label: entry.name, kind: shapeName(entry.shape), count: tagged(entry).length,
|
|
102
102
|
}));
|
|
@@ -110,13 +110,13 @@ export function createExplorer(data: ExplorerData) {
|
|
|
110
110
|
return rowsOf(path, (children.get(at) ?? []).flatMap(id => nodes.get(id) ?? []));
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
const stepName = (path: string[], depth: number) => { const step = path[depth - 1]; return record(path.slice(0, depth))?.label ?? lists[step] ?? (step.startsWith('sh:') ? shapeName(step.slice(3)) : step.startsWith('tx:') ? step.slice(3) : step.startsWith('id:') ? idIn(step.slice(3), scope(path.slice(0, depth - 1))) : idIn(step, scope(path.slice(0, depth - 1)))); };
|
|
113
|
+
const stepName = (path: string[], depth: number) => { const step = path[depth - 1]; return record(path.slice(0, depth))?.label ?? lists[step] ?? (step.startsWith('sh:') ? plural(shapeName(step.slice(3))) : step.startsWith('tx:') ? step.slice(3) : step.startsWith('id:') ? idIn(step.slice(3), scope(path.slice(0, depth - 1))) : idIn(step, scope(path.slice(0, depth - 1)))); };
|
|
114
114
|
function title(path: string[]) {
|
|
115
115
|
const at = path.at(-1);
|
|
116
116
|
if (!at) return data.name;
|
|
117
117
|
if (lists[at]) return lists[at];
|
|
118
118
|
if (at.startsWith('rc:')) return record(path)?.label ?? '';
|
|
119
|
-
if (at.startsWith('sh:')) return
|
|
119
|
+
if (at.startsWith('sh:')) return plural(shapeName(at.slice(3)));
|
|
120
120
|
if (at.startsWith('tx:')) return `${taxonomy.find(entry => entry.tag === at.slice(3))?.name ?? at.slice(3)} · Taxonomy`;
|
|
121
121
|
if (at.startsWith('id:')) return `${identifiers.find(entry => identifierKey(entry) === at.slice(3))?.name ?? at.slice(3)} · Identifier`;
|
|
122
122
|
if (at.length === 2) return `${owner(at)?.name ?? at} · Namespace`;
|
|
@@ -172,7 +172,7 @@ export function mountExplorer(surface: HTMLElement, data: ExplorerData) {
|
|
|
172
172
|
pathLine.replaceChildren(...[data.name, ...path].map((step, depth) =>
|
|
173
173
|
link(path.slice(0, depth), 'explorer-step', [[`${depth ? stepName(path, depth) : step} /`, '']])), corner);
|
|
174
174
|
const list = entries(path);
|
|
175
|
-
rows.replaceChildren(...list.map(entry => link(entry.path, 'explorer-row', [
|
|
175
|
+
rows.replaceChildren(...list.map(entry => link(entry.path, entry.gap ? 'explorer-row explorer-gap' : 'explorer-row', [
|
|
176
176
|
[entry.id.padEnd(8), 'explorer-mono'],
|
|
177
177
|
[entry.label, ''],
|
|
178
178
|
[entry.kind, 'explorer-dim'],
|