Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provides source map support for stack traces #19

Merged
merged 3 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
coverage
test/fixtures/ts/app/controller/home.js
test/fixtures/ts-pkg/app/controller/home.js
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
logs/
npm-debug.log
/node_modules
node_modules
coverage/
.idea/
run/
.DS_Store
*.swp
test/fixtures/ts/app/controller/home.js
test/fixtures/ts-pkg/app/controller/home.js
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Add `eggctl` to `package.json` scripts:
```

Then run as:

- `npm start`
- `npm stop`

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

### stop

Expand Down
4 changes: 3 additions & 1 deletion lib/cmd/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ class StartCommand extends Command {

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

// whether run in the background.
Expand Down
38 changes: 38 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const fs = require('fs');
const path = require('path');
const BaseCommand = require('common-bin');
const Logger = require('zlogger');
const helper = require('./helper');
Expand All @@ -16,11 +18,47 @@ class Command extends BaseCommand {
execArgv: true,
};

// common-bin setter, don't care about override at sub class
// https://github.com/node-modules/common-bin/blob/master/lib/command.js#L158
this.options = {
sourcemap: {
description: 'whether enable sourcemap support, will load `source-map-support` etc',
type: 'boolean',
alias: [ 'ts', 'typescript' ],
},
};

this.logger = new Logger({
prefix: '[egg-scripts] ',
time: false,
});
}

get context() {
const context = super.context;
const { argv, execArgvObj, cwd } = context;

// read `egg.typescript` from package.json
let baseDir = argv._[0] || cwd;
if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir);
const pkgFile = path.join(baseDir, 'package.json');
if (fs.existsSync(pkgFile)) {
const pkgInfo = require(pkgFile);
if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) {
argv.sourcemap = true;
}
}

// execArgv
if (argv.sourcemap) {
execArgvObj.require = execArgvObj.require || [];
execArgvObj.require.push(require.resolve('source-map-support/register'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ts 不支持 sourcemap?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ts 支持生成 sourcemap,这里是用来根据 sourcemap 还原堆栈的。可以看看 https://zhuanlan.zhihu.com/p/26267678

之前这个 require 都是放到 config 或 app.js agent.js ,或者放框架。
现在这个 PR 是直接在工具层引入了。

}

argv.sourcemap = argv.typescript = argv.ts = undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后面改造一下,需要哪些参数直接赋值,不然这种代码太多了。

Copy link
Member Author

@atian25 atian25 Apr 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 这里其实一句 argv.sourcemap = undefined 也够了。 上面有个 https://github.com/eggjs/egg-scripts/blob/ts/lib/command.js#L16 会自动删除别名。
  • 之前的想法是,上层业务框架可能会自定义一些入参,这里更倾向于黑名单机制,也就是明确在工具层用的入参,在这里删掉。否则白名单机制的话,上层业务框架就要定制 egg-scripts 。


return context;
}
}

module.exports = Command;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"mz-modules": "^2.0.0",
"node-homedir": "^1.1.0",
"runscript": "^1.3.0",
"source-map-support": "^0.5.4",
"zlogger": "^1.1.0"
},
"devDependencies": {
Expand All @@ -26,6 +27,7 @@
"eslint": "^4.11.0",
"eslint-config-egg": "^5.1.1",
"mm": "^2.2.0",
"typescript": "^2.8.1",
"urllib": "^2.25.1",
"webstorm-disable-index": "^1.2.0"
},
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/ts-pkg/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Controller } from 'egg';

export default class AppController extends Controller {
public index() {
try {
throw new Error('some err');
} catch (err) {
this.ctx.logger.error(err);
this.ctx.body = {
msg: err.message,
stack: err.stack,
};
}
}
};
6 changes: 6 additions & 0 deletions test/fixtures/ts-pkg/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
};
3 changes: 3 additions & 0 deletions test/fixtures/ts-pkg/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = '123456';
13 changes: 13 additions & 0 deletions test/fixtures/ts-pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "ts-pkg",
"version": "1.0.0",
"dependencies": {
"egg": "^1.0.0"
},
"egg": {
"typescript": true
},
"scripts": {
"build": "node ../../../node_modules/.bin/tsc"
}
}
29 changes: 29 additions & 0 deletions test/fixtures/ts-pkg/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"strict": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"charset": "utf8",
"allowJs": false,
"pretty": true,
"noEmitOnError": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"strictPropertyInitialization": false,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"inlineSourceMap": true,
"importHelpers": true
},
"exclude": [
"app/public",
"app/views"
]
}
15 changes: 15 additions & 0 deletions test/fixtures/ts/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Controller } from 'egg';

export default class AppController extends Controller {
public index() {
try {
throw new Error('some err');
} catch (err) {
this.ctx.logger.error(err);
this.ctx.body = {
msg: err.message,
stack: err.stack,
};
}
}
};
6 changes: 6 additions & 0 deletions test/fixtures/ts/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
};
3 changes: 3 additions & 0 deletions test/fixtures/ts/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = '123456';
10 changes: 10 additions & 0 deletions test/fixtures/ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ts",
"version": "1.0.0",
"dependencies": {
"egg": "^1.0.0"
},
"scripts": {
"build": "node ../../../node_modules/.bin/tsc"
}
}
29 changes: 29 additions & 0 deletions test/fixtures/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"strict": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"charset": "utf8",
"allowJs": false,
"pretty": true,
"noEmitOnError": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"strictPropertyInitialization": false,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"inlineSourceMap": true,
"importHelpers": true
},
"exclude": [
"app/public",
"app/views"
]
}
18 changes: 10 additions & 8 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,10 @@ describe('test/start.test.js', () => {

describe('read cluster config', () => {
let app;
const fixturePath = path.join(__dirname, 'fixtures/cluster-config');
let fixturePath;

before(function* () {
fixturePath = path.join(__dirname, 'fixtures/cluster-config');
yield utils.cleanup(fixturePath);
});

Expand Down Expand Up @@ -366,9 +367,10 @@ describe('test/start.test.js', () => {

describe('auto set custom node dir to PATH', () => {
let app;
const fixturePath = path.join(__dirname, 'fixtures/custom-node-dir');
let fixturePath;

before(function* () {
fixturePath = path.join(__dirname, 'fixtures/custom-node-dir');
yield utils.cleanup(fixturePath);
});

Expand All @@ -382,16 +384,16 @@ describe('test/start.test.js', () => {
path.join(fixturePath, 'node_modules/.bin'),
path.join(fixturePath, '.node/bin'),
].join(path.delimiter) + path.delimiter;
app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ]);
app.debug();
app = coffee.fork(eggBin, [ 'start', '--workers=2', '--port=7002', fixturePath ]);
// app.debug();
app.expect('code', 0);

yield sleep(waitTime);

assert(app.stderr === '');
assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:7001/));
assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:7002/));
assert(!app.stdout.includes('app_worker#3:'));
const result = yield httpclient.request('http://127.0.0.1:7001');
const result = yield httpclient.request('http://127.0.0.1:7002');
assert(result.data.toString().startsWith(`hi, ${expectPATH}`));
});
});
Expand All @@ -408,7 +410,7 @@ describe('test/start.test.js', () => {
});
afterEach(function* () {
yield coffee.fork(eggBin, [ 'stop', cwd ])
.debug()
// .debug()
.end();
yield utils.cleanup(cwd);
});
Expand Down Expand Up @@ -440,7 +442,7 @@ describe('test/start.test.js', () => {
it('should start default egg', function* () {
cwd = path.join(__dirname, 'fixtures/egg-app');
yield coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', cwd ])
.debug()
// .debug()
.expect('stdout', /Starting egg application/)
.expect('stdout', /egg started on http:\/\/127\.0\.0\.1:7001/)
.expect('code', 0)
Expand Down
Loading