-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeWallsProject.ts
58 lines (54 loc) · 1.75 KB
/
writeWallsProject.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
48
49
50
51
52
53
54
55
56
57
58
import {
WallsProjectEntry,
WallsProjectEntryType,
WallsWpjFile,
} from '../wpj/WallsWpjFile'
import fs from 'fs'
import mkdirp from 'mkdirp'
import iconv from 'iconv-lite'
import formatWallsWpjFile from '../wpj/formatWallsWpjFile'
import { promisify } from 'util'
import Path from 'path'
import formatWallsSrvFile from '../srv/formatWallsSrvFile'
async function writeSurveys(
path: string,
entry: WallsProjectEntry
): Promise<void> {
const childPath = entry.path ? Path.resolve(path, entry.path) : path
switch (entry.type) {
case WallsProjectEntryType.Book: {
await Promise.all(
entry.children.map((child) => writeSurveys(childPath, child))
)
break
}
case WallsProjectEntryType.Survey: {
const { content, name } = entry
if (!content || !name) break
const srvPath = Path.resolve(
childPath,
/\.[^.]+$/.test(name) ? name : name + '.srv'
)
await mkdirp(Path.dirname(srvPath))
const fsStream = fs.createWriteStream(srvPath)
const out = iconv.encodeStream('win1252').pipe(fsStream)
formatWallsSrvFile(content, { write: (data: string) => out.write(data) })
await promisify((cb: (err: any) => void) => fsStream.close(cb))()
}
}
}
export default async function writeWallsProject(
wpjPath: string,
wpj: WallsWpjFile
): Promise<void> {
await Promise.all([
(async (): Promise<void> => {
await mkdirp(Path.dirname(wpjPath))
const fsStream = fs.createWriteStream(wpjPath)
const out = iconv.encodeStream('win1252').pipe(fsStream)
formatWallsWpjFile(wpj, { write: (data: string) => out.write(data) })
await promisify((cb: (err: any) => void) => fsStream.close(cb))()
})(),
writeSurveys(Path.dirname(wpjPath), wpj.root),
])
}