Last active
December 1, 2018 19:23
-
-
Save iliyaZelenko/2647922d638a94d0b1bb3a8b0157f322 to your computer and use it in GitHub Desktop.
Find source (src) or another folder path going up the Hierarchy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { join } = require('path') | |
export function getSrcDir (srcFolder = 'src', startPath = __dirname) { | |
const parentPath = normalize(startPath + '/..') | |
if (startPath === parentPath) { | |
throw Error('Could not find folder.') | |
} | |
if (basename(startPath) === srcFolder) { | |
return startPath | |
} | |
// (OPTIONAL) if this path contains srcFolder | |
if (fs.existsSync(join(startPath, srcFolder))) { | |
return join(startPath, srcFolder) | |
} | |
return getSrcDir(srcFolder, parentPath) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment