UNPKG

amiddy

Version:

Middleware server with high configurability for development

1 lines 784 B
import fs from"fs";import path from"path";import logger from"./logger.js";const privateApi={isFile:filePath=>fs.existsSync(filePath)&&!fs.lstatSync(filePath).isDirectory(),onSaveCb:err=>{err&&logger.error(err.message,"file-write")}},service={};service.write=(filePath,content)=>{const sanitizedPath=filePath.replace(/\/\//,"/");fs.writeFile(sanitizedPath,content,privateApi.onSaveCb)},service.read=filePath=>fs.readFileSync(filePath,"utf8").replace(/^\ufeff/u,""),service.getAbsolutePath=pathToResolve=>{const cwd=process.cwd(),absolutePath=path.isAbsolute(pathToResolve)?pathToResolve:path.resolve(cwd,pathToResolve);if(privateApi.isFile(absolutePath))return absolutePath;throw Error(` Cannot resolve file: ${pathToResolve}\n cwd: ${cwd}`)};export{privateApi};export default service;