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

Commit

Permalink
fix: invoke export (#20)
Browse files Browse the repository at this point in the history
* fix: remove pkg unit test

* fix: remove serverless-midway-plugin

* fix: export all invoke function
  • Loading branch information
echosoar authored and czy88840616 committed Jan 14, 2020
1 parent e734c6b commit 9d743f8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 61 deletions.
5 changes: 3 additions & 2 deletions packages/serverless-invoke/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { invoke as InvokeFun } from './main';
export * from './main';
export * from './interface';
export const invoke = InvokeFun;
export * from './core';
export * from './utils';
120 changes: 61 additions & 59 deletions packages/serverless-invoke/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,70 @@ import { Invoke } from './invoke';
import { InvokeOptions } from './interface';
import { fork } from 'child_process';
import { get, getWssUrl } from './utils';
export const invoke = async (options: InvokeOptions) => {
if (!options.data || !options.data.length) {
options.data = [{}];
}

if (options.debug) {
if (typeof options.debug !== 'string' || !/^\d+^/.test(options.debug)) {
options.debug = '9229';
export const getInvoke = (Invoke, debugPath) => {
return async (options: InvokeOptions) => {
if (!options.data || !options.data.length) {
options.data = [{}];
}
options.isDebug = true;
const child = fork(
require.resolve('./debug'),
[
JSON.stringify(options),
options.debug
],
{
cwd: options.functionDir || process.env.PWD,
execArgv: [
'--inspect=' + options.debug
],
silent: true
}
);
getWssUrl(options.debug, 'devtoolsFrontendUrl', true).then(debugUrl => {
console.log('[local invoke] debug at 127.0.0.1:' + options.debug);
console.log('[local invoke] devtools at ' + debugUrl);
if (options.debugCb) {
options.debugCb({
port: options.debug,
info: debugUrl,
});
}
});

return new Promise((resolve, reject) => {
get(child, 'faastest').then(data => {
child.kill();
resolve(data);
});
let err = '';
child.stdout.on('data', buf => {
console.log('[local invoke log]', buf.toString());
});
child.stderr.on('data', buf => {
err += buf.toString();
});
child.on('close', () => {
if (err) {
reject(err);
if (options.debug) {
if (typeof options.debug !== 'string' || !/^\d+^/.test(options.debug)) {
options.debug = '9229';
}
options.isDebug = true;
const child = fork(
require.resolve(debugPath),
[
JSON.stringify(options),
options.debug
],
{
cwd: options.functionDir || process.env.PWD,
execArgv: [
'--inspect=' + options.debug
],
silent: true
}
);
getWssUrl(options.debug, 'devtoolsFrontendUrl', true).then(debugUrl => {
console.log('[local invoke] debug at 127.0.0.1:' + options.debug);
console.log('[local invoke] devtools at ' + debugUrl);
if (options.debugCb) {
options.debugCb({
port: options.debug,
info: debugUrl,
});
}
});
});
}

const invokeFun = new Invoke({
baseDir: options.functionDir,
functionName: options.functionName,
handler: options.handler,
trigger: options.trigger,
isDebug: options.isDebug
});

return invokeFun.invoke([].concat(options.data));
return new Promise((resolve, reject) => {
get(child, 'faastest').then(data => {
child.kill();
resolve(data);
});
let err = '';
child.stdout.on('data', buf => {
console.log('[local invoke log]', buf.toString());
});
child.stderr.on('data', buf => {
err += buf.toString();
});
child.on('close', () => {
if (err) {
reject(err);
}
});
});
}
const invokeFun = new Invoke({
baseDir: options.functionDir,
functionName: options.functionName,
handler: options.handler,
trigger: options.trigger,
isDebug: options.isDebug
});
return invokeFun.invoke([].concat(options.data));
};
};

export const invoke = getInvoke(Invoke, './debug');

0 comments on commit 9d743f8

Please sign in to comment.