Skip to content

Instantly share code, notes, and snippets.

@iliyaZelenko
Last active December 1, 2018 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iliyaZelenko/2647922d638a94d0b1bb3a8b0157f322 to your computer and use it in GitHub Desktop.
Save iliyaZelenko/2647922d638a94d0b1bb3a8b0157f322 to your computer and use it in GitHub Desktop.
Find source (src) or another folder path going up the Hierarchy.
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