sdc-build-wp 4.0.2 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/eslint.config.js +1 -1
  2. package/index.js +215 -119
  3. package/package.json +2 -3
package/eslint.config.js CHANGED
@@ -2,7 +2,7 @@ export default [
2
2
  {
3
3
  languageOptions: {
4
4
  parserOptions: {
5
- ecmaVersion: 6,
5
+ ecmaVersion: "latest",
6
6
  sourceType: "module",
7
7
  ecmaFeatures: {
8
8
  jsx: true,
package/index.js CHANGED
@@ -4,7 +4,7 @@ import project from './lib/project.js';
4
4
  import parseArgs from 'minimist';
5
5
  const argv = parseArgs(process.argv.slice(2));
6
6
  import chokidar from 'chokidar';
7
- import { glob, globSync } from 'glob';
7
+ import { glob, readdir } from 'node:fs/promises';
8
8
  import { existsSync } from 'node:fs';
9
9
  import { Tail } from 'tail';
10
10
 
@@ -18,149 +18,245 @@ import buildImages from './lib/images.js';
18
18
  import buildFonts from './lib/fonts.js';
19
19
  import buildBrowserSync from './lib/browsersync.js';
20
20
 
21
+ let paths = {
22
+ theme: {
23
+ json: `${project.path}/theme.json`,
24
+ scss: `${project.path}/_src/style/partials/_theme.scss`
25
+ },
26
+ nodeModules: `${project.path}/node_modules`,
27
+ composer: {
28
+ vendor: `${project.path}/vendor`
29
+ },
30
+ images: project.package?.sdc?.imagesPath || `${project.path}/_src/images`,
31
+ errorLog: process.env.ERROR_LOG_PATH || project.package.sdc?.error_log_path || '../../../../../logs/php/error.log'
32
+ };
33
+
21
34
  let chokidarOpts = {
22
35
  ignoreInitial: true,
23
36
  ignored: [
24
- project.path + '/node_modules',
25
- project.path + '/vendor',
26
- project.path + '/blocks/*/build/**/*'
37
+ paths.nodeModules,
38
+ paths.composer.vendor,
39
+ paths.theme.scss,
40
+ project.path + '/blocks/*/build/**/*',
27
41
  ]
28
42
  };
29
43
 
30
- let phpGlobPath = project.package?.sdc?.phpGlobPath || project.path + '/**/*.php';
31
- let phpGlob = globSync(phpGlobPath, {
32
- ignore: [
33
- project.path + '/node_modules',
34
- project.path + '/vendor'
35
- ]
36
- });
37
- let sassGlobPath = project.package?.sdc?.sassGlobPath || project.path + '{/_src/style,/blocks}/**/*.scss';
38
- let sassGlob = globSync(sassGlobPath, {
39
- ignore: [
40
- project.path + '/_src/style/partials/_theme.scss'
41
- ]
42
- });
43
- let jsGlobPath = project.package?.sdc?.jsGlobPath || project.path + '/_src/scripts/**/*.js';
44
- let jsGlob = globSync(jsGlobPath, {
45
- ignore: []
46
- });
47
- let blockGlobPath = project.package?.sdc?.blockGlobPath || project.path + '/blocks/*';
48
- let blockGlob = globSync(blockGlobPath);
49
-
50
- function bustFunctionsCache() {
51
- bustCache(project.path + '/functions.php');
52
- }
44
+ let globs = {};
45
+ let entries = {};
46
+ let filesSass = [];
47
+ let filesJS = [];
48
+
49
+ let builds = argv.builds ? argv.builds.split(',') : [
50
+ 'sass',
51
+ 'js',
52
+ 'blocks',
53
+ 'images',
54
+ 'fonts',
55
+ 'php'
56
+ ];
57
+
58
+ (async() => {
59
+
60
+ if (builds.includes('sass')) {
61
+ globs.sass = await Array.fromAsync(
62
+ glob(project.package?.sdc?.sassGlobPath ||
63
+ `${project.path}{/_src/style,/blocks}/**/*.scss`)
64
+ );
65
+ }
66
+ if (builds.includes('js')) {
67
+ globs.js = await Array.fromAsync(
68
+ glob(project.package?.sdc?.jsGlobPath ||
69
+ `${project.path}/_src/scripts/**/*.js`)
70
+ );
71
+ }
72
+ if (builds.includes('blocks')) {
73
+ globs.blocks = await Array.fromAsync(
74
+ glob(`${project.path}/blocks/*`)
75
+ );
76
+ globs.blocksSass = await Array.fromAsync(
77
+ glob(`${project.path}/blocks/*/src/*.scss`)
78
+ );
79
+ }
80
+ if (builds.includes('images')) {
81
+ globs.images = await Array.fromAsync(
82
+ glob(project.package?.sdc?.imagesPath ||
83
+ `${paths.images}/**/*`)
84
+ );
85
+ globs.imageDirectories = [
86
+ paths.images,
87
+ ...await getAllSubdirectories(paths.images)
88
+ ];
89
+ }
90
+ if (builds.includes('php')) {
91
+ globs.php = await Array.fromAsync(
92
+ glob(project.package?.sdc?.jsGlobPath ||
93
+ `${project.path}/**/*.php`)
94
+ );
95
+ }
96
+
97
+ for (const [name, files] of Object.entries(project.package.sdc.entries)) {
98
+ entries[name] = [];
99
+ files.forEach(function(file) {
100
+ entries[name].push(project.path + file);
101
+ });
102
+ }
103
+ // for (var filename of globs.blocksSass) {
104
+ // entries[`blocks/${path.basename(path.dirname(filename))}/style`] = [ filename ];
105
+ // }
53
106
 
54
- function frontrunImages() {
55
- [
56
- project.path + '/_src/images/',
57
- project.path + '/_src/images/**/*/'
58
- ].forEach((block) => {
59
- const imageDirectories = globSync(block);
60
- imageDirectories.forEach((dir) => {
61
- buildImages(dir);
107
+ for (const [name, files] of Object.entries(entries)) {
108
+ files.forEach(function(file) {
109
+ switch (path.parse(file).ext) {
110
+ case '.scss':
111
+ if (builds.includes('sass')) {
112
+ filesSass.push({
113
+ 'name': name,
114
+ 'file': file
115
+ });
116
+ }
117
+ break;
118
+ case '.js':
119
+ if (builds.includes('js')) {
120
+ filesJS.push({
121
+ 'name': name,
122
+ 'file': file
123
+ });
124
+ }
125
+ break;
126
+ }
62
127
  });
128
+ }
129
+
130
+ if (builds.includes('sass')) {
131
+ await runSass(true);
132
+ }
133
+ if (builds.includes('js')) {
134
+ await runJS();
135
+ }
136
+ if (builds.includes('blocks')) {
137
+ await runBlocks();
138
+ }
139
+ if (builds.includes('images')) {
140
+ await frontrunImages();
141
+ }
142
+ if (builds.includes('fonts')) {
143
+ await buildFonts(project.path + '/_src/fonts');
144
+ }
145
+ // if (builds.includes('php') && shouldPHPLint) {
146
+ // await runPHP(null, 'warn'); // this errors "Fatal error: Allowed memory size"
147
+ // }
148
+
149
+ if (argv.watch) {
150
+
151
+ buildBrowserSync();
152
+
153
+ if (builds.includes('sass')) {
154
+ chokidar.watch([
155
+ ...[paths.theme.json],
156
+ globs.sass
157
+ ], {
158
+ ...chokidarOpts
159
+ }).on('all', (event, path) => {
160
+ runSass(path == paths.theme.json);
161
+ });
162
+ }
163
+
164
+ if (builds.includes('js')) {
165
+ chokidar.watch(globs.js, {
166
+ ...chokidarOpts
167
+ }).on('all', (event, path) => {
168
+ runJS();
169
+ });
170
+ }
171
+
172
+ if (builds.includes('blocks')) {
173
+ for (let block of globs.blocks) {
174
+ chokidar.watch(`${block}/src`, {
175
+ ...chokidarOpts
176
+ }).on('all', (event, path) => {
177
+ runBlocks(block);
178
+ });
179
+ }
180
+ }
181
+
182
+ if (builds.includes('images')) {
183
+ chokidar.watch(paths.images, chokidarOpts).on('all', (event, path) => {
184
+ frontrunImages();
185
+ });
186
+ }
187
+
188
+ if (builds.includes('php') && shouldPHPLint) {
189
+ chokidar.watch(globs.php, {
190
+ ...chokidarOpts
191
+ }).on('all', (event, path) => {
192
+ runPHP(path);
193
+ });
194
+ }
195
+
196
+ if (existsSync(paths.errorLog)) {
197
+ let errorLogTail = new Tail(paths.errorLog);
198
+ errorLogTail.on('line', function(data) {
199
+ log('php', data);
200
+ });
201
+ } else {
202
+ log('info', `Cannot find error log @ ${paths.errorLog}. Skipping watching php error logs`);
203
+ }
204
+ }
205
+
206
+ })();
207
+
208
+ async function bustFunctionsCache() {
209
+ await bustCache(`${project.path}/functions.php`);
210
+ }
211
+
212
+ async function frontrunImages() {
213
+ globs.imageDirectories.forEach(directory => {
214
+ buildImages(directory);
63
215
  });
64
216
  }
65
217
 
66
- function runBlocks() {
67
- for (var block of blockGlob) {
68
- buildBlock(block);
218
+ async function runBlocks(singleBlock) {
219
+ if (singleBlock) {
220
+ await buildBlock(singleBlock);
221
+ } else {
222
+ for (var block of globs.blocks) {
223
+ await buildBlock(block);
224
+ }
69
225
  }
70
- bustFunctionsCache();
226
+ await bustFunctionsCache();
71
227
  }
72
228
 
73
- function runSass() {
74
- buildSassTheme();
229
+ async function runSass(buildTheme = true) {
230
+ if (buildTheme) {
231
+ await buildSassTheme();
232
+ }
75
233
  for (var block of filesSass) {
76
- buildSass(block.file, block.name, sassGlob);
77
- bustFunctionsCache();
234
+ await buildSass(block.file, block.name, globs.sass);
235
+ await bustFunctionsCache();
78
236
  }
79
237
  }
80
238
 
81
- function runJS() {
239
+ async function runJS() {
82
240
  for (var block of filesJS) {
83
- buildJS(block.file, block.name, jsGlob);
241
+ await buildJS(block.file, block.name, globs.js);
84
242
  bustFunctionsCache();
85
243
  }
86
244
  }
87
245
 
88
- function runPHP(file, method) {
89
- buildPHP(file, method);
90
- }
91
-
92
- let entries = {};
93
- for (const [name, files] of Object.entries(project.package.sdc.entries)) {
94
- entries[name] = [];
95
- files.forEach(function(file) {
96
- entries[name].push(project.path + file);
97
- });
98
- }
99
- let sassBlocksGlob = globSync(project.path + '/blocks/*/*.scss');
100
- for (var filename of sassBlocksGlob) {
101
- entries[`blocks/${path.basename(path.dirname(filename))}/style`] = [ filename ];
246
+ async function runPHP(file, method) {
247
+ await buildPHP(file, method);
102
248
  }
103
249
 
104
- let filesSass = [];
105
- let filesJS = [];
106
-
107
- for (const [name, files] of Object.entries(entries)) {
108
- files.forEach(function(file) {
109
- switch (path.parse(file).ext) {
110
- case '.scss':
111
- filesSass.push({
112
- 'name': name,
113
- 'file': file
114
- });
115
- break;
116
- case '.js':
117
- filesJS.push({
118
- 'name': name,
119
- 'file': file
120
- });
121
- break;
250
+ async function getAllSubdirectories(dir) {
251
+ let subdirectories = [];
252
+ const subdirectoriesEntries = await readdir(dir, { withFileTypes: true });
253
+ for (const subdirectoriesEntry of subdirectoriesEntries) {
254
+ if (subdirectoriesEntry.isDirectory()) {
255
+ const subdirPath = path.join(dir, subdirectoriesEntry.name);
256
+ subdirectories.push(subdirPath);
257
+ const nestedSubdirs = await getAllSubdirectories(subdirPath);
258
+ subdirectories = subdirectories.concat(nestedSubdirs);
122
259
  }
123
- });
124
- }
125
-
126
- // if (shouldPHPLint) {
127
- // runPHP(null, 'warn'); // this errors "Fatal error: Allowed memory size"
128
- // }
129
- runBlocks();
130
- runSass();
131
- runJS();
132
- frontrunImages()
133
- buildFonts(project.path + '/_src/fonts');
134
-
135
- if (argv.watch) {
136
- buildBrowserSync();
137
- chokidar.watch(blockGlob, chokidarOpts).on('all', (event, path) => {
138
- runBlocks();
139
- });
140
- chokidar.watch(sassGlob, chokidarOpts).on('all', (event, path) => {
141
- runSass();
142
- });
143
- chokidar.watch(project.path + '/theme.json', chokidarOpts).on('all', (event, path) => {
144
- runSass();
145
- });
146
- chokidar.watch(jsGlob, chokidarOpts).on('all', (event, path) => {
147
- runJS();
148
- });
149
- chokidar.watch(project.path + '/_src/images/**/*', chokidarOpts).on('all', (event, path) => {
150
- frontrunImages();
151
- });
152
- let errorLogPath = process.env.ERROR_LOG_PATH || project.package.sdc?.error_log_path || '../../../../../logs/php/error.log';
153
- if (existsSync(errorLogPath)) {
154
- let errorLogTail = new Tail(errorLogPath);
155
- errorLogTail.on('line', function(data) {
156
- log('php', data);
157
- });
158
- } else {
159
- log('info', `Cannot find error log @ ${errorLogPath}. Skipping watching php error logs`);
160
- }
161
- if (shouldPHPLint) {
162
- chokidar.watch(phpGlob, chokidarOpts).on('change', (path) => {
163
- runPHP(path);
164
- });
165
260
  }
261
+ return subdirectories;
166
262
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdc-build-wp",
3
- "version": "4.0.2",
3
+ "version": "4.1.0",
4
4
  "description": "Custom WordPress build process.",
5
5
  "engines": {
6
6
  "node": ">=20"
@@ -27,11 +27,10 @@
27
27
  "autoprefixer": "^10.4.20",
28
28
  "browser-sync": "^3.0.3",
29
29
  "chalk": "^5.4.1",
30
- "chokidar": "^3.6.0",
30
+ "chokidar": "^4.0.3",
31
31
  "esbuild": "^0.25.0",
32
32
  "eslint": "^9.21.0",
33
33
  "fs-extra": "^11.3.0",
34
- "glob": "^11.0.1",
35
34
  "imagemin": "^9.0.0",
36
35
  "imagemin-jpegtran": "^8.0.0",
37
36
  "imagemin-pngquant": "^10.0.0",