@@ -39,14 +39,14 @@ export default defineEventHandler(async (event) => {
39
39
const contentRoot = `${ rootDir } /content`
40
40
const imageRoot = `${ rootDir } /public/img/uploads`
41
41
42
+ const uploads : Array < ( ) => Promise < void > > = [ ]
42
43
const body = await readMultipartFormData ( event ) as PayloadData
43
44
const showcase : Showcase = {
44
45
it : empty ( ) ,
45
46
de : empty ( ) ,
46
47
fr : empty ( ) ,
47
48
en : empty ( )
48
49
}
49
-
50
50
const titleDe = body . find ( field => field . name === 'title-de' ) ?. data ?. toString ( )
51
51
showcase . slug = slugify ( titleDe ! , { lower : true , locale : 'de' } )
52
52
@@ -80,15 +80,15 @@ export default defineEventHandler(async (event) => {
80
80
break
81
81
case "image" : {
82
82
const imagePath = `${ imageRoot } /${ showcase . slug } -image.jpg`
83
- await fs . writeFile ( imagePath , data )
83
+ uploads . push ( fs . writeFile . bind ( null , imagePath , data ) )
84
84
}
85
85
break
86
86
}
87
87
}
88
88
89
89
return validate ( event , showcase ) || ( async ( ) => {
90
90
// TODO: choose to save to GitHub or locally based on environment
91
- await save ( showcase , contentRoot )
91
+ await save ( showcase , uploads , contentRoot )
92
92
93
93
if ( process . env . NODE_ENV === 'production' ) {
94
94
// TODO: commit and push to repo
@@ -99,17 +99,19 @@ export default defineEventHandler(async (event) => {
99
99
} ) ( )
100
100
} ) ;
101
101
102
- function save ( showcase : Showcase , contentRoot : string ) {
102
+ function save ( showcase : Showcase , uploads : Array < ( ) => Promise < void > > , contentRoot : string ) {
103
103
const { slug } = showcase
104
104
105
- return Promise . all ( languages . map ( language => {
105
+ const writeContent = languages . map ( language => {
106
106
const path = `${ contentRoot } /showcases/${ slug } .${ language } .md`
107
107
108
108
const { body, ...meta } = showcase [ language ]
109
109
const frontMatter = yaml . stringify ( meta )
110
110
111
111
return fs . writeFile ( path , `---\n${ frontMatter } ---\n${ body } ` )
112
- } ) )
112
+ } )
113
+
114
+ return Promise . all ( [ ...writeContent , ...uploads . map ( upload => upload ( ) ) ] )
113
115
}
114
116
115
117
interface Setter {
0 commit comments