Skip to content

Instantly share code, notes, and snippets.

@a6b8
Last active December 28, 2023 05:34
Show Gist options
  • Select an option

  • Save a6b8/cb85cd873d8d629deaf1babaa85b7a1d to your computer and use it in GitHub Desktop.

Select an option

Save a6b8/cb85cd873d8d629deaf1babaa85b7a1d to your computer and use it in GitHub Desktop.
Get root from module
#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