refactor: load project

This commit is contained in:
Carlos Valente
2024-07-02 16:52:18 +02:00
committed by Carlos Valente
parent 9c5e403b18
commit 751c3329f0
11 changed files with 153 additions and 87 deletions
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { ensureJsonExtension } from '../fileManagement.js';
import { appendToName, ensureJsonExtension } from '../fileManagement.js';
describe('ensureJsonExtension', () => {
it('should add .json to a filename without an extension', () => {
@@ -26,3 +26,26 @@ describe('ensureJsonExtension', () => {
expect(result).toBe('my.test.file.json');
});
});
describe('appendToName', () => {
it('appends a given string to a file name', () => {
const filename = 'file.json';
const append = '(recovered)';
const result = appendToName(filename, append);
expect(result).toBe('file (recovered).json');
});
it('handles paths', () => {
const path = '/Users/carlos/Library/Application Support/Ontime/projects/file.json';
const append = '(recovered)';
const result = appendToName(path, append);
expect(result).toBe('/Users/carlos/Library/Application Support/Ontime/projects/file (recovered).json');
});
it('handles multiple . in string', () => {
const path = 'strange.file.name.json';
const append = '(recovered)';
const result = appendToName(path, append);
expect(result).toBe('strange.file.name (recovered).json');
});
});
+10 -1
View File
@@ -28,7 +28,7 @@ export function ensureJsonExtension(filename: string | undefined): string | unde
* Lists all files in a directory
*/
export async function getFilesFromFolder(folderPath: string): Promise<string[]> {
return await readdir(folderPath);
return readdir(folderPath);
}
/**
@@ -38,3 +38,12 @@ export async function getFilesFromFolder(folderPath: string): Promise<string[]>
export const removeFileExtension = (filename: string): string => {
return parse(filename).name;
};
/**
* Appends a given string to a file name or path
* @example appendToName('file.json', '(recovered)') => 'file (recovered).json'
*/
export function appendToName(filePath: string, append: string): string {
const extension = filePath.split('.').pop();
return filePath.replace(`.${extension}`, ` ${append}.${extension}`);
}
+1 -13
View File
@@ -1,11 +1,10 @@
import multer from 'multer';
import path from 'path';
import fs from 'fs';
import { rename, rm } from 'fs/promises';
import { rm } from 'fs/promises';
import { ensureDirectory } from './fileManagement.js';
import { getAppDataPath, uploadsFolderPath } from '../setup/index.js';
import { deleteFile } from './parserUtils.js';
function generateNewFileName(filePath: string, callback: (newName: string) => void) {
const baseName = path.basename(filePath, path.extname(filePath));
@@ -69,14 +68,3 @@ export async function clearUploadfolder() {
// we dont care that there was no folder
}
}
/**
* Copies a file from upload folder to a destination
* @param filePath
* @param name
* @returns
*/
export async function moveUploadedFile(fromUpload: string, toDestination: string) {
await rename(fromUpload, toDestination);
await deleteFile(fromUpload);
}