Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 17, 2021
1 parent 5f4a6b4 commit 5889182
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 38 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
37 changes: 15 additions & 22 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
declare namespace cleanStack {
interface Options {
/**
Prettify the file paths in the stack:
export interface Options {
/**
Prettify the file paths in the stack:
`/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `~/dev/clean-stack/unicorn.js:2:15`
`/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `~/dev/clean-stack/unicorn.js:2:15`
@default false
*/
readonly pretty?: boolean;
@default false
*/
readonly pretty?: boolean;

/**
Remove the given base path from stack trace file paths, effectively turning absolute paths into relative ones.
/**
Remove the given base path from stack trace file paths, effectively turning absolute paths into relative ones.
Example with `'/Users/sindresorhus/dev/clean-stack/'` as `basePath`:
Example with `'/Users/sindresorhus/dev/clean-stack/'` as `basePath`:
`/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `unicorn.js:2:15`
*/
readonly basePath?: string;
}
`/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15` → `unicorn.js:2:15`
*/
readonly basePath?: string;
}

/**
Expand All @@ -27,7 +25,7 @@ Clean up error stack traces. Removes the mostly unhelpful internal Node.js entri
@example
```
import cleanStack = require('clean-stack');
import cleanStack from 'clean-stack';
const error = new Error('Missing unicorn');
Expand All @@ -48,9 +46,4 @@ console.log(cleanStack(error.stack));
// at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
```
*/
declare function cleanStack(
stack: string,
options?: cleanStack.Options
): string;

export = cleanStack;
export default function cleanStack(stack: string, options?: Options): string;
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
const os = require('os');
const escapeStringRegexp = require('escape-string-regexp');
import os from 'os';
import escapeStringRegexp from 'escape-string-regexp';

const extractPathRegex = /\s+at.*[(\s](.*)\)?/;
const pathRegex = /^(?:(?:(?:node|(?:(?:node:)?internal\/[\w/]*|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.js)?:\d+:\d+)|native)/;
const homeDir = typeof os.homedir === 'undefined' ? '' : os.homedir();

module.exports = (stack, {pretty = false, basePath} = {}) => {
export default function cleanStack(stack, {pretty = false, basePath} = {}) {
const basePathRegex = basePath && new RegExp(`(at | \\()${escapeStringRegexp(basePath)}`, 'g');

return stack.replace(/\\/g, '/')
Expand Down Expand Up @@ -42,4 +41,4 @@ module.exports = (stack, {pretty = false, basePath} = {}) => {
return line;
})
.join('\n');
};
}
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {expectType} from 'tsd';
import cleanStack = require('.');
import cleanStack from './index.js';

const error = new Error('Missing unicorn');

if (error.stack) {
expectType<string>(cleanStack(error.stack));
expectType<string>(cleanStack(error.stack, {pretty: true}));
expectType<string>(cleanStack(error.stack, {basePath: __dirname}));
expectType<string>(cleanStack(error.stack, {basePath: 'foo'}));
}
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -29,12 +31,12 @@
"electron"
],
"dependencies": {
"escape-string-regexp": "4.0.0"
"escape-string-regexp": "5.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.11.0",
"xo": "^0.32.0"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
},
"browser": {
"os": false
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm install clean-stack
## Usage

```js
const cleanStack = require('clean-stack');
import cleanStack from 'clean-stack';

const error = new Error('Missing unicorn');

Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from 'os';
import test from 'ava';
import cleanStack from '.';
import cleanStack from './index.js';

test('default', t => {
const pre = 'Error: foo\n at Test.fn (/Users/sindresorhus/dev/clean-stack/test.js:6:15)';
Expand Down

0 comments on commit 5889182

Please sign in to comment.