I have a project in a single repository that is supposed to have core files in the root folder, that are used in entire project, and separate theme folders that have their own unique settings and build processes.
Project looks like this:
core/
themes/
some/long/folder/for/a/theme/root/
theme-folders/
gulpfile.js
another/theme/folder/root/
theme-folders/
gulpfile.js
config.json
Each theme folder has it's own gulpfile.js. When I want to initiate gulp process, I start it from desired theme folder.
Gulp and gulp plugins that I use work fine only with relative paths and it is out of the question to use absolute paths. It is problematic to manually discover directory depth for relative paths and I would like instead to automate this process.
Question is - How do I discover directory depth from one gulpfile.js to project root, where config.json is located?
Based on Mark's answer, I have made my own solution. There is a plugin to determine root folder for the app and it is called app-root-path
.
const gulp = require('gulp');
const path = require('path');
const appRoot = require('app-root-path');
gulp.task('default', function () {
// Function to calculate directory depth
const dirDepth = (myDir) => {
return myDir.split(path.sep).length;
};
console.log('Current directory = ' + __dirname);
console.log('Root direcotry = ' + appRoot.path);
console.log('Depth difference = ' + (dirDepth(__dirname) - dirDepth(appRoot.path)));
var rel = '..' + path.sep;
var depth = dirDepth(__dirname) - dirDepth(appRoot.path);
var relPath = rel.repeat(depth);
console.log('Relative path from current folder to root is: ' + relPath);
var pkg = require(relPath + 'package.json');
console.log('Test to see if it works: ' + pkg.name);
});