🌳
pt0/serverF/libFs/libFs.mts
1import fs, { promises } from 'fs'
2import { fs1Promises as fsPromises } from './fsPromisesF.mts'
3import type { absFileDirPath } from '../../ptDirF.mts'
6const fsCpDel = async (fromPath : absFileDirPath, toPath : absFileDirPath) => {
7 try {
8 await fsPromises.cp(fromPath, toPath)
9 await fsPromises.unlink(fromPath)
10 } catch (err) {
11 console.trace(err, {fromPath, toPath})
12 process.exit(1)
13 }
16export const fsCp = async (fromPath : absFileDirPath, toPath : absFileDirPath) => {
17 assertDefined(fromPath)
19 if (fromPath == toPath) return
20 await fsPromises.cp(fromPath, toPath)
23export const fsRename = async (fromPath : absFileDirPath, toPath : absFileDirPath) => {
24 try {
25 await fsPromises.rename(fromPath, toPath)
26 } catch (err : unknown) {
27 if ((err as NodeJS.ErrnoException).code == 'EXDEV') {
28 console.log('warn, cp & delete workaround for cross-disk mv', fromPath, toPath)
29 await fsCpDel(fromPath, toPath)
30 return
31 }
32 console.trace(err, {fromPath, toPath})
33 process.exit(1)
34 }
37export const read1FileIfExist = async (filePath : absFileDirPath) => {
38 try {
39 return await promises.readFile(filePath, 'utf-8')
40 } catch (err : unknown) {
41 if ((err as NodeJS.ErrnoException).code == 'ENOENT') return false
42 throw err
43 }