/
ExportHTML.ts
41 lines (32 loc) · 918 Bytes
/
ExportHTML.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
import * as path from 'path'
// import { promises as fs } from 'fs'
import * as jetpack from "fs-jetpack";
export interface ExportOptions {
folderPath: string,
url: string,
data: string | null
srcFilePath: string | null
}
export const exportHTML = async (options: ExportOptions) => {
const { folderPath, url, data, srcFilePath } = options
const file = url.endsWith('/') ? `${url}index.html` : url
const filePath = path.join(folderPath ? folderPath : ".", file)
// console.log(`Saving file ${filePath}`)
// await fs.mkdir(path.dirname(filePath), {recursive:true})
if (data) {
try {
await jetpack.writeAsync(filePath, data)
return
} catch (error) {
console.error(error)
}
}
if (srcFilePath) {
try {
await jetpack.copyAsync(srcFilePath, filePath, {overwrite : true})
return
} catch (error) {
console.error(error)
}
}
}