-
Notifications
You must be signed in to change notification settings - Fork 5
/
create-mex.ts
executable file
·46 lines (43 loc) · 1.5 KB
/
create-mex.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
import { createMex, getMediaMetadataFromLocalFile, masterExam } from '@digabi/exam-engine-mastering'
import { createReadStream, createWriteStream, promises as fs } from 'fs'
import path from 'path'
import * as uuid from 'uuid'
import { examName } from '../utils'
export default async function createMexExam({
exam,
outdir = path.dirname(exam),
nsaScripts,
securityCodes,
passphrase,
privateKey,
}: {
exam: string
outdir?: string
nsaScripts: string
securityCodes?: string
passphrase: string
privateKey: string
}): Promise<void> {
const attachmentsDir = path.resolve(path.dirname(exam), 'attachments')
const resolveAttachment = (attachment: string) => path.resolve(attachmentsDir, attachment)
const sourceXml = await fs.readFile(exam, 'utf-8')
const answersPrivateKey = await fs.readFile(privateKey, 'utf-8')
const results = await masterExam(sourceXml, () => uuid.v4(), getMediaMetadataFromLocalFile(resolveAttachment))
await fs.mkdir(outdir, { recursive: true })
for (const { language, xml, attachments } of results) {
const outputFilename = `${examName(exam)}_${language}.mex`
await createMex(
xml,
attachments.map(({ filename, restricted }) => ({
filename,
restricted,
contents: createReadStream(resolveAttachment(filename)),
})),
createReadStream(nsaScripts),
securityCodes ? createReadStream(securityCodes) : null,
passphrase,
answersPrivateKey,
createWriteStream(path.resolve(outdir, outputFilename))
)
}
}