Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
Latest commit
53 lines (46 loc) · 1.73 KB
/
Copy pathbuild.js
File metadata and controls
53 lines (46 loc) · 1.73 KB
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
constfs=require('fs');
constpath=require('path');
constejs=require('ejs');
constfsExtra=require('fs-extra');
// Define paths
constoutputDir=path.join(__dirname,'build');
constviewsDir=path.join(__dirname,'views');
constpublicDir=path.join(__dirname,'public');// Source for static assets
// Function to render EJS templates to static HTML
constrenderEjsToHtml=async(template,data={},outputFile)=>{
try{
consthtml=awaitejs.renderFile(path.join(viewsDir,template),data);
fs.writeFileSync(path.join(outputDir,outputFile),html);
console.log(`Rendered ${template} to ${outputFile}`);
}catch(err){
console.error(`Error rendering ${template}:`,err);
}
};
// Function to copy all files from the public folder to the build folder
constcopyPublicFolder=async()=>{
try{
if(fs.existsSync(publicDir)){
awaitfsExtra.copy(publicDir,outputDir,{overwrite: true});
console.log('Copied public folder to build directory');
}else{
console.warn('Public directory does not exist:',publicDir);
}
}catch(err){
console.error('Error copying public folder:',err);
}
};
// Build function
constbuild=async()=>{
// Create the output directory if it doesn't exist
if(!fs.existsSync(outputDir)){
fs.mkdirSync(outputDir,{recursive: true});
console.log('Created output directory:',outputDir);
}
// Render EJS templates with dynamic title data
awaitrenderEjsToHtml('index.ejs',{title: 'Home'},'index.html');
// await renderEjsToHtml('downloads.ejs', { title: 'Downloads' }, 'downloads.html');
// Copy static files from public folder to build folder
awaitcopyPublicFolder();
};
// Run the build process
build().catch(console.error);