-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
47 lines (39 loc) · 1.42 KB
/
index.ts
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
import through2 from 'through2'
import type { AnyPointer } from 'clownface'
import type { Quad } from '@rdfjs/types'
import { resolveImport } from './lib/path.js'
import Environment from './lib/env.js'
import fetchImport from './lib/fetchImport.js'
import { log } from './lib/log.js'
function transform(env: Environment) {
const code = env.namespace('https://code.described.at/')
const importStatements: AnyPointer = env.clownface()
return through2.obj(async function (quad: Quad, _, done) {
if (quad.predicate.equals(code.imports) || quad.predicate.equals(code.extension)) {
importStatements.dataset.add(quad)
return done()
}
done(null, quad)
}, async function (done) {
try {
const imports = importStatements.has(code.imports)
.map(Import => {
const importPath = Import.out(code.imports).term!
const extension = Import.out(code.extension).value
return resolveImport(importPath, { extension })
})
for (const importTarget of imports) {
log.debug(`Importing ${importTarget}`)
const fetchStream = await fetchImport(env, importTarget)
const importStream = fetchStream.pipe(transform(env))
for await (const importedQuad of importStream) {
this.push(importedQuad)
}
}
} catch (e: unknown) {
this.destroy(new Error(`Failed to import: ${e}`))
}
done()
})
}
export default transform