Find absolute base path of the project directory

后端 未结 7 1477
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 21:35

Until now we could get the absolute path of a file to open later as readStream with this code snippet:

var base = path.resolve(\'.\');
var file = base + \'/d         


        
7条回答
  •  Happy的楠姐
    2020-12-04 22:17

    I ran into the same predicament when I updated to 0.6.5.

    What I'm currently doing is getting the path like this:

    var meteor_root = Npm.require('fs').realpathSync( process.cwd() + '/../' );
    

    This returns on dev mode:

    /my/application/.meteor/local/build/programs
    

    and on bundled mode:

    /my/application/build/app/programs
    

    So from here I'm getting to my application's "root" path like so:

    var application_root = Npm.require('fs').realpathSync( meteor_root + '/../' );
    
    // if running on dev mode
    if( Npm.require('path').basename( Npm.require('fs').realpathSync( meteor_root + '/../../../' ) ) == '.meteor' ){
        application_root =  Npm.require('fs').realpathSync( meteor_root + '/../../../../' );
    }
    

    The only case in which this would fail is if you happen to name your application's folder ".meteor" but that's an edge case.

    Relative to that you can access whatever else you need to.

    Additionally, you can also get direct access to to the assets folder that the meteor bundler creates:

    var assets_folder = meteor_root + '/server/assets/' + Npm.require('path').basename( application_root );
    

    This is likely to be temporary as I expect better file/path interaction APIs to be added eventually..

    Hope that helps

提交回复
热议问题