-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·144 lines (131 loc) · 3.61 KB
/
index.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
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
141
142
143
144
#!/usr/bin/env node
import path from 'path';
import project from './lib/project.js';
import parseArgs from 'minimist';
const argv = parseArgs(process.argv.slice(2));
import chokidar from 'chokidar';
import { glob, globSync } from 'glob';
import { existsSync } from 'node:fs';
import { Tail } from 'tail';
import log from './lib/logging.js';
import bustCache from './lib/bustCache.js';
import { buildSass, buildSassTheme } from './lib/style.js';
import buildJS from './lib/scripts.js';
import buildBlock from './lib/blocks.js';
import buildImages from './lib/images.js';
import buildFonts from './lib/fonts.js';
import buildBrowserSync from './lib/browsersync.js';
let chokidarOpts = {
ignoreInitial: true,
ignored: [
project.path + '/blocks/*/build/**/*'
]
};
let sassGlobPath = project.package?.sdc?.sassGlobPath || project.path + '{/_src/style,/blocks}/**/*.scss';
let sassGlob = globSync(sassGlobPath, {
ignore: [
project.path + '/_src/style/partials/_theme.scss'
]
});
let jsGlobPath = project.package?.sdc?.jsGlobPath || project.path + '/_src/scripts/**/*.js';
let jsGlob = globSync(jsGlobPath, {
ignore: []
});
let blockGlobPath = project.package?.sdc?.blockGlobPath || project.path + '/blocks/*';
let blockGlob = globSync(blockGlobPath);
function bustFunctionsCache() {
bustCache(project.path + '/functions.php');
}
function frontrunImages() {
[
project.path + '/_src/images/',
project.path + '/_src/images/**/*/'
].forEach((block) => {
const imageDirectories = globSync(block);
imageDirectories.forEach((dir) => {
buildImages(dir);
});
});
}
function runBlocks() {
for (var block of blockGlob) {
buildBlock(block);
}
bustFunctionsCache();
}
function runSass() {
buildSassTheme();
for (var block of filesSass) {
buildSass(block.file, block.name, sassGlob);
bustFunctionsCache();
}
}
function runJS() {
for (var block of filesJS) {
buildJS(block.file, block.name, jsGlob);
bustFunctionsCache();
}
}
let entries = {};
for (const [name, files] of Object.entries(project.package.sdc.entries)) {
entries[name] = [];
files.forEach(function(file) {
entries[name].push(project.path + file);
});
}
let sassBlocksGlob = globSync(project.path + '/blocks/*/*.scss');
for (var filename of sassBlocksGlob) {
entries[`blocks/${path.basename(path.dirname(filename))}/style`] = [ filename ];
}
let filesSass = [];
let filesJS = [];
for (const [name, files] of Object.entries(entries)) {
files.forEach(function(file) {
switch (path.parse(file).ext) {
case '.scss':
filesSass.push({
'name': name,
'file': file
});
break;
case '.js':
filesJS.push({
'name': name,
'file': file
});
break;
}
});
}
runBlocks();
runSass();
runJS();
frontrunImages()
buildFonts(project.path + '/_src/fonts');
if (argv.watch) {
buildBrowserSync();
chokidar.watch(blockGlob, chokidarOpts).on('all', (event, path) => {
runBlocks();
});
chokidar.watch(sassGlob, chokidarOpts).on('all', (event, path) => {
runSass();
});
chokidar.watch(project.path + '/theme.json', chokidarOpts).on('all', (event, path) => {
runSass();
});
chokidar.watch(jsGlob, chokidarOpts).on('all', (event, path) => {
runJS();
});
chokidar.watch(project.path + '/_src/images/**/*', chokidarOpts).on('all', (event, path) => {
frontrunImages();
});
let errorLogPath = process.env.ERROR_LOG_PATH || project.package.sdc?.error_log_path || '../../../../../logs/php/error.log';
if (existsSync(errorLogPath)) {
let errorLogTail = new Tail(errorLogPath);
errorLogTail.on('line', function(data) {
log('php', data);
});
} else {
log('info', `Cannot find error log @ ${errorLogPath}. Skipping watching php error logs`);
}
}