|
1 | 1 | #!/usr/bin/env node |
| 2 | +const fsPromises = require('fs/promises') |
2 | 3 | const path = require('path') |
3 | 4 | const fs = require('fs') |
4 | 5 | const os = require('os') |
@@ -81,20 +82,43 @@ async function main() { |
81 | 82 | } |
82 | 83 | } |
83 | 84 |
|
84 | | - const filePrefix = `speedscope-${+new Date()}-${process.pid}` |
85 | | - const jsPath = path.join(os.tmpdir(), `${filePrefix}.js`) |
86 | | - console.log(`Creating temp file ${jsPath}`) |
87 | | - fs.writeFileSync(jsPath, jsSource) |
88 | | - urlToOpen += `#localProfilePath=${jsPath}` |
| 85 | + |
| 86 | + const tmp_directory = path.join(os.tmpdir(),`SpeedScope`) |
| 87 | + |
| 88 | + try { |
| 89 | + fs.mkdirSync(tmp_directory) |
| 90 | + } catch ( exception ){ |
| 91 | + |
| 92 | + if( exception.code != 'EEXIST' ){ |
| 93 | + console.error(`Failed to create temporary directory 'SpeedScope'`) |
| 94 | + throw exception |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + console.log(`Created temporary folder ${tmp_directory}`) |
| 99 | + |
| 100 | + const tmp_folder = await fsPromises |
| 101 | + .mkdtemp(`${ tmp_directory }${ path.sep }`) |
| 102 | + .catch(( exception ) => { |
| 103 | + console.error(`Failed to create /tmp/ folder`) |
| 104 | + throw exception |
| 105 | + }) |
| 106 | + |
| 107 | + const path_source = path.join(tmp_folder,`Source.js`) |
| 108 | + |
| 109 | + console.log(`Creating temp file ${path_source}`) |
| 110 | + fs.writeFileSync(path_source, jsSource) |
| 111 | + urlToOpen += `#localProfilePath=${path_source}` |
89 | 112 |
|
90 | 113 | // For some silly reason, the OS X open command ignores any query parameters or hash parameters |
91 | 114 | // passed as part of the URL. To get around this weird issue, we'll create a local HTML file |
92 | 115 | // that just redirects. |
93 | | - const htmlPath = path.join(os.tmpdir(), `${filePrefix}.html`) |
94 | | - console.log(`Creating temp file ${htmlPath}`) |
95 | | - fs.writeFileSync(htmlPath, `<script>window.location=${JSON.stringify(urlToOpen)}</script>`) |
| 116 | + const path_wrapper = path.join(tmp_folder,`Wrapper.html`) |
| 117 | + |
| 118 | + console.log(`Creating temp file ${path_wrapper}`) |
| 119 | + fs.writeFileSync(path_wrapper, `<script>window.location=${JSON.stringify(urlToOpen)}</script>`) |
96 | 120 |
|
97 | | - urlToOpen = `file://${htmlPath}` |
| 121 | + urlToOpen = `file://${path_wrapper}` |
98 | 122 | } |
99 | 123 |
|
100 | 124 | console.log('Opening', urlToOpen, 'in your default browser') |
|
0 commit comments