Skip to content

Commit 0fb3801

Browse files
committed
feat: provides source map support for stack traces
1 parent 0016e28 commit 0fb3801

File tree

17 files changed

+292
-2
lines changed

17 files changed

+292
-2
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
coverage
2+
test/fixtures/ts/app/controller/home.js

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
logs/
22
npm-debug.log
3-
/node_modules
3+
node_modules
44
coverage/
55
.idea/
66
run/
77
.DS_Store
88
*.swp
9+
test/fixtures/ts/app/controller/home.js
10+
test/fixtures/ts-pkg/app/controller/home.js

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Add `eggctl` to `package.json` scripts:
2424
```
2525

2626
Then run as:
27+
2728
- `npm start`
2829
- `npm stop`
2930

@@ -55,6 +56,7 @@ $ eggctl start [options] [baseDir]
5556
- `stderr` - customize stderr file, default to `$HOME/logs/master-stderr.log`.
5657
- `timeout` - the maximum timeout when app starts, default to 300s.
5758
- `ignore-stderr` - whether ignore stderr when app starts.
59+
- `sourcemap` / `typescript` / `ts` - provides source map support for stack traces.
5860

5961
### stop
6062

lib/cmd/start.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ class StartCommand extends Command {
133133

134134
// remove unused properties from stringify, alias had been remove by `removeAlias`
135135
const ignoreKeys = [ '_', '$0', 'env', 'daemon', 'stdout', 'stderr', 'timeout', 'ignore-stderr' ];
136-
const eggArgs = [ this.serverBin, stringify(argv, ignoreKeys), `--title=${argv.title}` ];
136+
const clusterOptions = stringify(argv, ignoreKeys);
137+
// Note: `spawn` is not like `fork`, had to pass `execArgv` youself
138+
const eggArgs = [ ...(execArgv || []), this.serverBin, clusterOptions, `--title=${argv.title}` ];
137139
this.logger.info('Run node %s', eggArgs.join(' '));
138140

139141
// whether run in the background.

lib/command.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const fs = require('fs');
4+
const path = require('path');
35
const BaseCommand = require('common-bin');
46
const Logger = require('zlogger');
57
const helper = require('./helper');
@@ -16,11 +18,46 @@ class Command extends BaseCommand {
1618
execArgv: true,
1719
};
1820

21+
// common-bin setter, don't care about override at sub class
22+
// https://github.com/node-modules/common-bin/blob/master/lib/command.js#L158
23+
this.options = {
24+
sourcemap: {
25+
description: 'whether enable sourcemap support, will load `source-map-support` etc',
26+
type: 'boolean',
27+
alias: [ 'ts', 'typescript' ],
28+
},
29+
};
30+
1931
this.logger = new Logger({
2032
prefix: '[egg-scripts] ',
2133
time: false,
2234
});
2335
}
36+
37+
get context() {
38+
const context = super.context;
39+
const { argv, execArgvObj, cwd } = context;
40+
41+
// read `egg.typescript` from package.json
42+
const baseDir = argv._[0] || cwd;
43+
const pkgFile = path.join(baseDir, 'package.json');
44+
if (fs.existsSync(pkgFile)) {
45+
const pkgInfo = require(pkgFile);
46+
if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) {
47+
argv.sourcemap = true;
48+
}
49+
}
50+
51+
// execArgv
52+
if (argv.sourcemap) {
53+
execArgvObj.require = execArgvObj.require || [];
54+
execArgvObj.require.push(require.resolve('source-map-support/register'));
55+
}
56+
57+
argv.sourcemap = argv.typescript = argv.ts = undefined;
58+
59+
return context;
60+
}
2461
}
2562

2663
module.exports = Command;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"mz-modules": "^2.0.0",
1616
"node-homedir": "^1.1.0",
1717
"runscript": "^1.3.0",
18+
"source-map-support": "^0.5.4",
1819
"zlogger": "^1.1.0"
1920
},
2021
"devDependencies": {
@@ -26,6 +27,7 @@
2627
"eslint": "^4.11.0",
2728
"eslint-config-egg": "^5.1.1",
2829
"mm": "^2.2.0",
30+
"typescript": "^2.8.1",
2931
"urllib": "^2.25.1",
3032
"webstorm-disable-index": "^1.2.0"
3133
},
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Controller } from 'egg';
2+
3+
export default class AppController extends Controller {
4+
public async index() {
5+
try {
6+
throw new Error('some err');
7+
} catch (err) {
8+
this.ctx.logger.error(err);
9+
this.ctx.body = {
10+
msg: err.message,
11+
stack: err.stack,
12+
};
13+
}
14+
}
15+
};

test/fixtures/ts-pkg/app/router.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
const { router, controller } = app;
5+
router.get('/', controller.home.index);
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
exports.keys = '123456';

test/fixtures/ts-pkg/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "ts-pkg",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"egg": "^1.0.0"
6+
},
7+
"egg": {
8+
"typescript": true
9+
},
10+
"scripts": {
11+
"build": "node ../../../node_modules/.bin/tsc"
12+
}
13+
}

0 commit comments

Comments
 (0)