@ideasonpurpose/build-tools-wordpress 1.1.5 → 1.1.7

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.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file. Dates are d
4
4
 
5
5
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
6
6
 
7
+ #### v1.1.6
8
+
9
+ > 30 January 2024
10
+
11
+ - test stuff and fixes
12
+ - misc fixes and first zip testing
13
+
14
+ #### v1.1.5
15
+
16
+ > 25 January 2024
17
+
18
+ - move the localPort stuff into devserver-proxy
19
+
7
20
  #### v1.1.4
8
21
 
9
22
  > 25 January 2024
package/README.md CHANGED
@@ -1,18 +1,25 @@
1
1
  # @ideasonpurpose/build-tools-wordpress
2
2
 
3
- #### Version 1.1.5
3
+ #### Version 1.1.7
4
4
 
5
5
  ![NPM Version](https://img.shields.io/npm/v/%40ideasonpurpose%2Fbuild-tools-wordpress?logo=npm)
6
6
  ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/ideasonpurpose/build-tools-wordpress/npm-publish.yml?logo=github&logoColor=white)
7
7
 
8
-
9
8
  Build scripts and dependencies for IOP's WordPress development environments.
10
9
 
10
+ ## Config
11
+
12
+ Each project should have an **ideasonpurpose.config.js** file in the same directory as **package.json**. This file should export an object with at least three properties set:
13
+
14
+ - **`src`** - The **source** directory containing source files which should be compiled or transposed. The contents of this directory will be omitted from builds.
15
+ - **`dist`** - The **distribution** directory where processed, production-ready files will be output to. All contents of this directory will be included in builds.
16
+ - **`src`** - An array of file entry points relative to the `src` directory. Each entry point will generate a like-named output file. All files and assets imported by a given entry point will be accessible from that entry's corresponding output file.
17
+
11
18
  ## About This Project
12
19
 
13
20
  These tools were migrated from our [Docker-based WordPress build tools](https://github.com/ideasonpurpose/docker-build) to speed up development and began the process of moving our build tools away from webpack. Gathering dependencies also simplifies the package.json files in host projects, making those slightly more manageable.
14
21
 
15
- The included webpack.config.js file works best when paired with a PHP environment like our [Docker WordPress environments](https://github.com/ideasonpurpose/docker-wordpress-dev). It's capable of proxying to other servers, but that's sort of crazy.
22
+ The **example/webpack.config.js** file works best when paired with a PHP environment like our [Docker WordPress environments](https://github.com/ideasonpurpose/docker-wordpress-dev). It's capable of proxying to other servers, but that's sort of crazy.
16
23
 
17
24
  ### Additional Notes
18
25
 
@@ -20,12 +27,14 @@ This project expects an entirely ES Module based environment and specifies all d
20
27
 
21
28
  #### Publishing to [npm](https://www.npmjs.com/package/@ideasonpurpose/build-tools-wordpress)
22
29
 
23
- A GitHub action will auto-publish version-tagged releases to npm. In order to publish, the repository must have an `NPM_TOKEN` secret set with the token from npm. [Log into npmjs.org](https://www.npmjs.com/login) with a publish-authorized account, then find the token page linked from the Profile page sidebar. Generate a new token and update the repository secret.
30
+ A GitHub action will auto-publish version-tagged releases to npm. In order to publish, the repository must have an `NPM_TOKEN` secret set with the token from npm. [Log into npmjs.org](https://www.npmjs.com/login) with a publish-authorized account, then find the token page linked from the Profile page sidebar. Generate a new token and update the repository secret.
24
31
 
25
- ##  
32
+ <!-- START IOP CREDIT BLURB -->
26
33
 
34
+ ## &nbsp;
27
35
 
28
36
  #### Brought to you by IOP
29
37
 
30
38
  <a href="https://www.ideasonpurpose.com"><img src="https://raw.githubusercontent.com/ideasonpurpose/ideasonpurpose/master/iop-logo-white-on-black-88px.png" height="44" align="top" alt="IOP Logo"></a><img src="https://raw.githubusercontent.com/ideasonpurpose/ideasonpurpose/master/spacer.png" align="middle" width="4" height="54"> This project is actively developed and used in production at <a href="https://www.ideasonpurpose.com">Ideas On Purpose</a>.
31
39
 
40
+ <!-- END IOP CREDIT BLURB -->
@@ -15,7 +15,7 @@ const main = async () => {
15
15
  "\n",
16
16
  " 🚀 ",
17
17
  chalk.bold("Local WP site:"),
18
- chalk.magenta(`http://${info.hostname}:${chalk.bold(info.port)}`),
18
+ chalk.yellow(`http://${info.hostname}:${chalk.bold(info.port)}`),
19
19
  "\n",
20
20
  );
21
21
  } else {
package/bin/zip.js ADDED
@@ -0,0 +1,227 @@
1
+ import { readJson, ensureFile } from "fs-extra/esm";
2
+ import { stat } from "node:fs/promises";
3
+
4
+ import { basename, dirname, join } from "node:path/posix";
5
+ import { createReadStream, createWriteStream, statSync } from "node:fs";
6
+
7
+ import url from "url";
8
+ import { fileURLToPath, pathToFileURL } from "node:url";
9
+
10
+ import archiver from "archiver";
11
+ import chalk from "chalk";
12
+ import { cosmiconfig, cosmiconfigSync } from "cosmiconfig";
13
+ import { filesize } from "filesize";
14
+ import { globby } from "globby";
15
+ import isTextPath from "is-text-path";
16
+ import cliTruncate from "cli-truncate";
17
+ import stringLength from "string-length";
18
+ import replaceStream from "replacestream";
19
+
20
+ // import buildConfig from "../lib/buildConfig.js";
21
+ import { buildConfig } from "../index.js";
22
+ // import {dirname} from "node:path";
23
+ import { prettierHrtime } from "../lib/prettier-hrtime.js";
24
+
25
+ // console.log(process.cwd());
26
+ // console.log(import.meta.url);
27
+ // console.log(new URL(import.meta.url));
28
+
29
+ async function getConfig() {
30
+ const siteDir = process.cwd();
31
+ const explorer = cosmiconfig("ideasonpurpose");
32
+ const configFile = await explorer.search(siteDir);
33
+ const config = await buildConfig(configFile);
34
+
35
+ // config.configFileUrl2 = configFile.filepath;
36
+ console.log({ config, configFile });
37
+
38
+ return { config };
39
+ }
40
+ const { config } = await getConfig();
41
+
42
+ config.configFileUrl = new URL("package.json", config.configFileUrl);
43
+ const packageJson = readJson(new URL("package.json", config.configFileUrl));
44
+ packageJson.version = (await packageJson).version ??= "";
45
+
46
+ console.log({ pgk: await packageJson });
47
+ // console.log({ packageJson, env: process.env });
48
+
49
+ const archiveName = (await packageJson).name || "archive";
50
+
51
+ const versionDirName = packageJson.version
52
+ ? `${archiveName}-${packageJson.version}`.replace(/[ .]/g, "_")
53
+ : archiveName;
54
+
55
+ const zipFileName = `${versionDirName}.zip`;
56
+ const zipFile = new URL(`_builds/${zipFileName}`, config.configFileUrl);
57
+ // .pathname;
58
+
59
+ console.log({ zipFile, zipFileName });
60
+ await ensureFile(zipFile.pathname);
61
+ const output = createWriteStream(zipFile);
62
+
63
+ output.on("finish", finishReporter);
64
+
65
+ const archive = archiver("zip", { zlib: { level: 9 } });
66
+ archive.pipe(output);
67
+
68
+ console.log(chalk.bold("Bundling Project for Deployment"));
69
+ const start = process.hrtime();
70
+
71
+ /**
72
+ * Set projectDir to the parent directory of config.src, all bundled
73
+ * files will be found relative to this.
74
+ */
75
+ const projectDir = new URL(`${config.src}/../`, config.configFileUrl);
76
+ // process.exit();
77
+
78
+ /**
79
+ * Counters for total uncompressed size and number of files
80
+ * and a re-usable scoped continuer for file info
81
+ */
82
+ let inBytes = 0;
83
+ let fileCount = 0;
84
+
85
+ const globOpts = { cwd: projectDir.pathname, nodir: false };
86
+ globby(
87
+ [
88
+ "**/*",
89
+ "!_builds",
90
+ "!**/*.sql",
91
+ "!**/node_modules",
92
+ "!CHANGELOG.md",
93
+ "!composer.lock",
94
+ "!coverage*",
95
+ "!docker-compose.yml",
96
+ "!docker-compose.yml",
97
+ "!package-lock.json",
98
+ "!phpunit.xml",
99
+ "!src",
100
+ "!test",
101
+ "!tests",
102
+ ],
103
+ globOpts,
104
+ )
105
+ .then((fileList) => {
106
+ /**
107
+ * Throw an error and bail out if there are no files to zip
108
+ */
109
+ if (!fileList.length) {
110
+ throw new Error("No files found.");
111
+ }
112
+ return fileList;
113
+ })
114
+ .then((fileList) =>
115
+ fileList.map((f) => {
116
+ const file = {
117
+ path: f,
118
+ stat: statSync(new URL(f, projectDir)),
119
+ contents: createReadStream(join(globOpts.cwd, f)),
120
+ };
121
+
122
+ /**
123
+ * Replace the dev folder name with the versioned folder name in hard-coded
124
+ * include paths. These replacements are only run against webpack's compiled
125
+ * assets and Composer's generated autoloaders.
126
+ */
127
+ if (isTextPath(f)) {
128
+ const devPath = new RegExp(`wp-content/themes/${archiveName}/`, "gi");
129
+
130
+ file.contents = file.contents.pipe(
131
+ replaceStream(devPath, `wp-content/themes/${versionDirName}/`),
132
+ );
133
+ }
134
+
135
+ file.contents.on("data", (chunk) => {
136
+ inBytes += chunk.length;
137
+ });
138
+
139
+ file.contents.on("end", () => foundReporter(file));
140
+
141
+ /**
142
+ * Adding a data handler changes a stream's mode from paused to flowing
143
+ * so we need to change it back or the streams will be truncated
144
+ */
145
+ file.contents.pause();
146
+ return file;
147
+ }),
148
+ )
149
+ .then((fileList) =>
150
+ fileList.map((f) =>
151
+ archive.append(f.contents, {
152
+ name: f.path,
153
+ prefix: versionDirName,
154
+ }),
155
+ ),
156
+ )
157
+ .then(() => archive.finalize())
158
+ .catch(console.error);
159
+
160
+ function foundReporter(file) {
161
+ fileCount += 1;
162
+ let outString = [
163
+ "🔍 ",
164
+ chalk.yellow("Found"),
165
+ chalk.magenta(fileCount),
166
+ chalk.yellow("files..."),
167
+ chalk.gray(`(Uncompressed: ${filesize(inBytes)}) `),
168
+ ].join(" ");
169
+
170
+ /**
171
+ * calculate width of terminal then shorten paths
172
+ */
173
+ const cols = process.stdout.columns - stringLength(outString) - 1;
174
+ outString += chalk.blue(cliTruncate(file.path, cols, { position: "middle" }));
175
+
176
+ if (fileCount % 25 == 0) {
177
+ process.stdout.clearLine();
178
+ process.stdout.cursorTo(0);
179
+ process.stdout.write(outString);
180
+ }
181
+ }
182
+
183
+ function finishReporter() {
184
+ const outBytes = archive.pointer();
185
+ const end = process.hrtime(start);
186
+ const duration = prettierHrtime(end);
187
+ const savedBytes = inBytes - outBytes;
188
+ const savedPercent = ((1 - outBytes / inBytes) * 100).toFixed(2);
189
+
190
+ process.stdout.clearLine();
191
+ process.stdout.cursorTo(0);
192
+
193
+ console.log(
194
+ "🔍 ",
195
+ chalk.yellow("Found"),
196
+ chalk.magenta(fileCount),
197
+ chalk.yellow("files"),
198
+ chalk.gray(`(Uncompressed: ${filesize(inBytes)})`),
199
+ );
200
+ console.log(
201
+ "👀 ",
202
+ chalk.yellow("Webpack Bundle Analyzer report:"),
203
+ chalk.magenta("webpack/stats/index.html"),
204
+ );
205
+ console.log(
206
+ "📦 ",
207
+ chalk.yellow("Created"),
208
+ chalk.magenta(filesize(outBytes)),
209
+ chalk.yellow("Zip archive"),
210
+ chalk.gray(`(Saved ${filesize(savedBytes)}, ${savedPercent}%)`),
211
+ );
212
+ console.log(
213
+ "🎁 ",
214
+ chalk.yellow("Theme archive"),
215
+ chalk.magenta(
216
+ `${basename(dirname(zipFile.pathname))}/${chalk.bold(zipFileName)}`,
217
+ ),
218
+ chalk.yellow("created in"),
219
+ chalk.magenta(duration),
220
+ );
221
+ console.log("⏳");
222
+ console.log(
223
+ "🚀 ",
224
+ chalk.bold(`Remember to push to ${chalk.cyan("GitHub!")}`),
225
+ );
226
+ console.log("✨");
227
+ }