fs.helpers.ts 1000 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import fs from 'fs-extra';
  2. export const readJsonFile = (path: string): unknown | null => {
  3. try {
  4. const rawFile = fs.readFileSync(path).toString();
  5. return JSON.parse(rawFile);
  6. } catch (e) {
  7. return null;
  8. }
  9. };
  10. export const readFile = (path: string): string => {
  11. try {
  12. return fs.readFileSync(path).toString();
  13. } catch {
  14. return '';
  15. }
  16. };
  17. export const readdirSync = (path: string): string[] => fs.readdirSync(path);
  18. export const fileExists = (path: string): boolean => fs.existsSync(path);
  19. export const writeFile = (path: string, data: string) => fs.writeFileSync(path, data);
  20. export const createFolder = (path: string) => {
  21. if (!fileExists(path)) {
  22. fs.mkdirSync(path, { recursive: true });
  23. }
  24. };
  25. export const deleteFolder = (path: string) => fs.rmSync(path, { recursive: true });
  26. export const getSeed = () => {
  27. const seed = readFile('/runtipi/state/seed');
  28. return seed.toString();
  29. };
  30. export const unlinkFile = (path: string) => fs.promises.unlink(path);