-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathindex-cli.ts
More file actions
61 lines (59 loc) · 2.11 KB
/
Copy pathindex-cli.ts
File metadata and controls
61 lines (59 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { readFileSync, writeFileSync } from 'node:fs'
import { parseArgs } from 'node:util'
import { GitReader } from '#design-diff/git'
import { Metrics, measured, measurements } from '#design-diff/metrics'
import { SourceTree } from '#design-diff/source'
import { INDEX_VERSION, IndexStore, indexIdentity } from '#design-diff/store'
import type { Config } from '#design-diff/types'
/** Warm reusable syntax and revision facts without extracting or executing the application. */
async function main(): Promise<void> {
const { values } = parseArgs({
options: {
ref: { type: 'string' },
'cache-dir': { type: 'string' },
metrics: { type: 'string' },
identity: { type: 'boolean' },
},
strict: true,
})
const config = JSON.parse(
readFileSync(new URL('../../design-diff.config.json', import.meta.url), 'utf8')
) as Config
if (values.identity) {
process.stdout.write(`${indexIdentity(config)}\n`)
return
}
if (!values.ref || !values['cache-dir'])
throw new Error('Required: --ref revision --cache-dir directory')
const metrics = new Metrics()
await measurements.run(metrics, async () => {
const reader = new GitReader(process.cwd())
const commit = reader.compare(values.ref!, values.ref!).head
const index = await IndexStore.open(values['cache-dir']!, config)
try {
const tree = new SourceTree(reader, commit, config, index)
measured('graph', () => tree.buildGraph())
tree.complete()
if (!index.persistent) throw new Error('Index storage unavailable')
} finally {
index.close()
}
const result = {
status: 'completed',
commit,
identity: index.identity,
indexSchemaVersion: INDEX_VERSION,
...metrics.snapshot(),
}
if (values.metrics) writeFileSync(values.metrics, `${JSON.stringify(result, null, 2)}\n`)
process.stdout.write(
`${JSON.stringify({ status: result.status, commit, identity: index.identity })}\n`
)
})
}
await main().catch(() => {
process.stderr.write(
'Design index failed: check arguments, source history and resource limits.\n'
)
process.exitCode = 1
})