| 213 | 213 | * Creates the default config files if they don't exist yet. |
| 214 | 214 | */ |
| 215 | 215 | function 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 | + ]; |
| 220 | 237 | |
| 221 | 238 | for (const filedefaultItem of Object.values(files)defaultItems) { |
| 222 | 239 | try { |
| 223 | 240 | if (!fsdefaultItem.existsSync(type === 'file)') { |
| 224 | | - const defaultFilePath = path.join('./default', path.parse(file).base); |
| 241 | + if (!fs.existsSync(defaultItem.productionPath)) { |
| 225 | 242 | 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 | + ); |
| 227 | 262 | } |
| 228 | 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 | } |