Skip to content

Commit

Permalink
bobbyg603/issue16 (#17)
Browse files Browse the repository at this point in the history
* Make Command Line Invocation Easier
Fixes #16
  • Loading branch information
bobbyg603 authored Apr 19, 2021
1 parent 527abef commit e24c2c2
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 97 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
],
"args": [
"bin/index.ts",
"__tests__/test-data/dir-multiple-source-map-files/stack-to-convert.txt",
"__tests__/test-data/dir-multiple-source-map-files"
"__tests__/test-data/dir-multiple-source-map-files",
"__tests__/test-data/dir-multiple-source-map-files/stack-to-convert.txt"
],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart",
Expand Down
47 changes: 36 additions & 11 deletions bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
import * as clipboardy from "clipboardy";
import { Stats } from "fs";
import * as fs from "fs/promises";
import { StackConverter } from "../lib/stack-converter";

(async () => {
try {
if (process.argv.length != 4) {
if (
process.argv.some(arg => arg === '-h')
|| process.argv.some(arg => arg === '/h')
|| process.argv.some(arg => arg === '-help')
|| process.argv.some(arg => arg === '/help')
) {
helpAndExit();
}

const stackFilePath = process.argv[2];
const sourceMapPath = process.argv[3];

try {
await fs.lstat(stackFilePath)
} catch {
throw new Error(`Stack file path ${stackFilePath} does not exist`);
let sourceMapPath = process.argv[2];
if (!sourceMapPath) {
sourceMapPath = ".";
}

let sourceMapStat: Stats;
try {
sourceMapStat = await fs.lstat(sourceMapPath)
} catch {
throw new Error(`Source map path ${stackFilePath} does not exist`);
throw new Error(`Source map path ${sourceMapPath} does not exist`);
}

let stackFileContents;
const stackFilePath = process.argv[3];
if (!stackFilePath) {
stackFileContents = await clipboardy.read();
} else {
try {
await fs.lstat(stackFilePath)
} catch {
throw new Error(`Stack file path ${stackFilePath} does not exist`);
}
try {
stackFileContents = await fs.readFile(stackFilePath, 'utf-8');
} catch {
throw new Error(`Could not read contents of ${stackFilePath}`);
}
}

if (!stackFileContents) {
throw new Error('Stack contents are empty');
}

const converter = sourceMapStat.isDirectory() ? await StackConverter.createFromDirectory(sourceMapPath) : new StackConverter([sourceMapPath]);
const stackFileContents = await fs.readFile(stackFilePath, 'utf-8');
const { error, stack } = await converter.convert(stackFileContents);
if (error) {
throw new Error(error);
Expand All @@ -44,7 +66,10 @@ function helpAndExit() {
stack-converter command line usage:
stack-converter "/path/to/stack.txt" [ "/path/to/source-maps-dir" OR "/path/to/stack.js.map" ]'
stack-converter [ [ "/source-map-directory" OR "/source.map.js" ] [ "/stack-trace.txt" ] ]
* Optionally provide either a path to a directory containing source maps or a .map.js file - Defaults to current directory
* Optionally provide a path to a .txt file containing a JavaScript Error stack trace - Defaults to value in clipboard
❤️ [email protected]
`;
Expand Down
2 changes: 1 addition & 1 deletion lib/stack-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class StackConverter {

const stackFrames = stackTraceParser.parse(stack);
if (stackFrames.length == 0) {
return { error: 'No stack frames found in stackString' };
return { error: 'No stack frames found in stack input' };
}

const buff: string[] = [];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"bugsplat"
],
"dependencies": {
"clipboardy": "^2.3.0",
"fs-extra": "^9.1.0",
"stacktrace-parser": "^0.1.10",
"ts-node": "^9.1.1",
Expand Down
Loading

0 comments on commit e24c2c2

Please sign in to comment.