Skip to content

Commit cf0c53d

Browse files
authored
Merge pull request #1084 from TanukiSharp/feature/from-countly-root
Added local module to resolve relative path from Countly root path
2 parents 54a940c + a83edd3 commit cf0c53d

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
"plugins/pluginManager.js",
196196
"plugins/*/api/**/*.js",
197197
"plugins/*/frontend/*.js",
198+
"local-modules/**/*.js",
198199
"plugins/*/extend/*.js"
199200
],
200201
"env": {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "from-countly-root",
3+
"version": "0.1.0",
4+
"description": "Resolves a relative path from Countly root directory to an absolute path.",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+ssh://git@github.com/Countly/countly-server.git"
9+
}
10+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"express-session": "1.16.2",
4848
"external-ip": "2.1.1",
4949
"formidable": "1.2.1",
50+
"from-countly-root": "file:./local-modules/from-countly-root",
5051
"fs.extra": "^1.3.2",
5152
"geoip-lite": "1.3.7",
5253
"grunt": "1.0.4",

0 commit comments

Comments
 (0)