Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
feat: code analysis (#98)
Browse files Browse the repository at this point in the history
* feat: code analysis

* fix: new debug

* fix: tscompile

* fix: inner

Co-authored-by: Harry Chen <czy88840616@gmail.com>
  • Loading branch information
echosoar and czy88840616 committed Mar 27, 2020
1 parent b2b8271 commit 2651faa
Show file tree
Hide file tree
Showing 23 changed files with 550 additions and 310 deletions.
4 changes: 3 additions & 1 deletion packages/faas-cli-command-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"typings": "dist/index.d.ts",
"dependencies": {
"@midwayjs/serverless-spec-builder": "^0.2.55",
"minimist": "^1.2.0"
"minimist": "^1.2.0",
"urllib": "^2.34.1",
"ws": "^7.2.3"
},
"devDependencies": {
"benchmark": "^2.1.4",
Expand Down
6 changes: 5 additions & 1 deletion packages/faas-cli-command-core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export class BaseCLI {
cwd = process.cwd();

constructor(argv) {
this.argv = minimist(argv.slice(2));
if (Array.isArray(argv)) {
this.argv = minimist(argv.slice(2));
} else {
this.argv = argv;
}
this.commands = [].concat(this.argv._);
this.loadSpec();
this.providerName = (this.spec.provider && this.spec.provider.name) || '';
Expand Down
124 changes: 124 additions & 0 deletions packages/faas-cli-command-core/src/debug.ts
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);
}
1 change: 1 addition & 0 deletions packages/faas-cli-command-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './npm';
export * from './utils/loadSpec';
export * from './utils/commandLineUsage';
export * from './cli';
export * from './debug';
16 changes: 10 additions & 6 deletions packages/faas-cli/bin/fun.js
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();
}
});
3 changes: 2 additions & 1 deletion packages/faas-util-ts-compile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dependencies": {
"fs-extra": "^8.1.0",
"globby": "^10.0.1",
"midway-bin": "^2.0.0"
"midway-bin": "^2.0.0",
"typescript": "^3.8.3"
},
"scripts": {
"build": "npm run lint && midway-bin build -c",
Expand Down
Loading

0 comments on commit 2651faa

Please sign in to comment.