1- import { writeFile , mkdir , rm , unlink , readFile } from ' fs/promises'
2- import { NextResponse } from ' next/server'
3- import path from ' path'
1+ import { writeFile , mkdir , rm , unlink , readFile } from " fs/promises" ;
2+ import { NextResponse } from " next/server" ;
3+ import path from " path" ;
44
55function getBasePath ( ) {
6- return path . join ( process . cwd ( ) , '..' , ' compiled' )
6+ return path . join ( process . cwd ( ) , ".." , " compiled" ) ;
77}
88
99export async function POST ( request : Request ) {
1010 try {
11- const { path : filePath , content } = await request . json ( )
12- const fullPath = path . join ( getBasePath ( ) , filePath )
13-
11+ const { path : filePath , content, isDirectory } = await request . json ( ) ;
12+ const fullPath = path . join ( getBasePath ( ) , filePath ) ;
13+
1414 // Ensure the directory exists
15- await mkdir ( path . dirname ( fullPath ) , { recursive : true } )
16-
17- // Write the file
18- await writeFile ( fullPath , JSON . stringify ( content , null , 2 ) )
19- return NextResponse . json ( { success : true } )
15+ await mkdir ( path . dirname ( fullPath ) , { recursive : true } ) ;
16+
17+ if ( isDirectory ) {
18+ // If it's a directory, just create it
19+ await mkdir ( fullPath , { recursive : true } ) ;
20+ } else {
21+ // If it's a file, write the content
22+ await writeFile ( fullPath , JSON . stringify ( content , null , 2 ) ) ;
23+ }
24+
25+ return NextResponse . json ( { success : true } ) ;
2026 } catch ( error ) {
21- console . error ( ' Error creating file:' , error )
27+ console . error ( " Error creating file:" , error ) ;
2228 return NextResponse . json (
23- { error : ' Failed to create file' } ,
29+ { error : " Failed to create file" } ,
2430 { status : 500 }
25- )
31+ ) ;
2632 }
2733}
2834
2935export async function DELETE ( request : Request ) {
3036 try {
31- const { path : itemPath , type } = await request . json ( )
32- const fullPath = path . join ( getBasePath ( ) , itemPath )
37+ const { path : itemPath , type } = await request . json ( ) ;
38+ const fullPath = path . join ( getBasePath ( ) , itemPath ) ;
3339
34- if ( type === ' folder' ) {
35- await rm ( fullPath , { recursive : true , force : true } )
40+ if ( type === " folder" ) {
41+ await rm ( fullPath , { recursive : true , force : true } ) ;
3642 } else {
37- await unlink ( fullPath )
43+ await unlink ( fullPath ) ;
3844 }
3945
40- return NextResponse . json ( { success : true } )
46+ return NextResponse . json ( { success : true } ) ;
4147 } catch ( error ) {
42- console . error ( ' Error deleting item:' , error )
48+ console . error ( " Error deleting item:" , error ) ;
4349 return NextResponse . json (
44- { error : ' Failed to delete item' } ,
50+ { error : " Failed to delete item" } ,
4551 { status : 500 }
46- )
52+ ) ;
4753 }
4854}
4955
5056export async function GET ( request : Request ) {
5157 try {
52- const { searchParams } = new URL ( request . url )
53- const filePath = searchParams . get ( ' path' )
54-
58+ const { searchParams } = new URL ( request . url ) ;
59+ const filePath = searchParams . get ( " path" ) ;
60+
5561 if ( ! filePath ) {
5662 return NextResponse . json (
57- { error : ' No file path provided' } ,
63+ { error : " No file path provided" } ,
5864 { status : 400 }
59- )
65+ ) ;
6066 }
6167
62- const fullPath = path . join ( getBasePath ( ) , filePath )
63- const fileContent = await readFile ( fullPath , ' utf-8' )
64- const parsedContent = JSON . parse ( fileContent )
65-
66- return NextResponse . json ( parsedContent )
68+ const fullPath = path . join ( getBasePath ( ) , filePath ) ;
69+ const fileContent = await readFile ( fullPath , " utf-8" ) ;
70+ const parsedContent = JSON . parse ( fileContent ) ;
71+
72+ return NextResponse . json ( parsedContent ) ;
6773 } catch ( error ) {
68- console . error ( 'Error reading file:' , error )
69- return NextResponse . json (
70- { error : 'Failed to read file' } ,
71- { status : 500 }
72- )
74+ console . error ( "Error reading file:" , error ) ;
75+ return NextResponse . json ( { error : "Failed to read file" } , { status : 500 } ) ;
7376 }
7477}
7578
7679export async function PUT ( request : Request ) {
7780 try {
78- const { path : filePath , content } = await request . json ( )
79-
81+ const { path : filePath , content } = await request . json ( ) ;
82+
8083 if ( ! filePath ) {
8184 return NextResponse . json (
82- { error : ' No file path provided' } ,
85+ { error : " No file path provided" } ,
8386 { status : 400 }
84- )
87+ ) ;
8588 }
8689
8790 // Validate content
8891 if ( ! content ) {
8992 return NextResponse . json (
90- { error : ' No content provided' } ,
93+ { error : " No content provided" } ,
9194 { status : 400 }
92- )
95+ ) ;
9396 }
9497
95- const fullPath = path . join ( getBasePath ( ) , filePath )
96-
98+ const fullPath = path . join ( getBasePath ( ) , filePath ) ;
99+
97100 // Check if directory exists, if not create it
98- await mkdir ( path . dirname ( fullPath ) , { recursive : true } )
99-
101+ await mkdir ( path . dirname ( fullPath ) , { recursive : true } ) ;
102+
100103 // Add retry logic
101104 const maxRetries = 3 ;
102105 for ( let i = 0 ; i < maxRetries ; i ++ ) {
103106 try {
104- const jsonContent = JSON . stringify ( content , null , 2 )
105- await writeFile ( fullPath , jsonContent , ' utf-8' )
106- return NextResponse . json ( { success : true } )
107+ const jsonContent = JSON . stringify ( content , null , 2 ) ;
108+ await writeFile ( fullPath , jsonContent , " utf-8" ) ;
109+ return NextResponse . json ( { success : true } ) ;
107110 } catch ( writeError ) {
108111 if ( i === maxRetries - 1 ) throw writeError ;
109- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // Wait 1s before retry
112+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ; // Wait 1s before retry
110113 }
111114 }
112115 } catch ( error ) {
113- console . error ( ' Error writing file:' , error )
116+ console . error ( " Error writing file:" , error ) ;
114117 return NextResponse . json (
115- {
116- error : ' Failed to write file' ,
117- details : error instanceof Error ? error . message : ' Unknown error'
118+ {
119+ error : " Failed to write file" ,
120+ details : error instanceof Error ? error . message : " Unknown error" ,
118121 } ,
119122 { status : 500 }
120- )
123+ ) ;
121124 }
122- }
125+ }
0 commit comments