This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: code analysis * fix: new debug * fix: tscompile * fix: inner Co-authored-by: Harry Chen <czy88840616@gmail.com>
- Loading branch information
1 parent
b2b8271
commit 2651faa
Showing
23 changed files
with
550 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { fork, execSync } from 'child_process'; | ||
import * as minimist from 'minimist'; | ||
export const debug = async (options) => { | ||
const { callback, debugFile, debug } = options; | ||
let argv: any = {}; | ||
if (process.argv[2] === 'isDebuging' && process.argv[3]) { | ||
argv = JSON.parse(process.argv[3]); | ||
if (argv.debugInspectPort) { | ||
// debug模式 | ||
await waitDebug(argv.debugInspectPort); | ||
} | ||
} else { | ||
argv = minimist(process.argv.slice(2)); | ||
} | ||
|
||
if (argv.debug && debugFile) { | ||
delete argv.debug; | ||
argv.debugInspectPort = debug || '9229'; | ||
const child = fork( | ||
debugFile, | ||
[ | ||
'isDebuging', | ||
JSON.stringify(argv) | ||
], | ||
{ | ||
cwd: process.cwd(), | ||
env: process.env, | ||
execArgv: [ | ||
'--inspect=' + argv.debugInspectPort | ||
] | ||
} | ||
); | ||
child.on('message', (m) => { | ||
if (m === 'childExit') { | ||
process.exit(); | ||
} | ||
}); | ||
process.on('SIGINT', () => { | ||
execSync('kill -9 ' + child.pid); | ||
child.kill(); | ||
process.exit(); | ||
}); | ||
return; | ||
} | ||
if (argv.debugInspectPort) { | ||
process.on('exit', () => { | ||
process.send('childExit'); | ||
}); | ||
} | ||
if (callback) { | ||
callback(argv); | ||
} | ||
}; | ||
|
||
export function getWssUrl(port, type?: string, isThrowErr?: boolean) { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
const urllib = require('urllib'); | ||
urllib.request('http://127.0.0.1:' + port + '/json/list', (err, data) => { | ||
if (err) { | ||
if (isThrowErr) { | ||
reject(err); | ||
} else { | ||
getWssUrl(port, type, isThrowErr).then(resolve); | ||
} | ||
} else { | ||
const debugInfo = JSON.parse(data.toString()); | ||
const url = debugInfo[0][type || 'webSocketDebuggerUrl'] || ''; | ||
resolve( | ||
url.replace('js_app.html?experiments=true&', 'inspector.html?') | ||
); | ||
} | ||
}); | ||
}, 100); | ||
}); | ||
} | ||
|
||
function debugWs(addr) { | ||
return new Promise(resolve => { | ||
const WebSocket = require('ws'); | ||
const ws = new WebSocket(addr); | ||
let currentId = 0; | ||
const cbMap = {}; | ||
ws.on('open', () => { | ||
ws.on('message', message => { | ||
if (message.utf8Data) { | ||
const data = JSON.parse(message.utf8Data); | ||
if (data.id) { | ||
if (data.id > currentId) { | ||
currentId = data.id - 0; | ||
} | ||
if (cbMap[data.id]) { | ||
cbMap[data.id](data); | ||
} | ||
} | ||
} | ||
}); | ||
const send = (method, params?: any) => { | ||
return new Promise(resolve => { | ||
const curId = currentId + 1; | ||
currentId = curId; | ||
cbMap[curId] = data => { | ||
resolve(data); | ||
}; | ||
const param: any = { id: curId, method }; | ||
if (params) { | ||
param.params = params; | ||
} | ||
ws.send(JSON.stringify(param)); | ||
}); | ||
}; | ||
send('Profiler.enable'); | ||
send('Runtime.enable'); | ||
send('Debugger.enable', { maxScriptsCacheSize: 10000000 }); | ||
send('Debugger.setBlackboxPatterns', { patterns: ['internal'] }); | ||
resolve(send); | ||
}); | ||
}); | ||
} | ||
|
||
export async function waitDebug(port) { | ||
const wssUrl = await getWssUrl(port); | ||
return debugWs(wssUrl); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
require('source-map-support/register'); | ||
|
||
const { CLI } = require('../dist'); | ||
const cli = new CLI(process.argv); | ||
cli.start(); | ||
const core = require("@midwayjs/fcli-command-core"); | ||
core.debug({ | ||
debugFile: __filename, | ||
callback: argv => { | ||
require('source-map-support/register'); | ||
const { CLI } = require('../dist'); | ||
const cli = new CLI(argv); | ||
cli.start(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.