New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getFileName does not work in electron #29

Open
xianjimli opened this Issue Jan 8, 2017 · 0 comments

Comments

Projects
None yet
1 participant
@xianjimli

xianjimli commented Jan 8, 2017

getFileName does not work in electron, In Electron, filename starts with "file://", it should be removed, or bind will failed.

exports.getFileName = function getFileName (calling_file) {
  var origPST = Error.prepareStackTrace
    , origSTL = Error.stackTraceLimit
    , dummy = {}
    , fileName

  Error.stackTraceLimit = 10

  Error.prepareStackTrace = function (e, st) {
    for (var i=0, l=st.length; i<l; i++) {
      fileName = st[i].getFileName()
      if (fileName !== __filename) {
        if (calling_file) {
            if (fileName !== calling_file) {
              return
            }
        } else {
          return
        }
      }
    }
  }

  // run the 'prepareStackTrace' function above
  Error.captureStackTrace(dummy)
  dummy.stack

 // cleanup
  Error.prepareStackTrace = origPST
  Error.stackTraceLimit = origSTL

  //In Electron, filename starts with "file://"
  var fileSchema = "file://";
  if(fileName.indexOf(fileSchema) === 0) {
     fileName = fileName.substr(fileSchema.length);
     //on windows
     if(fileName.indexOf(":/") == 2){
         fileName = fileName.substr(1);
     }
  }

  return fileName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment