|
| 1 | +import yargs from 'yargs'; |
| 2 | +import { |
| 3 | + explore, |
| 4 | + writeHtmlToTempFile, |
| 5 | + ExploreOptions, |
| 6 | +} from 'source-map-explorer'; |
| 7 | +import sourceMapForRamBundle from './ram-bundle'; |
| 8 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 9 | +import RamBundleParser from 'metro/src/lib/RamBundleParser'; |
| 10 | +import path from 'path'; |
| 11 | + |
| 12 | +import fs from 'fs'; |
| 13 | + |
| 14 | +const argv = yargs |
| 15 | + .strict() |
| 16 | + .scriptName('explore') |
| 17 | + .demandCommand(2, 'bundle and source map files must be specified') |
| 18 | + .options({ |
| 19 | + json: { |
| 20 | + type: 'string', |
| 21 | + description: |
| 22 | + 'If filename specified save output as JSON to specified file otherwise output to stdout.', |
| 23 | + conflicts: ['tsv', 'html'], |
| 24 | + }, |
| 25 | + tsv: { |
| 26 | + type: 'string', |
| 27 | + description: |
| 28 | + 'If filename specified save output as TSV to specified file otherwise output to stdout.', |
| 29 | + conflicts: ['json', 'html'], |
| 30 | + }, |
| 31 | + html: { |
| 32 | + type: 'string', |
| 33 | + description: |
| 34 | + 'If filename specified save output as HTML to specified file otherwise output to stdout rather than opening a browser.', |
| 35 | + conflicts: ['json', 'tsv'], |
| 36 | + }, |
| 37 | + }) |
| 38 | + .group(['json', 'tsv', 'html'], 'Output:') |
| 39 | + .parse(); |
| 40 | + |
| 41 | +const bundle = path.resolve(argv._[0]); |
| 42 | +const sourceMap = path.resolve(argv._[1]); |
| 43 | +const options: ExploreOptions = { |
| 44 | + output: { |
| 45 | + format: |
| 46 | + typeof argv.json === 'string' |
| 47 | + ? 'json' |
| 48 | + : typeof argv.tsv === 'string' |
| 49 | + ? 'tsv' |
| 50 | + : 'html', |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +try { |
| 55 | + const bundleFile = fs.readFileSync(bundle); |
| 56 | + new RamBundleParser(bundleFile); |
| 57 | + sourceMapForRamBundle(bundle, sourceMap, options, false); |
| 58 | +} catch (err) { |
| 59 | + if (path.basename(bundle) === 'UNBUNDLE') { |
| 60 | + sourceMapForRamBundle(bundle, sourceMap, options, true); |
| 61 | + } else { |
| 62 | + explore([bundle, sourceMap], options) |
| 63 | + .then(result => { |
| 64 | + return writeHtmlToTempFile(result.output); |
| 65 | + }) |
| 66 | + .catch(err => console.log(err)); |
| 67 | + } |
| 68 | +} |
0 commit comments