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 -11Ignore whitespace
.gitignore+1 -0
@@ -45,6 +45,7 @@ access.log
45/vectors/45/vectors/
46/cache/46/cache/
47public/css/user.css47public/css/user.css
48public/error/
48/plugins/49/plugins/
49/data50/data
50/default/scaffold51/default/scaffold
default/user.css → default/public/css/user.css+0 -0
post-install.js+51 -11
@@ -2,7 +2,7 @@
2 * Scripts to be done before starting the server for the first time.2 * Scripts to be done before starting the server for the first time.
3 */3 */
4import fs from 'node:fs';4import fs from 'node:fs';
5import path from 'node:path';5import path from 'node:path/posix'; // Windows can handle Unix paths, but not vice-versa
6import crypto from 'node:crypto';6import crypto from 'node:crypto';
7import process from 'node:process';7import process from 'node:process';
8import yaml from 'yaml';8import yaml from 'yaml';
@@ -213,20 +213,60 @@ function addMissingConfigValues() {
213 * Creates the default config files if they don't exist yet.213 * Creates the default config files if they don't exist yet.
214 */214 */
215function createDefaultFiles() {215function 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 */
220223
221 for (const file of Object.values(files)) {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 ];
237
238 for (const defaultItem of defaultItems) {
222 try {239 try {
223 if (!fs.existsSync(file)) {240 if (defaultItem.type === 'file') {
224 const defaultFilePath = path.join('./default', path.parse(file).base);241 if (!fs.existsSync(defaultItem.productionPath)) {
225 fs.copyFileSync(defaultFilePath, file);242 fs.copyFileSync(
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 );
227 }262 }
228 } catch (error) {263 } 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 );
230 }270 }
231 }271 }
232}272}