-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarter.js
More file actions
140 lines (134 loc) · 4.3 KB
/
Copy pathstarter.js
File metadata and controls
140 lines (134 loc) · 4.3 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// @ts-check
process.argv.shift() // Remove the node executable file path.
process.argv.shift() // Remove the script file path.
const
browserSync = require('browser-sync').create(),
fs = require('fs'),
path = require('path'),
// ...
base = process.argv.shift() || (fs.existsSync(`${__dirname}/src/application/pages/index.html`) ? 'application' : 'template'),
language = process.argv.shift() || 'en',
root = path.resolve(__dirname, base).replace(/\\/g, '/'),
src = 'src',
pages = `${src}/pages`,
fallback = `${pages}/${language}`,
start = `${pages}/index.html`
/**
* This function is used to copy a file from the page folder to the fallback folder.
* @param {string} filename
* @returns
*/
const copy = (filename) => {
const
source = `${pages}/${filename}`,
target = `${fallback}/${filename}`
return fs.copyFile(`${root}/${source}`, `${root}/${target}`, error => {
if (error) console.error(
`ERROR: '${base}/${source}' could not be copied to '${base}/${target}'.`,
error instanceof Error ? error.message : error
); else console.log(`'${base}/${source}' was copied to '${base}/${target}'.`)
})
}
// Create the fallback directory if necessary
if (fs.existsSync(`${root}/${fallback}`)) console.log(`The fallback directory '${base}/${fallback}/' already exists.`)
else fs.mkdir(
`${root}/${fallback}`,
{ recursive: true }, error => {
if (error) console.error(
`ERROR: The fallback directory '${base}/${fallback}/' could not be created.`,
error instanceof Error ? error.message : error
); else console.log(`The fallback directory '${base}/${fallback}/' has been created.`)
}
)
// Synchronise the main folder with the fallback folder. Meaning the folder representing the default language.
const resources = {
pages: fs.readdirSync(`${root}/${pages}`, { withFileTypes: true }),
fallback: fs.readdirSync(`${root}/${fallback}`, { withFileTypes: true }),
}
for (const resource of resources.pages) if (resource.isFile()) copy(resource.name)
for (const resource of resources.fallback) if (resource.isFile() && !fs.existsSync(`${root}/${pages}/${resource.name}`)) {
fs.unlinkSync(`${root}/${fallback}/${resource.name}`) // delete phantom files
console.log(`The fallback file '${base}/${fallback}/${resource.name}' was deleted.`)
}
/**
* Watch the pages/ folder for changes to synchronise with the fallback/ folder.
* @see https://nodejs.org/docs/latest/api/fs.html#fswatchfilename-options-listener
*
* @type {NodeJS.Timeout | undefined} Property used to throttle the number of changes.
*/
let timeout = undefined
fs.watch(
`${root}/${pages}`,
(event, filename) => {
if (filename && fs.statSync(`${root}/${pages}/${filename}`).isFile()) {
clearTimeout(timeout) // Reset the timeout to prevent unnecessary changes.
timeout = setTimeout(
(event, filename) => {
if (event == 'change' || fs.existsSync(`${root}/${pages}/${filename}`)) copy(filename) // existing or new file
else if (fs.existsSync(`${root}/${fallback}/${filename}`)) {
fs.unlinkSync(`${root}/${fallback}/${filename}`) // deleted file
console.log(`The fallback file '${base}/${fallback}/${filename}' was deleted.`)
}
},
1000,
event, filename
)
}
}
)
/**
* Serve files from the $base folder and automatically watch for html/css/js changes.
* @see https://browsersync.io/
* @see https://browsersync.io/docs
* @see https://browsersync.io/docs/options
*/
browserSync.init({
watch: true,
serveStatic: [base, `${base}/public`, `${base}/${src}`, `${base}/${pages}`],
server: {
baseDir: base,
index: start,
},
// files: [
// // '**/*.*',
// '**/*.css',
// '**/*.gif',
// '**/*.htm',
// '**/*.html',
// '**/*.inc',
// '**/*.jpeg',
// '**/*.jpg',
// '**/*.js',
// '**/*.json',
// '**/*.mp3',
// '**/*.mp4',
// '**/*.php',
// '**/*.png',
// '**/*.svg',
// '**/*.webp',
// // '!*.css.map',
// // '!*.less',
// // '!*.log',
// // '!*.sass',
// // '!*.scss',
// // '!*.sql',
// // '!*.ts',
// ],
ignore: [
'.vscode/**',
'builds/**',
'node_modules/**',
'package.*json',
'starter*.js',
'*.md',
'*.less',
'*.css.map',
],
// port: 9013, // Using a custom port leads to an unpleasant delay between switches
// cwd: '.',
logPrefix: 'WebReloader',
// reloadDelay: delay * 2,
// startPath: ''
// localOnly: true,
})
// browserSync.watch(['**/*.*'], { ignored: '*.less' })