-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-demo-site.js
67 lines (59 loc) · 2.02 KB
/
build-demo-site.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
import { fileURLToPath } from 'url'
import fs from 'fs'
import path from 'path'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const ORIGINAL = path.join(__dirname, 'demo')
const SITE_FOLDER = path.join(__dirname, 'demo-standalone')
const HTML_FILE = path.join(SITE_FOLDER, 'index.html')
function copyFolderSync(from, to) {
fs.mkdirSync(to)
fs.readdirSync(from).forEach(element => {
if (fs.lstatSync(path.join(from, element)).isFile()) {
fs.copyFileSync(path.join(from, element), path.join(to, element))
} else {
copyFolderSync(path.join(from, element), path.join(to, element))
}
})
}
const deleteFolderRecursive = function(dirpath) {
if (fs.existsSync(dirpath)) {
fs.readdirSync(dirpath).forEach((file, index) => {
const curPath = path.join(dirpath, file)
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
deleteFolderRecursive(curPath)
} else {
// delete file
fs.unlinkSync(curPath)
}
})
fs.rmdirSync(dirpath)
}
}
const mapScript = (htmlstring, source, altname) => {
fs.copyFileSync(__dirname + source, path.join(SITE_FOLDER, 'lib', altname))
return htmlstring.replace(source, './lib/' + altname)
}
deleteFolderRecursive(SITE_FOLDER)
copyFolderSync(ORIGINAL, SITE_FOLDER)
fs.mkdirSync(path.join(SITE_FOLDER, 'lib'))
let htmlString = fs.readFileSync(HTML_FILE, 'utf-8')
htmlString = mapScript(
htmlString,
'/node_modules/es-module-shims/dist/es-module-shims.js',
'es-module-shims.js'
)
htmlString = mapScript(
htmlString,
'/node_modules/hyperapp/src/index.js',
'hyperapp.js'
)
htmlString = mapScript(
htmlString,
'/node_modules/hyperlit/index.js',
'hyperlit.js'
)
copyFolderSync(__dirname + '/src', path.join(SITE_FOLDER, 'lib', 'form'))
htmlString = htmlString.replace('/src/index.js', './lib/form/index.js')
fs.writeFileSync(HTML_FILE, htmlString, 'utf-8')