-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerateZipsPlugin.ts
136 lines (120 loc) · 3.59 KB
/
generateZipsPlugin.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import type { AstroIntegration } from 'astro'
import JSZip from 'jszip'
import fs from 'node:fs/promises'
import path from 'node:path'
import type { Plugin, ResolvedConfig } from 'vite'
type PathOrFolder = string | { [name: string]: PathOrFolder }
type AssetZips = {
name: string
content: Record<string, PathOrFolder>
}[]
async function addFileOrFolder(
zip: JSZip,
publicDir: string,
contentPath: string,
name: string
): Promise<void>
async function addFileOrFolder(
zip: JSZip,
publicDir: string,
folderContent: Record<string, PathOrFolder>,
name?: string
): Promise<void>
async function addFileOrFolder(
zip: JSZip,
publicDir: string,
content: PathOrFolder,
name?: string
): Promise<void>
async function addFileOrFolder(
zip: JSZip,
publicDir: string,
content: PathOrFolder,
name?: string
) {
if (typeof content === 'string') {
if (!name) throw new Error('Name is required for files')
const filePath = path.join(publicDir, content)
const data = await fs.readFile(filePath)
zip.file(name, new Uint8Array(data))
return
}
const folder = name ? zip.folder(name) : zip
if (!folder) throw new Error('Error creating folder')
await Promise.all(
Object.entries(content).map(
async ([fileOrFolderName, fileOrFolderContent]) => {
return await addFileOrFolder(
folder,
publicDir,
fileOrFolderContent,
fileOrFolderName
)
}
)
)
}
export type GenerateZipsOptions = {
zips: AssetZips
/** Base directory to read files from. Defaults to Vite's public directory. */
baseDir?: string
}
/**
* Generates zip files from the specified assets.
* @param zips For each zip file, specify the name and the content of the zip file.
* The content is an object where the keys are file/folder names and the values
* are either the file paths or deeper folder structures.
*/
export const generateZipsVitePlugin = (options: GenerateZipsOptions) => {
let config: ResolvedConfig
const plugin: Plugin = {
name: 'generate-zips',
configResolved(resolvedConfig) {
config = resolvedConfig
},
async buildEnd() {
const publicDir = options?.baseDir
? path.resolve(options.baseDir)
: config.publicDir
? path.resolve(config.root, config.publicDir)
: path.resolve(config.root, 'public')
const outDir = config.build.outDir
? path.resolve(config.root, config.build.outDir)
: path.resolve(config.root, 'dist')
for (const zipDefinition of options.zips) {
try {
console.log(`[generate-zips] Generating ${zipDefinition.name}...`)
const zip = new JSZip()
await addFileOrFolder(zip, publicDir, zipDefinition.content)
const zipContent = await zip.generateAsync({
type: 'nodebuffer',
})
const outputPath = path.join(outDir, zipDefinition.name)
await fs.mkdir(path.dirname(outputPath), { recursive: true })
await fs.writeFile(outputPath, new Uint8Array(zipContent))
} catch (error) {
console.error(
`[generate-zips] Error generating zip "${zipDefinition.name}":`
)
throw error
}
}
},
}
return plugin
}
export const generateZips = (options: GenerateZipsOptions) => {
const integration: AstroIntegration = {
name: 'generate-zips',
hooks: {
'astro:config:setup': ({ updateConfig }) => {
updateConfig({
vite: {
plugins: [generateZipsVitePlugin(options)],
},
})
},
},
}
return integration
}