Skip to content

Commit

Permalink
Adjusted tmp layout
Browse files Browse the repository at this point in the history
  • Loading branch information
PhoneDroid committed Jan 21, 2025
1 parent bd7b44a commit 9d6186f
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
const fsPromises = require('fs/promises')
const path = require('path')
const fs = require('fs')
const os = require('os')
Expand Down Expand Up @@ -81,20 +82,43 @@ async function main() {
}
}

const filePrefix = `speedscope-${+new Date()}-${process.pid}`
const jsPath = path.join(os.tmpdir(), `${filePrefix}.js`)
console.log(`Creating temp file ${jsPath}`)
fs.writeFileSync(jsPath, jsSource)
urlToOpen += `#localProfilePath=${jsPath}`

const tmp_directory = path.join(os.tmpdir(),`SpeedScope`)

try {
fs.mkdirSync(tmp_directory)
} catch ( exception ){

if( exception.code != 'EEXIST' ){
console.error(`Failed to create temporary directory 'SpeedScope'`)
throw exception
}
}

console.log(`Created temporary folder ${tmp_directory}`)

const tmp_folder = await fsPromises
.mkdtemp(`${ tmp_directory }${ path.sep }`)
.catch(( exception ) => {
console.error(`Failed to create /tmp/ folder`)
throw exception
})

const path_source = path.join(tmp_folder,`Source.js`)

console.log(`Creating temp file ${path_source}`)
fs.writeFileSync(path_source, jsSource)
urlToOpen += `#localProfilePath=${path_source}`

// For some silly reason, the OS X open command ignores any query parameters or hash parameters
// passed as part of the URL. To get around this weird issue, we'll create a local HTML file
// that just redirects.
const htmlPath = path.join(os.tmpdir(), `${filePrefix}.html`)
console.log(`Creating temp file ${htmlPath}`)
fs.writeFileSync(htmlPath, `<script>window.location=${JSON.stringify(urlToOpen)}</script>`)
const path_wrapper = path.join(tmp_folder,`Wrapper.html`)

console.log(`Creating temp file ${path_wrapper}`)
fs.writeFileSync(path_wrapper, `<script>window.location=${JSON.stringify(urlToOpen)}</script>`)

urlToOpen = `file://${htmlPath}`
urlToOpen = `file://${path_wrapper}`
}

console.log('Opening', urlToOpen, 'in your default browser')
Expand Down

0 comments on commit 9d6186f

Please sign in to comment.