@0001-labs/idv1 2.0.2 → 3.0.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 +7 -16
- package/bin/idv1.mjs +1 -1
- package/package.json +1 -1
- package/src/idv1.ts +37 -88
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ const parsed = parseCode(candidate)
|
|
|
29
29
|
|
|
30
30
|
## The idv1 command
|
|
31
31
|
|
|
32
|
-
A codebase keeps a `.id` file
|
|
32
|
+
A codebase keeps its vocabulary in a `.id` file of plain lines. The first five name the shapes in pattern order (`LLNNNN`, `NNLLLL`, `LLLLNN`, `LLNNXX`, `XXXXXX`); a line left as its pattern keeps no name, and a shape name is a label, not a taxonomy reservation. After them, two capitals and a name are a namespace, and a six-character pattern holding one lowercase pair and a name is a taxonomy, which carries its shape. Blank lines and `#` comments are free. Codes themselves live with their records, in a column beside the database's own key, never in `.id`.
|
|
33
33
|
|
|
34
34
|
```text
|
|
35
35
|
User
|
|
@@ -38,30 +38,19 @@ Product
|
|
|
38
38
|
Item
|
|
39
39
|
Event
|
|
40
40
|
|
|
41
|
-
namespaces
|
|
42
41
|
SH Shop
|
|
43
42
|
|
|
44
|
-
taxonomies
|
|
45
43
|
coNNXX Component
|
|
46
44
|
NNsvLL Service
|
|
47
|
-
|
|
48
|
-
ids
|
|
49
|
-
SHop01 Shop @ .
|
|
50
|
-
co12Ca Cart @ src/cart
|
|
51
|
-
02svSt Stripe
|
|
52
|
-
|
|
53
|
-
facts
|
|
54
|
-
SHop01 rHasPt co12Ca
|
|
55
45
|
```
|
|
56
46
|
|
|
57
47
|
```sh
|
|
58
|
-
npx idv1 directory
|
|
59
|
-
npx idv1
|
|
60
|
-
npx idv1
|
|
61
|
-
npx idv1 resolve co14Ch
|
|
48
|
+
npx idv1 directory # the vocabulary, and whether it is healthy
|
|
49
|
+
npx idv1 health # exits 1 with each problem
|
|
50
|
+
npx idv1 resolve co12Ca # the shape, taxonomy and namespace a code reads as
|
|
62
51
|
```
|
|
63
52
|
|
|
64
|
-
The same operations are exported: `readDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory
|
|
53
|
+
The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory` and `runDirectoryCommand`.
|
|
65
54
|
|
|
66
55
|
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.
|
|
67
56
|
|
|
@@ -73,6 +62,8 @@ Optional `collections` carry source records and lazy `load` functions. Records m
|
|
|
73
62
|
|
|
74
63
|
## Upgrading from 1.x
|
|
75
64
|
|
|
65
|
+
Release 3 keeps `.id` to vocabulary: `Directory` holds shapes, namespaces and taxonomies; `Part`, facts and `idv1 status` are gone, and `idv1 health` names every other line it finds. `readDirectory` still reads the JSON form.
|
|
66
|
+
|
|
76
67
|
Release 2 changes API names, not code grammar, scan payloads or capacity. Replace semantic shape arguments with patterns, `parseCode(...).kind` with `.shape`, and `codeKind` with `codeShape`. Use `codeCandidate(shape)` and `isCodeOf(shape, value)` in place of per-kind wrappers. Regex constants are pattern-named, such as `LLNNXX_CODE`; `blockedLLLLNNCode` replaces the semantic name of its filter. `.id` part and taxonomy `shape` values use patterns. Keep application words in the optional `shapes` map.
|
|
77
68
|
|
|
78
69
|
## Documentation
|
package/bin/idv1.mjs
CHANGED
|
@@ -12,6 +12,6 @@ if (!existsSync(file)) {
|
|
|
12
12
|
console.error('No .id file here or above. See https://idv1.0001.dev/docs/annotate')
|
|
13
13
|
process.exit(2)
|
|
14
14
|
}
|
|
15
|
-
const { code, out } = runDirectoryCommand(command, args, readFileSync(file, 'utf8')
|
|
15
|
+
const { code, out } = runDirectoryCommand(command, args, readFileSync(file, 'utf8'))
|
|
16
16
|
console.log(out)
|
|
17
17
|
process.exit(code)
|
package/package.json
CHANGED
package/src/idv1.ts
CHANGED
|
@@ -1,141 +1,90 @@
|
|
|
1
1
|
import { JUNCTION_SEPARATOR, parseCode, type CodeShape } from './identifier'
|
|
2
|
-
import { blockedCodeLetters, blockedLLLLNNCode } from './util/blockedWords'
|
|
3
2
|
import { namespaceHealth, namespaceOf, type Namespace } from './namespace'
|
|
4
3
|
import { taxonomyHealth, taxonomyOf, type Taxonomy } from './taxonomy'
|
|
5
4
|
|
|
6
|
-
export type Part = { id: string; shape: CodeShape; name: string; path: string | null }
|
|
7
5
|
export type ShapeNames = Partial<Record<CodeShape, string>>
|
|
8
|
-
export type Directory = { shapes
|
|
9
|
-
export type Resolved = { id: string; shape: CodeShape | null; namespace?: Namespace; taxonomy?: Taxonomy
|
|
6
|
+
export type Directory = { shapes: ShapeNames; namespaces: Namespace[]; taxonomies: Taxonomy[]; unread?: string[] }
|
|
7
|
+
export type Resolved = { id: string; shape: CodeShape | null; namespace?: Namespace; taxonomy?: Taxonomy }
|
|
10
8
|
|
|
11
9
|
const SHAPES: CodeShape[] = ['LLNNNN', 'NNLLLL', 'LLLLNN', 'LLNNXX', 'XXXXXX']
|
|
10
|
+
const NAMESPACE_KEY = /^[A-Z]{2}$/
|
|
11
|
+
const TAXONOMY_KEY = /^(?=[NLXOP]*[a-z]{2}[NLXOP]*$).{6}$/
|
|
12
12
|
const list = <T>(value: unknown): T[] => (Array.isArray(value) ? value : []) as T[]
|
|
13
|
-
|
|
14
|
-
const SECTIONS = ['namespaces', 'taxonomies', 'ids', 'facts'] as const
|
|
15
13
|
export const patternShape = (pattern: string) => parseCode(pattern.replace(/N/g, '1').replace(/[LXOP]/g, 'a'))?.shape ?? null
|
|
16
14
|
|
|
17
15
|
export function readDirectory(text: string): Directory {
|
|
18
16
|
if (text.trimStart().startsWith('{')) {
|
|
19
|
-
const value = JSON.parse(text) as Partial<Directory>
|
|
20
|
-
return { shapes: value.shapes ?? {}, namespaces: list(value.namespaces), taxonomies: list(value.taxonomies),
|
|
17
|
+
const value = JSON.parse(text) as Partial<Directory> & { parts?: { id: string }[]; facts?: string[] }
|
|
18
|
+
return { shapes: value.shapes ?? {}, namespaces: list(value.namespaces), taxonomies: list(value.taxonomies), unread: [...list<{ id: string }>(value.parts).map(part => part.id), ...list<string>(value.facts)] }
|
|
21
19
|
}
|
|
22
|
-
const directory: Directory = { shapes: {}, namespaces: [], taxonomies: [],
|
|
23
|
-
|
|
24
|
-
for (const line of text.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#'))) {
|
|
25
|
-
if ((SECTIONS as readonly string[]).includes(line)) { section = line; continue }
|
|
20
|
+
const directory: Directory = { shapes: {}, namespaces: [], taxonomies: [], unread: [] }
|
|
21
|
+
text.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#')).forEach((line, index) => {
|
|
26
22
|
const [key, ...rest] = line.split(' '), name = rest.join(' ')
|
|
27
|
-
if (
|
|
28
|
-
else if (
|
|
29
|
-
else if (
|
|
30
|
-
else
|
|
31
|
-
|
|
32
|
-
}
|
|
23
|
+
if (index < SHAPES.length) { if (line !== SHAPES[index]) directory.shapes[SHAPES[index]] = line }
|
|
24
|
+
else if (NAMESPACE_KEY.test(key)) directory.namespaces.push({ shorthand: key, name })
|
|
25
|
+
else if (TAXONOMY_KEY.test(key)) directory.taxonomies.push({ shorthand: key.replace(/[^a-z]/g, ''), name, shape: patternShape(key)!, pattern: key })
|
|
26
|
+
else directory.unread!.push(line)
|
|
27
|
+
})
|
|
33
28
|
return directory
|
|
34
29
|
}
|
|
35
30
|
|
|
36
31
|
export function writeDirectory(directory: Directory): string {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
['facts', directory.facts],
|
|
43
|
-
].filter(([, lines]) => lines.length).map(([section, lines]) => [section, ...lines].join('\n')).join('\n\n') + '\n'
|
|
32
|
+
return [
|
|
33
|
+
SHAPES.map(shape => directory.shapes[shape] ?? shape),
|
|
34
|
+
directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`),
|
|
35
|
+
directory.taxonomies.map(({ pattern, name }) => `${pattern.replace(/\s/g, '')} ${name}`),
|
|
36
|
+
].filter(lines => lines.length).map(lines => lines.join('\n')).join('\n\n') + '\n'
|
|
44
37
|
}
|
|
45
38
|
|
|
46
|
-
export function directoryHealth(directory: Directory
|
|
39
|
+
export function directoryHealth(directory: Directory): string[] {
|
|
47
40
|
const problems: string[] = []
|
|
48
|
-
for (const [shape, name] of Object.entries(directory.shapes
|
|
41
|
+
for (const [shape, name] of Object.entries(directory.shapes)) if (!SHAPES.includes(shape as CodeShape) || typeof name !== 'string' || !name.trim()) problems.push(`Invalid shape name: ${shape}`)
|
|
49
42
|
const seen = (values: string[], what: string) => values.forEach((value, index) => { if (values.indexOf(value) !== index) problems.push(`${what} ${value} is declared twice`) })
|
|
50
43
|
problems.push(...namespaceHealth(directory.namespaces), ...taxonomyHealth(directory.taxonomies))
|
|
51
44
|
seen(directory.namespaces.map(namespace => namespace.shorthand), 'Namespace')
|
|
52
45
|
seen(directory.taxonomies.map(taxonomy => taxonomy.shorthand), 'Taxonomy')
|
|
53
|
-
|
|
54
|
-
const ids = new Set(directory.parts.map(part => part.id))
|
|
55
|
-
for (const part of directory.parts) {
|
|
56
|
-
const shape = parseCode(part.id)?.shape
|
|
57
|
-
if (shape !== part.shape) problems.push(`${part.id} reads as ${shape ?? 'no shape'}, not ${part.shape}`)
|
|
58
|
-
else if (shape === 'LLLLNN' ? blockedLLLLNNCode(part.id) : blockedCodeLetters(part.id)) problems.push(`${part.id} uses blocked letters or digits`)
|
|
59
|
-
if (!part.name) problems.push(`${part.id} has no name`)
|
|
60
|
-
if (part.path && !exists(part.path)) problems.push(`${part.id}: ${part.path} is gone`)
|
|
61
|
-
}
|
|
62
|
-
for (const fact of directory.facts) {
|
|
63
|
-
const [subject, relation, object, extra] = fact.split(' ')
|
|
64
|
-
if (extra !== undefined || !/^r[A-Za-z]{5}$/.test(relation ?? '')) problems.push(`Not a fact: ${fact}`)
|
|
65
|
-
else for (const id of [subject, object]) if (!ids.has(id)) problems.push(`Unknown id ${id} in: ${fact}`)
|
|
66
|
-
}
|
|
46
|
+
for (const line of directory.unread ?? []) problems.push(`Not vocabulary: ${line} (codes and links live with their records)`)
|
|
67
47
|
return problems
|
|
68
48
|
}
|
|
69
49
|
|
|
70
50
|
export function resolveCode(directory: Directory, value: string): Resolved[] {
|
|
71
51
|
const qualified = /^-([A-Z]{2})-(.+)$/.exec(value)
|
|
72
|
-
|
|
73
|
-
return ids.map(id => {
|
|
52
|
+
return (qualified ? qualified[2] : value).split(JUNCTION_SEPARATOR).map(id => {
|
|
74
53
|
const shape = parseCode(id)?.shape ?? null
|
|
75
|
-
|
|
76
|
-
const facts = directory.facts.filter(fact => { const [subject, , object] = fact.split(' '); return subject === id || object === id })
|
|
77
|
-
return { id, shape, namespace, taxonomy: shape ? taxonomyOf(directory, id) : undefined, part: directory.parts.find(part => part.id === id), facts }
|
|
54
|
+
return { id, shape, namespace: namespaceOf(directory, id, qualified?.[1]), taxonomy: shape ? taxonomyOf(directory, id) : undefined }
|
|
78
55
|
})
|
|
79
56
|
}
|
|
80
57
|
|
|
81
|
-
const
|
|
82
|
-
const readFact = (directory: Directory, fact: string) => { const [subject, relation, object] = fact.split(' '); return `${subject} ${relation} ${object} ${nameOf(directory, subject)} → ${nameOf(directory, object)}` }
|
|
58
|
+
const shapeName = (directory: Directory, shape: CodeShape) => directory.shapes[shape] ? `${shape} · ${directory.shapes[shape]}` : shape
|
|
83
59
|
|
|
84
60
|
export function describeResolved(directory: Directory, resolved: Resolved[]): string {
|
|
85
|
-
return resolved.map(({ id, shape, namespace, taxonomy
|
|
86
|
-
`${id}
|
|
87
|
-
` shape ${shape
|
|
61
|
+
return resolved.map(({ id, shape, namespace, taxonomy }) => [
|
|
62
|
+
`${id}${shape ? '' : ' not a code'}`,
|
|
63
|
+
...(shape ? [` shape ${shapeName(directory, shape)}`] : []),
|
|
88
64
|
...(taxonomy ? [` taxonomy ${taxonomy.shorthand} · ${taxonomy.name}`] : []),
|
|
89
65
|
...(namespace ? [` namespace ${namespace.shorthand} · ${namespace.name}`] : []),
|
|
90
|
-
...(part?.path ? [` path ${part.path}`] : []),
|
|
91
|
-
...facts.map(fact => ` fact ${readFact(directory, fact)}`),
|
|
92
66
|
].join('\n')).join('\n\n')
|
|
93
67
|
}
|
|
94
68
|
|
|
95
|
-
export function
|
|
96
|
-
const count = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`
|
|
97
|
-
const shapes = SHAPES.map(shape => [shape, directory.parts.filter(part => part.shape === shape).length] as const).filter(([, n]) => n)
|
|
69
|
+
export function describeDirectory(directory: Directory, problems: string[] = directoryHealth(directory)): string {
|
|
98
70
|
return [
|
|
99
|
-
|
|
100
|
-
problems.length ? `${count(problems.length, 'problem')}: run idv1 health` : 'healthy',
|
|
101
|
-
'',
|
|
102
|
-
...shapes.map(([shape, n]) => ` ${shape.padEnd(9)}${n} ${directory.shapes?.[shape] ?? shape}`),
|
|
103
|
-
...directory.taxonomies.map(taxonomy => ` ${taxonomy.shorthand.padEnd(9)}${directory.parts.filter(part => taxonomyOf(directory, part.id) === taxonomy).length} ${taxonomy.name}`),
|
|
104
|
-
].join('\n')
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function describeDirectory(directory: Directory): string {
|
|
108
|
-
const contains = directory.facts.map(fact => fact.split(' ')).filter(([, relation]) => relation === 'rHasPt')
|
|
109
|
-
const children = (id: string) => contains.filter(([subject]) => subject === id).map(([, , object]) => object)
|
|
110
|
-
const roots = directory.parts.filter(part => !contains.some(([, , object]) => object === part.id))
|
|
111
|
-
const line = (part: Part | undefined, id: string, depth: number) => `${' '.repeat(depth)}${id} ${part?.name ?? '?'}${part && taxonomyOf(directory, id) ? ` (${taxonomyOf(directory, id)!.name})` : ''}${part?.path ? ` ${part.path}` : ''}`
|
|
112
|
-
const tree = (id: string, depth: number, trail: Set<string>): string[] => trail.has(id) ? [] : [line(directory.parts.find(part => part.id === id), id, depth), ...children(id).flatMap(child => tree(child, depth + 1, new Set([...trail, id])))]
|
|
113
|
-
return [
|
|
114
|
-
'Shapes',
|
|
115
|
-
...SHAPES.map(shape => ` ${shape} ${directory.shapes?.[shape] ?? shape}`),
|
|
116
|
-
'',
|
|
117
|
-
'Namespaces',
|
|
118
|
-
...directory.namespaces.map(({ shorthand, name }) => ` ${shorthand} ${name}`),
|
|
71
|
+
...SHAPES.map(shape => `${shape} ${directory.shapes[shape] ?? ''}`.trimEnd()),
|
|
119
72
|
'',
|
|
120
|
-
|
|
121
|
-
...directory.taxonomies.map(({ shorthand, name, shape, pattern }) => ` ${shorthand} ${name} ${shape} ${pattern}`),
|
|
73
|
+
...directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`),
|
|
122
74
|
'',
|
|
123
|
-
|
|
124
|
-
...roots.flatMap(part => tree(part.id, 1, new Set())),
|
|
75
|
+
...directory.taxonomies.map(({ pattern, name }) => `${pattern} ${name}`),
|
|
125
76
|
'',
|
|
126
|
-
'
|
|
127
|
-
...directory.facts.filter(fact => fact.split(' ')[1] !== 'rHasPt').map(fact => ` ${readFact(directory, fact)}`),
|
|
77
|
+
problems.length ? `${problems.length} problem${problems.length === 1 ? '' : 's'}: run idv1 health` : 'healthy',
|
|
128
78
|
].join('\n')
|
|
129
79
|
}
|
|
130
80
|
|
|
131
|
-
export function runDirectoryCommand(command: string | undefined, args: string[], text: string
|
|
81
|
+
export function runDirectoryCommand(command: string | undefined, args: string[], text: string): { code: number; out: string } {
|
|
132
82
|
const directory = readDirectory(text)
|
|
133
|
-
const problems = directoryHealth(directory
|
|
83
|
+
const problems = directoryHealth(directory)
|
|
134
84
|
switch (command) {
|
|
135
85
|
case 'health': return { code: problems.length ? 1 : 0, out: problems.length ? problems.join('\n') : 'healthy' }
|
|
136
|
-
case '
|
|
137
|
-
case '
|
|
138
|
-
|
|
139
|
-
default: return { code: 2, out: 'idv1 directory | status | health | resolve <id>' }
|
|
86
|
+
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
|
+
case 'directory': case undefined: return { code: 0, out: describeDirectory(directory, problems) }
|
|
88
|
+
default: return { code: 2, out: 'idv1 directory | health | resolve <code>' }
|
|
140
89
|
}
|
|
141
90
|
}
|