rework `createDefaultFiles()` Reorganised copy-able `default/` files as a sparse copy of the production file-tree. This should save the `defaultItems` (formerly `files`) array from getting unwieldy.

e07faea874bf201e65aa442a5136dcd27ef8e414

Spappz <34202141+Spappz@users.noreply.github.com>

Signed
3 files changed, +52 -11Showing whitespace changes
.gitignore+1 -0
@@ -45,6 +45,7 @@ access.log
4545/vectors/
4646/cache/
4747public/css/user.css
48+public/error/
4849/plugins/
4950/data
5051/default/scaffold
default/user.css → default/public/css/user.css+0 -0
post-install.js+51 -11
@@ -2,7 +2,7 @@
22 * Scripts to be done before starting the server for the first time.
33 */
44import fs from 'node:fs';
55import path from 'node:path/posix'; // Windows can handle Unix paths, but not vice-versa
66import crypto from 'node:crypto';
77import process from 'node:process';
88import yaml from 'yaml';
@@ -213,20 +213,60 @@ function addMissingConfigValues() {
213213 * Creates the default config files if they don't exist yet.
214214 */
215215function createDefaultFiles() {
216- const files = {
216+ /**
217- config: './config.yaml',
217+ * @typedef DefaultItem
218- user: './public/css/user.css',
218+ * @type {object}
219- };
219+ * @property {'file' | 'directory'} type - Whether the item should be copied as a single file or merged into a directory structure.
220+ * @property {string} defaultPath - The path to the default item (typically in `default/`).
221+ * @property {string} productionPath - The path to the copied item for production use.
222+ */
223+
224+ /** @type {DefaultItem[]} */
225+ const defaultItems = [
226+ {
227+ type: 'file',
228+ defaultPath: './default/config.yaml',
229+ productionPath: './config.yaml',
230+ },
231+ {
232+ type: 'directory',
233+ defaultPath: './default/public/',
234+ productionPath: './public/',
235+ },
236+ ];
220237
221238 for (const filedefaultItem of Object.values(files)defaultItems) {
222239 try {
223240 if (!fsdefaultItem.existsSync(type === 'file)') {
224- const defaultFilePath = path.join('./default', path.parse(file).base);
241+ if (!fs.existsSync(defaultItem.productionPath)) {
225242 fs.copyFileSync(defaultFilePath, file);
226- console.log(color.green(`Created default file: ${file}`));
243+ defaultItem.defaultPath,
244+ defaultItem.productionPath,
245+ );
246+ console.log(
247+ color.green(`Created default file: ${defaultItem.productionPath}`),
248+ );
249+ }
250+ } else if (defaultItem.type === 'directory') {
251+ fs.cpSync(defaultItem.defaultPath, defaultItem.productionPath, {
252+ force: false, // Don't overwrite existing files!
253+ recursive: true,
254+ });
255+ console.log(
256+ color.green(`Synchronized missing files: ${defaultItem.productionPath}`),
257+ );
258+ } else {
259+ throw new Error(
260+ 'FATAL: Unexpected default file format in `post-install.js#createDefaultFiles()`.',
261+ );
227262 }
228263 } catch (error) {
229- console.error(color.red(`FATAL: Could not write default file: ${file}`), error);
264+ console.error(
265+ color.red(
266+ `FATAL: Could not write default ${defaultItem.type}: ${defaultItem.productionPath}`,
267+ ),
268+ error,
269+ );
230270 }
231271 }
232272}