Skip to content

Commit

Permalink
chore: pretty errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AutoSponge committed Mar 30, 2020
1 parent 626143b commit 82219e6
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 12 deletions.
16 changes: 10 additions & 6 deletions lib/scriptwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { resolve } = require('path');
const { readdir } = require('fs').promises;
const importGlobal = require('import-global');
const playwright = importGlobal('playwright');
const PrettyError = require('pretty-error');

const EVENTS_PATH = resolve(__dirname, 'events');
const EVENTS_DIR = readdir(EVENTS_PATH);
Expand All @@ -16,6 +17,11 @@ const config = new Map();
const company = new Map();
const completions = new Set();

process.on('unhandledRejection', error => {
const err = new PrettyError();
console.log(err.render(error));
});

/**
* @typedef {Object} config
* @property {string} browserType
Expand Down Expand Up @@ -124,9 +130,7 @@ module.exports = class Scriptwriter extends EventEmitter {
completer,
});
}
Object.keys(repl.repl.commands).forEach((key) =>
completions.add(`.${key}`)
);
Object.keys(repl.repl.commands).forEach(key => completions.add(`.${key}`));
const director = this.replServer;
await this.assign({ director });
const { browserType, launch } = this.config;
Expand All @@ -151,7 +155,7 @@ module.exports = class Scriptwriter extends EventEmitter {
const last = chunks.pop();
// there's only one token
if (!chunks.length) {
completions = this.completions.flatMap((c) => {
completions = this.completions.flatMap(c => {
return c.startsWith(last) ? `${line}${c.substring(last.length)}` : [];
});
break complex;
Expand All @@ -162,14 +166,14 @@ module.exports = class Scriptwriter extends EventEmitter {
if (!obj) break complex;
completions = Reflect.ownKeys(obj)
.concat(Reflect.ownKeys(Reflect.getPrototypeOf(obj) || {}))
.flatMap((c) => {
.flatMap(c => {
if (c === 'constructor') return [];
if (typeof c !== 'string') return [];
return [`${line.replace(/\.[^\.]*$/, '')}.${c}`];
})
.sort();
}
const hits = completions.filter((c) => c.startsWith(line));
const hits = completions.filter(c => c.startsWith(line));
if (hits.length === 1 && hits[0] === line) return [[], line];
return [hits.length ? hits : completions, line];
}
Expand Down
163 changes: 158 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"dlv": "^1.1.3",
"import-global": "^0.1.0",
"kleur": "^3.0.3",
"meow": "^6.1.0"
"meow": "^6.1.0",
"pretty-error": "^2.1.1"
},
"devDependencies": {
"ava": "^3.5.1",
Expand Down

0 comments on commit 82219e6

Please sign in to comment.