-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
50 lines (39 loc) · 1.57 KB
/
build.js
File metadata and controls
50 lines (39 loc) · 1.57 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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const srcDir = 'src'
const buildDir = 'build'
fs.rmSync(buildDir, { recursive: true, force: true })
fs.mkdirSync(buildDir, { recursive: true })
const layoutTemplate = fs.readFileSync(path.join(srcDir, 'layout.html'), 'utf8')
const isDevelopment = process.argv.includes('--dev')
const basePath = isDevelopment ? '/' : '/jory/'
let layout = layoutTemplate.replace(/{base}/g, basePath)
fs.readdirSync(srcDir).forEach(file => {
const srcPath = path.join(srcDir, file)
if (file === 'layout.html') return
if (file.endsWith('.html')) {
const content = fs.readFileSync(srcPath, 'utf8')
const pageName = file.replace('.html', '')
const pageJsPath = path.join(srcDir, `${pageName}.js`)
const scriptTag = fs.existsSync(pageJsPath) && pageName !== 'index'
? `\n <script defer src="${pageName}.js"></script>`
: ''
let html = layout.replace('{content}', content).replace('{script}', scriptTag)
if (file === 'index.html') {
fs.writeFileSync(path.join(buildDir, file), html)
console.log(`Built ${file}`)
} else {
// Create directory with index.html for clean URLs
const dirPath = path.join(buildDir, pageName)
fs.mkdirSync(dirPath, { recursive: true })
fs.writeFileSync(path.join(dirPath, 'index.html'), html)
console.log(`Built ${pageName}/index.html`)
}
} else {
const destPath = path.join(buildDir, file)
fs.cpSync(srcPath, destPath, { recursive: true })
console.log(`Copied ${file}`)
}
})
console.log('Build complete!')