UNPKG

preferred-node-version

Version:

Get the preferred Node.js version of a project or user

58 lines (38 loc) 984 B
import{readFile}from"node:fs/promises"; import{basename}from"node:path"; import{loadNodeEnvRc}from"./nodeenv.js"; import{loadPackageJson}from"./package.js"; export const loadVersionFile=async(filePath)=>{ const content=await safeReadFile(filePath); const contentA=content.trim(); if(contentA===""){ return } const filename=basename(filePath); const loadFunction=LOAD_FUNCTIONS[filename]; if(loadFunction===undefined){ throw new Error( `Option "files" is invalid: ${filename} Allowed values are: ${NODE_VERSION_FILES.join(", ")}` ) } const rawVersion=loadFunction(contentA); return rawVersion }; const safeReadFile=async(path)=>{ try{ return await readFile(path,"utf8") }catch{ return"" } }; const identity=(content)=>content; const LOAD_FUNCTIONS={ ".n-node-version":identity, ".naverc":identity, ".node-version":identity, ".nodeenvrc":loadNodeEnvRc, ".nvmrc":identity, "package.json":loadPackageJson }; export const NODE_VERSION_FILES=Object.keys(LOAD_FUNCTIONS);