mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
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.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -45,6 +45,7 @@ access.log
|
|||||||
/vectors/
|
/vectors/
|
||||||
/cache/
|
/cache/
|
||||||
public/css/user.css
|
public/css/user.css
|
||||||
|
public/error/
|
||||||
/plugins/
|
/plugins/
|
||||||
/data
|
/data
|
||||||
/default/scaffold
|
/default/scaffold
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
* Scripts to be done before starting the server for the first time.
|
* Scripts to be done before starting the server for the first time.
|
||||||
*/
|
*/
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path/posix'; // Windows can handle Unix paths, but not vice-versa
|
||||||
import crypto from 'node:crypto';
|
import crypto from 'node:crypto';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import yaml from 'yaml';
|
import yaml from 'yaml';
|
||||||
@@ -213,20 +213,60 @@ function addMissingConfigValues() {
|
|||||||
* Creates the default config files if they don't exist yet.
|
* Creates the default config files if they don't exist yet.
|
||||||
*/
|
*/
|
||||||
function createDefaultFiles() {
|
function createDefaultFiles() {
|
||||||
const files = {
|
/**
|
||||||
config: './config.yaml',
|
* @typedef DefaultItem
|
||||||
user: './public/css/user.css',
|
* @type {object}
|
||||||
};
|
* @property {'file' | 'directory'} type - Whether the item should be copied as a single file or merged into a directory structure.
|
||||||
|
* @property {string} defaultPath - The path to the default item (typically in `default/`).
|
||||||
|
* @property {string} productionPath - The path to the copied item for production use.
|
||||||
|
*/
|
||||||
|
|
||||||
for (const file of Object.values(files)) {
|
/** @type {DefaultItem[]} */
|
||||||
|
const defaultItems = [
|
||||||
|
{
|
||||||
|
type: 'file',
|
||||||
|
defaultPath: './default/config.yaml',
|
||||||
|
productionPath: './config.yaml',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'directory',
|
||||||
|
defaultPath: './default/public/',
|
||||||
|
productionPath: './public/',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const defaultItem of defaultItems) {
|
||||||
try {
|
try {
|
||||||
if (!fs.existsSync(file)) {
|
if (defaultItem.type === 'file') {
|
||||||
const defaultFilePath = path.join('./default', path.parse(file).base);
|
if (!fs.existsSync(defaultItem.productionPath)) {
|
||||||
fs.copyFileSync(defaultFilePath, file);
|
fs.copyFileSync(
|
||||||
console.log(color.green(`Created default file: ${file}`));
|
defaultItem.defaultPath,
|
||||||
|
defaultItem.productionPath,
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
color.green(`Created default file: ${defaultItem.productionPath}`),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if (defaultItem.type === 'directory') {
|
||||||
|
fs.cpSync(defaultItem.defaultPath, defaultItem.productionPath, {
|
||||||
|
force: false, // Don't overwrite existing files!
|
||||||
|
recursive: true,
|
||||||
|
});
|
||||||
|
console.log(
|
||||||
|
color.green(`Synchronized missing files: ${defaultItem.productionPath}`),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw new Error(
|
||||||
|
'FATAL: Unexpected default file format in `post-install.js#createDefaultFiles()`.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(color.red(`FATAL: Could not write default file: ${file}`), error);
|
console.error(
|
||||||
|
color.red(
|
||||||
|
`FATAL: Could not write default ${defaultItem.type}: ${defaultItem.productionPath}`,
|
||||||
|
),
|
||||||
|
error,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user