@0001-labs/idv1 3.0.1 → 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 +2 -1
- package/bin/idv1.mjs +13 -2
- package/package.json +1 -1
- package/src/idv1.ts +28 -1
package/README.md
CHANGED
|
@@ -48,9 +48,10 @@ 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
|
|
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
|
}
|