|
| 1 | +var fs = require('fs'); |
| 2 | +var path = require('path'); |
| 3 | + |
| 4 | +var REFERENCE_FILE_NAME = 'package.json'; |
| 5 | +var REFERENCE_PROJECT_NAME = 'countly-server'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Find a file from a base directory by walking up the hierarchy. |
| 9 | + * @param {string} root The reference path to start lookup from. |
| 10 | + * @param {string} fileToSearch The name of the file to search. |
| 11 | + * @returns {string} The path where the file to search as been found. |
| 12 | + */ |
| 13 | +function findReferenceFile(root, fileToSearch) { |
| 14 | + var prevRoot; |
| 15 | + |
| 16 | + do { |
| 17 | + var filename = path.join(root, fileToSearch); |
| 18 | + if (fs.existsSync(filename) && fs.statSync(filename).isFile()) { |
| 19 | + return root; |
| 20 | + } |
| 21 | + prevRoot = root; |
| 22 | + root = path.dirname(root); |
| 23 | + } while (prevRoot !== root); |
| 24 | + |
| 25 | + return null; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Get the project name from a package.json file. |
| 30 | + * @param {string} jsonFilename The package.json full filename. |
| 31 | + * @returns {string} The name of the package (property "name" of the package.json). |
| 32 | + */ |
| 33 | +function getProjectName(jsonFilename) { |
| 34 | + try { |
| 35 | + return JSON.parse(fs.readFileSync(jsonFilename)).name; |
| 36 | + } |
| 37 | + catch (error) { |
| 38 | + return null; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Find the path that contains a file REFERENCE_FILE_NAME with |
| 44 | + * project name REFERENCE_PROJECT_NAME, from __dirname path. |
| 45 | + * @returns {string} The absolute path of Countly. |
| 46 | + */ |
| 47 | +function findReferencePath() { |
| 48 | + var basePath = __dirname; |
| 49 | + var asLongAsNeeded = true; |
| 50 | + |
| 51 | + while (asLongAsNeeded) { |
| 52 | + // Look-up for the closest reference file in the hierarchy. |
| 53 | + |
| 54 | + var referencePath = findReferenceFile(basePath, REFERENCE_FILE_NAME); |
| 55 | + |
| 56 | + if (!referencePath) { |
| 57 | + throw new Error('Imposible to find "' + REFERENCE_FILE_NAME + '" from "' + basePath + '"'); |
| 58 | + } |
| 59 | + |
| 60 | + // Make sure this is the reference file we are looking for. |
| 61 | + |
| 62 | + var projectName = getProjectName(path.join(referencePath, REFERENCE_FILE_NAME)); |
| 63 | + |
| 64 | + if (projectName === REFERENCE_PROJECT_NAME) { |
| 65 | + return referencePath; |
| 66 | + } |
| 67 | + |
| 68 | + // If not, keep looking up the hierarchy. |
| 69 | + |
| 70 | + basePath = path.dirname(referencePath); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +var referencePath = findReferencePath(); |
| 75 | + |
| 76 | +/** |
| 77 | + * Resolves the given relative path to absolute from Countly repository root path. |
| 78 | + * @param {string} relativePath The path relative to Countly root directory. |
| 79 | + * @returns {string} The absolute path resolved for the given relative path. |
| 80 | + */ |
| 81 | +function fromCountlyRoot(relativePath) { |
| 82 | + return path.join(referencePath, relativePath); |
| 83 | +} |
| 84 | + |
| 85 | +module.exports = fromCountlyRoot; |
0 commit comments