From 5406472d651e4f27a26e6443c4117aa9bedb9c6b Mon Sep 17 00:00:00 2001 From: muxator Date: Wed, 22 Aug 2018 01:14:30 +0200 Subject: [PATCH] AbsolutePaths: makeAbsolute() computes an absolute path from a relative one The base is assumed to be exports.findEtherpadRoot(), without depending on process.cwd. --- src/node/utils/AbsolutePaths.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/node/utils/AbsolutePaths.js b/src/node/utils/AbsolutePaths.js index 3a7e60f0d..6d67bd15d 100644 --- a/src/node/utils/AbsolutePaths.js +++ b/src/node/utils/AbsolutePaths.js @@ -114,3 +114,24 @@ exports.findEtherpadRoot = function() { absPathLogger.error(`To run, Etherpad has to identify an absolute base path. This is not: "${etherpadRoot}"`); process.exit(1); }; + +/** + * Receives a filesystem path in input. If the path is absolute, returns it + * unchanged. If the path is relative, an absolute version of it is returned, + * built prepending exports.findEtherpadRoot() to it. + * + * @param {string} somePath - an absolute or relative path + * @return {string} An absolute path. If the input path was already absolute, + * it is returned unchanged. Otherwise it is interpreted + * relative to exports.root. + */ +exports.makeAbsolute = function(somePath) { + if (path.isAbsolute(somePath)) { + return somePath; + } + + const rewrittenPath = path.normalize(path.join(exports.findEtherpadRoot(), somePath)); + + absPathLogger.debug(`Relative path "${somePath}" can be rewritten to "${rewrittenPath}"`); + return rewrittenPath; +};