first commit
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
import {
|
||||
appDataDir,
|
||||
join,
|
||||
basename as pathBasename,
|
||||
dirname as pathDirname
|
||||
} from '@tauri-apps/api/path';
|
||||
import { stat, mkdir, readDir, copyFile, rename, remove } from '@tauri-apps/plugin-fs';
|
||||
import { open } from '@tauri-apps/plugin-dialog';
|
||||
import baseicon from '$lib/assets/baseicon.png';
|
||||
import { BASE_LIBRARY_DIR } from './settings';
|
||||
|
||||
/**
|
||||
* Create a custom icon with text overlay
|
||||
* @param {string} [iconText] - Text to overlay on the icon
|
||||
* @returns {Promise<Blob>} Promise resolving to a PNG blob of the icon
|
||||
*/
|
||||
export async function createIcon(iconText) {
|
||||
// Create canvas element
|
||||
const canvas = document.createElement('canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
// Load the base icon image
|
||||
const img = new Image();
|
||||
img.src = baseicon;
|
||||
|
||||
// Wait for image to load
|
||||
await new Promise((resolve) => {
|
||||
img.onload = resolve;
|
||||
});
|
||||
|
||||
// Set canvas dimensions to match image
|
||||
canvas.width = img.width;
|
||||
canvas.height = img.height;
|
||||
|
||||
// Draw the base icon
|
||||
ctx.drawImage(img, 0, 0);
|
||||
|
||||
// Set text properties
|
||||
ctx.fillStyle = '#7d70ba';
|
||||
ctx.textAlign = 'left';
|
||||
ctx.textBaseline = 'top';
|
||||
|
||||
// Load the custom font
|
||||
const fontFace = new FontFace(
|
||||
'Europa-Mono',
|
||||
`url(${baseicon.replace('baseicon.png', 'fonts/Europa-Mono-Medium.otf')})`
|
||||
);
|
||||
await fontFace.load();
|
||||
document.fonts.add(fontFace);
|
||||
|
||||
// Set font size based on canvas dimensions
|
||||
const fontSize = canvas.width / 6;
|
||||
ctx.font = `${fontSize}px Europa-Mono`;
|
||||
// Set font size based on canvas dimensions
|
||||
|
||||
// Add version text in the center
|
||||
const text = iconText || 'XXX';
|
||||
ctx.fillText(text, 150, 150);
|
||||
|
||||
// Convert canvas to blob
|
||||
return new Promise((resolve) => {
|
||||
canvas.toBlob((blob) => {
|
||||
resolve(blob);
|
||||
}, 'image/png');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create library directory structure if it doesn't exist
|
||||
* @param {string} libraryDir - Library directory path
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function ensureLibraryStructure(libraryDir) {
|
||||
try {
|
||||
// Create main library directory
|
||||
const baseDir = await join(libraryDir, BASE_LIBRARY_DIR);
|
||||
// Create blender subdirectory
|
||||
const blenderDir = await join(baseDir, 'blender');
|
||||
await mkdir(blenderDir, { recursive: true });
|
||||
|
||||
// Create templates subdirectory
|
||||
const templatesDir = await join(baseDir, 'templates');
|
||||
await mkdir(templatesDir, { recursive: true });
|
||||
|
||||
const configDir = await join(baseDir, 'config');
|
||||
await mkdir(configDir, { recursive: true });
|
||||
|
||||
console.log('Library structure created at:', libraryDir);
|
||||
} catch (error) {
|
||||
console.error('Error creating library structure:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a directory picker dialog for selecting a directory.
|
||||
* @param {string} [currentPath] - Current path to use as default
|
||||
* @returns {Promise<string|null>} Selected path or null if cancelled
|
||||
*/
|
||||
export async function selectDirectory(currentPath) {
|
||||
try {
|
||||
// Use currentPath if provided, otherwise use app data directory
|
||||
let defaultPath = await appDataDir();
|
||||
if (currentPath) {
|
||||
defaultPath = currentPath;
|
||||
}
|
||||
|
||||
const selected = await open({
|
||||
multiple: false,
|
||||
recursive: true,
|
||||
directory: true,
|
||||
title: 'Select Library Directory',
|
||||
defaultPath
|
||||
});
|
||||
|
||||
return selected;
|
||||
} catch (error) {
|
||||
console.error('Error opening directory picker:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a directory exists
|
||||
* @param {string} path - Directory path to check
|
||||
* @returns {Promise<boolean>} True if directory exists
|
||||
*/
|
||||
export async function directoryExists(path) {
|
||||
try {
|
||||
const stats = await stat(path);
|
||||
return stats.isDirectory;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a directory recursively if it doesn't exist
|
||||
* @param {string} path - Directory path to create
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function ensureDirectory(path) {
|
||||
try {
|
||||
await mkdir(path, { recursive: true });
|
||||
} catch (error) {
|
||||
console.error(`Error creating directory ${path}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the basename of a path (file or directory name)
|
||||
* @param {string} path - Full path
|
||||
* @returns {Promise<string>} Basename
|
||||
*/
|
||||
export async function basename(path) {
|
||||
return pathBasename(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the directory name of a path
|
||||
* @param {string} path - Full path
|
||||
* @returns {Promise<string>} Directory name
|
||||
*/
|
||||
export async function dirname(path) {
|
||||
return pathDirname(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a file or directory recursively
|
||||
* @param {string} source - Source path
|
||||
* @param {string} destination - Destination path
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function copyPath(source, destination) {
|
||||
try {
|
||||
const statInfo = await stat(source);
|
||||
if (statInfo.isDirectory) {
|
||||
// Create destination directory
|
||||
await mkdir(destination, { recursive: true });
|
||||
// Read all entries in source
|
||||
const entries = await readDir(source);
|
||||
for (const entry of entries) {
|
||||
const srcPath = await join(source, entry.name);
|
||||
const destPath = await join(destination, entry.name);
|
||||
await copyPath(srcPath, destPath); // recursive call
|
||||
}
|
||||
} else {
|
||||
// It's a file, copy directly
|
||||
await copyFile(source, destination);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error copying ${source} to ${destination}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move or rename a file or directory
|
||||
* @param {string} source - Source path
|
||||
* @param {string} destination - Destination path
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function movePath(source, destination) {
|
||||
try {
|
||||
await rename(source, destination);
|
||||
} catch (error) {
|
||||
console.error(`Error moving ${source} to ${destination}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip top-level directory if the target directory contains only one subdirectory.
|
||||
* This is useful after extracting archives that create an unnecessary parent folder.
|
||||
* @param {string} targetDir - Directory to check and potentially flatten
|
||||
* @returns {Promise<boolean>} True if flattening was performed, false otherwise
|
||||
*/
|
||||
export async function stripTopLevelDirectory(targetDir) {
|
||||
try {
|
||||
// Read contents of target directory
|
||||
const entries = await readDir(targetDir);
|
||||
|
||||
// If there's exactly one entry and it's a directory, flatten it
|
||||
if (entries.length === 1 && entries[0].isDirectory) {
|
||||
const singleDirName = entries[0].name;
|
||||
const singleDirPath = await join(targetDir, singleDirName);
|
||||
|
||||
// Read contents of the single subdirectory
|
||||
const subEntries = await readDir(singleDirPath);
|
||||
|
||||
// Move all items from subdirectory to target directory
|
||||
for (const entry of subEntries) {
|
||||
const sourcePath = await join(singleDirPath, entry.name);
|
||||
const destPath = await join(targetDir, entry.name);
|
||||
await rename(sourcePath, destPath);
|
||||
}
|
||||
|
||||
// Remove the now-empty subdirectory
|
||||
await remove(singleDirPath, { recursive: true });
|
||||
|
||||
console.log(`Flattened directory structure: removed ${singleDirName}/`);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error(`Error stripping top-level directory in ${targetDir}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user