@0001-labs/idv1 2.0.0 → 2.0.1
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 +21 -1
- package/package.json +1 -1
- package/src/idv1.ts +29 -2
package/README.md
CHANGED
|
@@ -29,7 +29,27 @@ const parsed = parseCode(candidate)
|
|
|
29
29
|
|
|
30
30
|
## The idv1 command
|
|
31
31
|
|
|
32
|
-
A codebase keeps a `.id` file
|
|
32
|
+
A codebase keeps a `.id` file: a section name on its own line, then one entry a line, key first. A shape name is a label, not a taxonomy reservation. A taxonomy is written as its pattern, which carries its pair and its shape; an id's shape is read from the id. `readDirectory` still reads the JSON form; `writeDirectory` writes this one.
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
shapes
|
|
36
|
+
LLNNNN User
|
|
37
|
+
|
|
38
|
+
namespaces
|
|
39
|
+
SH Shop
|
|
40
|
+
|
|
41
|
+
taxonomies
|
|
42
|
+
coNNXX Component
|
|
43
|
+
NNsvLL Service
|
|
44
|
+
|
|
45
|
+
ids
|
|
46
|
+
SHop01 Shop @ .
|
|
47
|
+
co12Ca Cart @ src/cart
|
|
48
|
+
02svSt Stripe
|
|
49
|
+
|
|
50
|
+
facts
|
|
51
|
+
SHop01 rHasPt co12Ca
|
|
52
|
+
```
|
|
33
53
|
|
|
34
54
|
```sh
|
|
35
55
|
npx idv1 directory
|
package/package.json
CHANGED
package/src/idv1.ts
CHANGED
|
@@ -11,9 +11,36 @@ export type Resolved = { id: string; shape: CodeShape | null; namespace?: Namesp
|
|
|
11
11
|
const SHAPES: CodeShape[] = ['LLNNNN', 'NNLLLL', 'LLLLNN', 'LLNNXX', 'XXXXXX']
|
|
12
12
|
const list = <T>(value: unknown): T[] => (Array.isArray(value) ? value : []) as T[]
|
|
13
13
|
|
|
14
|
+
const SECTIONS = ['shapes', 'namespaces', 'taxonomies', 'ids', 'facts'] as const
|
|
15
|
+
export const patternShape = (pattern: string) => parseCode(pattern.replace(/N/g, '1').replace(/[LXOP]/g, 'a'))?.shape ?? null
|
|
16
|
+
|
|
14
17
|
export function readDirectory(text: string): Directory {
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
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), parts: list(value.parts), facts: list(value.facts) }
|
|
21
|
+
}
|
|
22
|
+
const directory: Directory = { shapes: {}, namespaces: [], taxonomies: [], parts: [], facts: [] }
|
|
23
|
+
let section = ''
|
|
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 }
|
|
26
|
+
const [key, ...rest] = line.split(' '), name = rest.join(' ')
|
|
27
|
+
if (section === 'shapes') directory.shapes![key as CodeShape] = name
|
|
28
|
+
else if (section === 'namespaces') directory.namespaces.push({ shorthand: key, name })
|
|
29
|
+
else if (section === 'taxonomies') directory.taxonomies.push({ shorthand: key.replace(/[^a-z]/g, ''), name, shape: patternShape(key)!, pattern: key })
|
|
30
|
+
else if (section === 'ids') { const [label, path] = name.split(' @ '); directory.parts.push({ id: key, shape: parseCode(key)?.shape!, name: label, path: path ?? null }) }
|
|
31
|
+
else if (section === 'facts') directory.facts.push(line)
|
|
32
|
+
}
|
|
33
|
+
return directory
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function writeDirectory(directory: Directory): string {
|
|
37
|
+
return [
|
|
38
|
+
['shapes', Object.entries(directory.shapes ?? {}).map(([shape, name]) => `${shape} ${name}`)],
|
|
39
|
+
['namespaces', directory.namespaces.map(({ shorthand, name }) => `${shorthand} ${name}`)],
|
|
40
|
+
['taxonomies', directory.taxonomies.map(({ pattern, name }) => `${pattern.replace(/\s/g, '')} ${name}`)],
|
|
41
|
+
['ids', directory.parts.map(({ id, name, path }) => `${id} ${name}${path ? ` @ ${path}` : ''}`)],
|
|
42
|
+
['facts', directory.facts],
|
|
43
|
+
].filter(([, lines]) => lines.length).map(([section, lines]) => [section, ...lines].join('\n')).join('\n\n') + '\n'
|
|
17
44
|
}
|
|
18
45
|
|
|
19
46
|
export function directoryHealth(directory: Directory, exists: (path: string) => boolean = () => true): string[] {
|