1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+
4+ function copyDirectorySync ( src , dest ) {
5+ try {
6+ // Check if source exists
7+ if ( ! fs . existsSync ( src ) ) {
8+ console . log ( `Source directory ${ src } does not exist, skipping...` ) ;
9+ return ;
10+ }
11+
12+ // Create destination directory if it doesn't exist
13+ if ( ! fs . existsSync ( dest ) ) {
14+ fs . mkdirSync ( dest , { recursive : true } ) ;
15+ }
16+
17+ // Read directory contents
18+ const entries = fs . readdirSync ( src , { withFileTypes : true } ) ;
19+
20+ for ( const entry of entries ) {
21+ const srcPath = path . join ( src , entry . name ) ;
22+ const destPath = path . join ( dest , entry . name ) ;
23+
24+ if ( entry . isDirectory ( ) ) {
25+ copyDirectorySync ( srcPath , destPath ) ;
26+ } else {
27+ fs . copyFileSync ( srcPath , destPath ) ;
28+ }
29+ }
30+
31+ console . log ( `Successfully copied ${ src } to ${ dest } ` ) ;
32+ } catch ( error ) {
33+ console . error ( `Error copying ${ src } to ${ dest } :` , error . message ) ;
34+ }
35+ }
36+
37+ function moveDirectorySync ( src , dest ) {
38+ try {
39+ // Check if source exists
40+ if ( ! fs . existsSync ( src ) ) {
41+ console . log ( `Source directory ${ src } does not exist, skipping...` ) ;
42+ return ;
43+ }
44+
45+ // Create destination directory if it doesn't exist
46+ const destDir = path . dirname ( dest ) ;
47+ if ( ! fs . existsSync ( destDir ) ) {
48+ fs . mkdirSync ( destDir , { recursive : true } ) ;
49+ }
50+
51+ // Move the directory
52+ fs . renameSync ( src , dest ) ;
53+ console . log ( `Successfully moved ${ src } to ${ dest } ` ) ;
54+ } catch ( error ) {
55+ console . error ( `Error moving ${ src } to ${ dest } :` , error . message ) ;
56+ }
57+ }
58+
59+ // Check if we're in a CI environment (Azure DevOps sets BUILD_SOURCESDIRECTORY)
60+ const isCI = process . env . BUILD_SOURCESDIRECTORY || process . env . CI ;
61+
62+ // Move static files (this is generated, so we can always move it)
63+ const staticSrc = '.next/static' ;
64+ const staticDest = '.next/standalone/.next/static' ;
65+ moveDirectorySync ( staticSrc , staticDest ) ;
66+
67+ // Handle public folder differently for CI vs local
68+ const publicSrc = 'public' ;
69+ const publicDest = '.next/standalone/public' ;
70+
71+ if ( isCI ) {
72+ // In CI, we can move since it's a clean environment
73+ moveDirectorySync ( publicSrc , publicDest ) ;
74+ console . log ( 'CI environment detected - moved public folder' ) ;
75+ } else {
76+ // Locally, copy to avoid git changes
77+ copyDirectorySync ( publicSrc , publicDest ) ;
78+ console . log ( 'Local environment detected - copied public folder' ) ;
79+ }
80+
81+ console . log ( 'Post-build file organization complete' ) ;
0 commit comments