UNPKG

fs-utils-sync

Version:

The fs-utils-sync package provides a collection of well-tested, synchronous file system utility functions. It promotes consistency and readability across projects by providing a unified approach to common file operations, saving you development time and i

1 lines 4.83 kB
import{Buffer}from"node:buffer";import{accessSync,lstatSync,mkdirSync,rmSync,writeFileSync,readFileSync,symlinkSync,unlinkSync,copyFileSync,cpSync,readdirSync}from"node:fs";import{basename,extname,dirname}from"node:path";import{encodeError,extractMessage}from"error-message-utils";import{sortRecords}from"web-utils-kit";import{ERRORS}from"./shared/errors.js";import{buildDirectoryElementsOptions}from"./utils/index.js";const pathExists=e=>{try{return accessSync(e),!0}catch(e){return!1}},getPathElement=e=>{try{const r=lstatSync(e);return{path:e,baseName:basename(e),extName:extname(e),isFile:r.isFile(),isDirectory:r.isDirectory(),isSymbolicLink:r.isSymbolicLink(),size:r.size,creation:Math.round(r.birthtimeMs)}}catch(e){return null}},isDirectory=e=>{const r=getPathElement(e);return null!==r&&r.isDirectory},deleteDirectory=e=>rmSync(e,{recursive:!0,force:!0}),createDirectory=(e,r)=>{if(isDirectory(e)){if(!r)throw new Error(encodeError(`The directory ${e} already exists.`,ERRORS.DIRECTORY_ALREADY_EXISTS));deleteDirectory(e)}mkdirSync(e,{recursive:!0})},copyDirectory=(e,r)=>{if(!isDirectory(e))throw new Error(encodeError(`The srcPath '${e}' is not a directory.`,ERRORS.NOT_A_DIRECTORY));deleteDirectory(r),cpSync(e,r,{recursive:!0})},createDirectorySymLink=(e,r)=>{const t=getPathElement(e);if(null===t)throw new Error(encodeError(`The target dir '${e}' does not exist.`,ERRORS.NON_EXISTENT_DIRECTORY));if(!t.isDirectory)throw new Error(encodeError(`The target dir '${e}' is not a directory.`,ERRORS.NOT_A_DIRECTORY));symlinkSync(e,r,"dir")},readDirectory=(e,r=!1)=>{if(!isDirectory(e))throw new Error(encodeError(`The dir '${e}' is not a directory.`,ERRORS.NOT_A_DIRECTORY));return readdirSync(e,{encoding:"utf-8",recursive:r}).map((r=>`${e}/${r}`))},getDirectoryElements=(e,r)=>{if(!isDirectory(e))throw new Error(encodeError(`The path '${e} is not a directory'`,ERRORS.NOT_A_DIRECTORY));const t=buildDirectoryElementsOptions(r),i=[],o=[],n=[];return readDirectory(e).map((e=>getPathElement(e))).forEach((e=>{e.isDirectory?i.push(e):!e.isFile||t.includeExts.length&&!t.includeExts.includes(e.extName.toLowerCase())?e.isSymbolicLink&&n.push(e):o.push(e)})),i.sort(sortRecords(t.sortByKey,t.sortOrder)),o.sort(sortRecords(t.sortByKey,t.sortOrder)),n.sort(sortRecords(t.sortByKey,t.sortOrder)),{directories:i,files:o,symbolicLinks:n}},isFile=e=>{const r=getPathElement(e);return null!==r&&r.isFile},writeFile=(e,r,t)=>{const i=dirname(e);pathExists(i)||createDirectory(i),writeFileSync(e,r,t)},writeTextFile=(e,r)=>{if("string"!=typeof r||!r.length)throw new Error(encodeError(`The provided data for the file '${e}' is empty or invalid. Received: ${r}`,ERRORS.FILE_CONTENT_IS_EMPTY_OR_INVALID));writeFile(e,r,{encoding:"utf-8"})},writeJSONFile=(e,r,t=2)=>{let i;try{i="string"==typeof r?r:JSON.stringify(r,void 0,t)}catch(r){throw new Error(encodeError(`The JSON data for the file '${e}' could not be stringified: ${extractMessage(r)}`,ERRORS.FILE_CONTENT_IS_EMPTY_OR_INVALID))}writeTextFile(e,i)},writeBufferFile=(e,r)=>{if(!Buffer.isBuffer(r))throw new Error(encodeError(`The provided data is not a valid Buffer. Received: ${r}`,ERRORS.FILE_CONTENT_IS_EMPTY_OR_INVALID));writeFile(e,r)},readFile=(e,r=null)=>{if(!isFile(e))throw new Error(encodeError(`The file '${e}' is not a file.`,ERRORS.NOT_A_FILE));return readFileSync(e,r)},readTextFile=e=>{const r=readFile(e,{encoding:"utf-8"});if("string"!=typeof r||!r.length)throw new Error(encodeError(`The file '${e}' is empty or invalid.`,ERRORS.FILE_CONTENT_IS_EMPTY_OR_INVALID));return r},readJSONFile=e=>{const r=readTextFile(e);try{return JSON.parse(r)}catch(r){throw new Error(encodeError(`The JSON file '${e}' cound not be parsed: ${extractMessage(r)}`,ERRORS.FILE_CONTENT_IS_EMPTY_OR_INVALID))}},readBufferFile=e=>{const r=readFile(e,null);if(!Buffer.isBuffer(r)||!r.toString().length)throw new Error(encodeError(`The file '${e}' is not a valid Buffer. Received: ${r}`,ERRORS.FILE_CONTENT_IS_EMPTY_OR_INVALID));return r},copyFile=(e,r)=>{if(!isFile(e))throw new Error(encodeError(`The file '${e}' is not a file.`,ERRORS.NOT_A_FILE));copyFileSync(e,r)},deleteFile=e=>{if(!isFile(e))throw new Error(encodeError(`The file '${e} is not a file.'`,ERRORS.NOT_A_FILE));unlinkSync(e)},createFileSymLink=(e,r)=>{const t=getPathElement(e);if(null===t)throw new Error(encodeError(`The target file '${e}' does not exist.`,ERRORS.NON_EXISTENT_FILE));if(!t.isFile)throw new Error(encodeError(`The target file '${e}' is not a file.`,ERRORS.NOT_A_FILE));symlinkSync(e,r,"file")};export{pathExists,getPathElement,isDirectory,createDirectory,copyDirectory,deleteDirectory,createDirectorySymLink,readDirectory,getDirectoryElements,isFile,writeFile,writeTextFile,writeJSONFile,writeBufferFile,readFile,readTextFile,readJSONFile,readBufferFile,copyFile,deleteFile,createFileSymLink};