Last active
December 28, 2023 05:34
-
-
Save a6b8/cb85cd873d8d629deaf1babaa85b7a1d to your computer and use it in GitHub Desktop.
Get root from module
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
| #getRootAbsolutePath() { | |
| const __filename = fileURLToPath( import.meta.url ) | |
| const __dirname = path.dirname( __filename ) | |
| const root = new Array( 10 ) | |
| .fill() | |
| .reduce( ( acc, a ) => { | |
| try { | |
| acc['_acc'] = path.resolve( acc['_acc'], '..' ) | |
| const files = fs.readdirSync( acc['_acc'] ) | |
| if( files.includes( 'package.json' ) ) { | |
| const tmp = fs.readFileSync( `${acc['_acc']}/package.json` ) | |
| const json = JSON.parse( tmp ) | |
| if( | |
| Object.hasOwn( json, 'main' ) && | |
| acc['result'] === null | |
| ) { | |
| acc['result'] = acc['_acc'] | |
| } | |
| } | |
| } catch( e ) {} | |
| return acc | |
| }, { '_acc': __dirname, 'result': null } ) | |
| return root | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment