-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
71 lines (59 loc) · 1.53 KB
/
build.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const fs = require('fs')
const rimraf = require("rimraf");
const { ncp } = require('ncp')
const { minify } = require('html-minifier')
const { ROOT } = require('./config')
const templatePath = 'public/index.html'
const rawTemplate = fs.readFileSync(templatePath, 'utf8')
const parsedTemplate = rawTemplate.replace(/%{ROOT}/g, ROOT)
const minifiedPageContent = minify(parsedTemplate, {
removeComments: true,
collapseWhitespace: true
})
const initializeDirectory = dir => {
if (dir && fs.existsSync(dir)) {
rimraf.sync(dir)
}
if (dir && !fs.existsSync(dir)){
fs.mkdirSync(dir)
}
}
const distDirectory = 'dist'
initializeDirectory(distDirectory)
const directoriesToCopy = [
'config.js',
'css/',
'data/',
'img/',
'js/',
'projects/',
'template/'
]
for (const dir of directoriesToCopy) {
ncp(dir, `${distDirectory}/${dir}`, err => {
if (err) throw err
console.log(`The directory "${dir}" has been copied!`)
})
}
const routes = [
'index.html',
'home/index.html',
'about/index.html',
'hacks/index.html',
'webdictionary/index.html',
'webinfluencers/index.html',
'weblandscape/index.html',
'webphenomena/index.html',
'worldwidemap/index.html'
]
for (const route of routes) {
const pathItems = route.split('/')
const [dir] = pathItems.length > 1 ? pathItems : []
if (dir) {
initializeDirectory(`./${distDirectory}/${dir}`)
}
fs.writeFile(`./${distDirectory}/${route}`, minifiedPageContent, err => {
if (err) throw err
console.log(`The file "${route}" has been saved!`)
})
}