@0001-labs/idv1 2.0.3 → 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 -12
- package/bin/idv1.mjs +1 -1
- package/package.json +1 -1
- package/src/idv1.ts +29 -82
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 of plain lines
|
|
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
|
|
@@ -42,22 +42,15 @@ SH Shop
|
|
|
42
42
|
|
|
43
43
|
coNNXX Component
|
|
44
44
|
NNsvLL Service
|
|
45
|
-
|
|
46
|
-
SHop01 Shop @ .
|
|
47
|
-
co12Ca Cart @ src/cart
|
|
48
|
-
02svSt Stripe
|
|
49
|
-
|
|
50
|
-
SHop01 rHasPt co12Ca
|
|
51
45
|
```
|
|
52
46
|
|
|
53
47
|
```sh
|
|
54
|
-
npx idv1 directory
|
|
55
|
-
npx idv1
|
|
56
|
-
npx idv1
|
|
57
|
-
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
|
|
58
51
|
```
|
|
59
52
|
|
|
60
|
-
The same operations are exported: `readDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory
|
|
53
|
+
The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory` and `runDirectoryCommand`.
|
|
61
54
|
|
|
62
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.
|
|
63
56
|
|
|
@@ -69,6 +62,8 @@ Optional `collections` carry source records and lazy `load` functions. Records m
|
|
|
69
62
|
|
|
70
63
|
## Upgrading from 1.x
|
|
71
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
|
+
|
|
72
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.
|
|
73
68
|
|
|
74
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,143 +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']
|
|
12
|
-
const list = <T>(value: unknown): T[] => (Array.isArray(value) ? value : []) as T[]
|
|
13
|
-
|
|
14
|
-
export const patternShape = (pattern: string) => parseCode(pattern.replace(/N/g, '1').replace(/[LXOP]/g, 'a'))?.shape ?? null
|
|
15
10
|
const NAMESPACE_KEY = /^[A-Z]{2}$/
|
|
16
11
|
const TAXONOMY_KEY = /^(?=[NLXOP]*[a-z]{2}[NLXOP]*$).{6}$/
|
|
17
|
-
const
|
|
18
|
-
const
|
|
12
|
+
const list = <T>(value: unknown): T[] => (Array.isArray(value) ? value : []) as T[]
|
|
13
|
+
export const patternShape = (pattern: string) => parseCode(pattern.replace(/N/g, '1').replace(/[LXOP]/g, 'a'))?.shape ?? null
|
|
19
14
|
|
|
20
15
|
export function readDirectory(text: string): Directory {
|
|
21
16
|
if (text.trimStart().startsWith('{')) {
|
|
22
|
-
const value = JSON.parse(text) as Partial<Directory>
|
|
23
|
-
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)] }
|
|
24
19
|
}
|
|
25
|
-
const directory: Directory = { shapes: {}, namespaces: [], taxonomies: [],
|
|
20
|
+
const directory: Directory = { shapes: {}, namespaces: [], taxonomies: [], unread: [] }
|
|
26
21
|
text.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#')).forEach((line, index) => {
|
|
27
22
|
const [key, ...rest] = line.split(' '), name = rest.join(' ')
|
|
28
|
-
if (index < SHAPES.length) { if (line !== SHAPES[index]) directory.shapes
|
|
23
|
+
if (index < SHAPES.length) { if (line !== SHAPES[index]) directory.shapes[SHAPES[index]] = line }
|
|
29
24
|
else if (NAMESPACE_KEY.test(key)) directory.namespaces.push({ shorthand: key, name })
|
|
30
25
|
else if (TAXONOMY_KEY.test(key)) directory.taxonomies.push({ shorthand: key.replace(/[^a-z]/g, ''), name, shape: patternShape(key)!, pattern: key })
|
|
31
|
-
else
|
|
32
|
-
else { const [label, path] = name.split(' @ '); directory.parts.push({ id: key, shape: parseCode(key)?.shape!, name: label, path: path ?? null }) }
|
|
26
|
+
else directory.unread!.push(line)
|
|
33
27
|
})
|
|
34
28
|
return directory
|
|
35
29
|
}
|
|
36
30
|
|
|
37
31
|
export function writeDirectory(directory: Directory): string {
|
|
38
32
|
return [
|
|
39
|
-
SHAPES.map(shape => directory.shapes
|
|
33
|
+
SHAPES.map(shape => directory.shapes[shape] ?? shape),
|
|
40
34
|
directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`),
|
|
41
35
|
directory.taxonomies.map(({ pattern, name }) => `${pattern.replace(/\s/g, '')} ${name}`),
|
|
42
|
-
directory.parts.map(({ id, name, path }) => `${id} ${name}${path ? ` @ ${path}` : ''}`),
|
|
43
|
-
directory.facts,
|
|
44
36
|
].filter(lines => lines.length).map(lines => lines.join('\n')).join('\n\n') + '\n'
|
|
45
37
|
}
|
|
46
38
|
|
|
47
|
-
export function directoryHealth(directory: Directory
|
|
39
|
+
export function directoryHealth(directory: Directory): string[] {
|
|
48
40
|
const problems: string[] = []
|
|
49
|
-
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}`)
|
|
50
42
|
const seen = (values: string[], what: string) => values.forEach((value, index) => { if (values.indexOf(value) !== index) problems.push(`${what} ${value} is declared twice`) })
|
|
51
43
|
problems.push(...namespaceHealth(directory.namespaces), ...taxonomyHealth(directory.taxonomies))
|
|
52
44
|
seen(directory.namespaces.map(namespace => namespace.shorthand), 'Namespace')
|
|
53
45
|
seen(directory.taxonomies.map(taxonomy => taxonomy.shorthand), 'Taxonomy')
|
|
54
|
-
|
|
55
|
-
const ids = new Set(directory.parts.map(part => part.id))
|
|
56
|
-
for (const part of directory.parts) {
|
|
57
|
-
const shape = parseCode(part.id)?.shape
|
|
58
|
-
if (shape !== part.shape) problems.push(`${part.id} reads as ${shape ?? 'no shape'}, not ${part.shape}`)
|
|
59
|
-
else if (shape === 'LLLLNN' ? blockedLLLLNNCode(part.id) : blockedCodeLetters(part.id)) problems.push(`${part.id} uses blocked letters or digits`)
|
|
60
|
-
if (!part.name) problems.push(`${part.id} has no name`)
|
|
61
|
-
if (TAXONOMY_KEY.test(part.id) || FACT.test(`${part.id} ${part.name}`)) problems.push(`${part.id} would read back as a taxonomy or a fact`)
|
|
62
|
-
if (part.path && !exists(part.path)) problems.push(`${part.id}: ${part.path} is gone`)
|
|
63
|
-
}
|
|
64
|
-
for (const fact of directory.facts) {
|
|
65
|
-
const [subject, relation, object, extra] = fact.split(' ')
|
|
66
|
-
if (extra !== undefined || !RELATION.test(relation ?? '')) problems.push(`Not a fact: ${fact}`)
|
|
67
|
-
else for (const id of [subject, object]) if (!ids.has(id)) problems.push(`Unknown id ${id} in: ${fact}`)
|
|
68
|
-
}
|
|
46
|
+
for (const line of directory.unread ?? []) problems.push(`Not vocabulary: ${line} (codes and links live with their records)`)
|
|
69
47
|
return problems
|
|
70
48
|
}
|
|
71
49
|
|
|
72
50
|
export function resolveCode(directory: Directory, value: string): Resolved[] {
|
|
73
51
|
const qualified = /^-([A-Z]{2})-(.+)$/.exec(value)
|
|
74
|
-
|
|
75
|
-
return ids.map(id => {
|
|
52
|
+
return (qualified ? qualified[2] : value).split(JUNCTION_SEPARATOR).map(id => {
|
|
76
53
|
const shape = parseCode(id)?.shape ?? null
|
|
77
|
-
|
|
78
|
-
const facts = directory.facts.filter(fact => { const [subject, , object] = fact.split(' '); return subject === id || object === id })
|
|
79
|
-
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 }
|
|
80
55
|
})
|
|
81
56
|
}
|
|
82
57
|
|
|
83
|
-
const
|
|
84
|
-
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
|
|
85
59
|
|
|
86
60
|
export function describeResolved(directory: Directory, resolved: Resolved[]): string {
|
|
87
|
-
return resolved.map(({ id, shape, namespace, taxonomy
|
|
88
|
-
`${id}
|
|
89
|
-
` shape ${shape
|
|
61
|
+
return resolved.map(({ id, shape, namespace, taxonomy }) => [
|
|
62
|
+
`${id}${shape ? '' : ' not a code'}`,
|
|
63
|
+
...(shape ? [` shape ${shapeName(directory, shape)}`] : []),
|
|
90
64
|
...(taxonomy ? [` taxonomy ${taxonomy.shorthand} · ${taxonomy.name}`] : []),
|
|
91
65
|
...(namespace ? [` namespace ${namespace.shorthand} · ${namespace.name}`] : []),
|
|
92
|
-
...(part?.path ? [` path ${part.path}`] : []),
|
|
93
|
-
...facts.map(fact => ` fact ${readFact(directory, fact)}`),
|
|
94
66
|
].join('\n')).join('\n\n')
|
|
95
67
|
}
|
|
96
68
|
|
|
97
|
-
export function
|
|
98
|
-
const count = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`
|
|
99
|
-
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 {
|
|
100
70
|
return [
|
|
101
|
-
|
|
102
|
-
problems.length ? `${count(problems.length, 'problem')}: run idv1 health` : 'healthy',
|
|
103
|
-
'',
|
|
104
|
-
...shapes.map(([shape, n]) => ` ${shape.padEnd(9)}${n} ${directory.shapes?.[shape] ?? shape}`),
|
|
105
|
-
...directory.taxonomies.map(taxonomy => ` ${taxonomy.shorthand.padEnd(9)}${directory.parts.filter(part => taxonomyOf(directory, part.id) === taxonomy).length} ${taxonomy.name}`),
|
|
106
|
-
].join('\n')
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function describeDirectory(directory: Directory): string {
|
|
110
|
-
const contains = directory.facts.map(fact => fact.split(' ')).filter(([, relation]) => relation === 'rHasPt')
|
|
111
|
-
const children = (id: string) => contains.filter(([subject]) => subject === id).map(([, , object]) => object)
|
|
112
|
-
const roots = directory.parts.filter(part => !contains.some(([, , object]) => object === part.id))
|
|
113
|
-
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}` : ''}`
|
|
114
|
-
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])))]
|
|
115
|
-
return [
|
|
116
|
-
'Shapes',
|
|
117
|
-
...SHAPES.map(shape => ` ${shape} ${directory.shapes?.[shape] ?? shape}`),
|
|
118
|
-
'',
|
|
119
|
-
'Namespaces',
|
|
120
|
-
...directory.namespaces.map(({ shorthand, name }) => ` ${shorthand} ${name}`),
|
|
71
|
+
...SHAPES.map(shape => `${shape} ${directory.shapes[shape] ?? ''}`.trimEnd()),
|
|
121
72
|
'',
|
|
122
|
-
|
|
123
|
-
...directory.taxonomies.map(({ shorthand, name, shape, pattern }) => ` ${shorthand} ${name} ${shape} ${pattern}`),
|
|
73
|
+
...directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`),
|
|
124
74
|
'',
|
|
125
|
-
|
|
126
|
-
...roots.flatMap(part => tree(part.id, 1, new Set())),
|
|
75
|
+
...directory.taxonomies.map(({ pattern, name }) => `${pattern} ${name}`),
|
|
127
76
|
'',
|
|
128
|
-
'
|
|
129
|
-
...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',
|
|
130
78
|
].join('\n')
|
|
131
79
|
}
|
|
132
80
|
|
|
133
|
-
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 } {
|
|
134
82
|
const directory = readDirectory(text)
|
|
135
|
-
const problems = directoryHealth(directory
|
|
83
|
+
const problems = directoryHealth(directory)
|
|
136
84
|
switch (command) {
|
|
137
85
|
case 'health': return { code: problems.length ? 1 : 0, out: problems.length ? problems.join('\n') : 'healthy' }
|
|
138
|
-
case '
|
|
139
|
-
case '
|
|
140
|
-
|
|
141
|
-
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>' }
|
|
142
89
|
}
|
|
143
90
|
}
|