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

Commit

Permalink
fix: core hooks (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Mar 31, 2020
1 parent 953db8a commit ac6b574
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 29 deletions.
14 changes: 13 additions & 1 deletion packages/faas-cli-command-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IPluginInstance, ICommandInstance } from './interface/plugin';
import { IProviderInstance } from './interface/provider';
import GetMap from './errorMap';
import { loadNpm } from './npm';
import { resolve } from 'path';

const RegProviderNpm = /^npm:([\w]*):(.*)$/i; // npm providerName pkgName
const RegProviderLocal = /^local:([\w]*):(.*)$/i; // local providerName pkgPath
Expand Down Expand Up @@ -128,9 +129,16 @@ export class CommandHookCore implements ICommandHooksCore {
return this.displayHelp(commandsArray, commandInfo.usage);
}
for (const lifecycle of lifecycleEvents) {
this.debug('Core Lifecycle', lifecycle);
const hooks = this.hooks[lifecycle] || [];
for (const hook of hooks) {
await hook();
try {
await hook();
} catch (e) {
this.debug('Core Lifecycle Hook Error');
console.log(e);
throw e;
}
}
}
}
Expand Down Expand Up @@ -335,6 +343,10 @@ export class CommandHookCore implements ICommandHooksCore {
return;
}
try {
if (this.options.config && this.options.config.servicePath && /^\./.test(localPath)) {
localPath = resolve(this.options.config.servicePath, localPath);
}
this.debug('Core Local Plugin', localPath);
let plugin = require(localPath);
if (typeof plugin === 'object') {
plugin = plugin[Object.keys(plugin)[0]];
Expand Down
30 changes: 17 additions & 13 deletions packages/faas-cli-command-core/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ export const debug = async (options) => {
]
}
);
const exit = () => {
execSync('kill -9 ' + child.pid);
child.kill();
process.exit();
};
child.on('message', (m) => {
if (m === 'childExit') {
process.exit();
exit();
}
});
process.on('SIGINT', () => {
execSync('kill -9 ' + child.pid);
child.kill();
process.exit();
});
process.on('SIGINT', exit);
return;
}
if (argv.debugInspectPort) {
Expand Down Expand Up @@ -83,14 +84,17 @@ function debugWs(addr) {
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;
try {
message = JSON.parse(message);
} catch (e) {}
if (message.params) {
const id = message.params.scriptId;
if (id) {
if (id > currentId) {
currentId = id - 0;
}
if (cbMap[data.id]) {
cbMap[data.id](data);
if (cbMap[id]) {
cbMap[id](message.params);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/faas-cli-command-core/test/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
const core = require("../dist");
core.debug({
debugFile: __filename,
callback: () => {
setTimeout(() => {
process.exit();
}, 10000);
}
});
15 changes: 15 additions & 0 deletions packages/faas-cli-command-core/test/debug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { waitDebug } from '../src';
import { exec} from 'child_process';
import { join } from 'path';
import * as assert from 'assert';
describe('/test/debug.test.ts', () => {
it('debug', (done) => {
const port = 9229;
const child = exec(`node ${join(__dirname, './debug.js')} --debug=${port} -h`, () => {});
child.stdout.on('data', console.log);
waitDebug(port).then(send => {
assert(typeof send === 'function');
done();
});
});
});
9 changes: 3 additions & 6 deletions packages/faas-cli-command-core/test/f.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@


service: ginkgo-test

service:
name: ginkgo-test
provider:
name: ginkgo
runtime: node10tb

functions:
index:
handler: index.handler
events:
- hsf: true
- hsf: true
21 changes: 20 additions & 1 deletion packages/faas-cli-command-core/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import { loadSpec } from '../src/utils/loadSpec';
import { loadSpec, writeToSpec } from '../src/utils/loadSpec';
import { commandLineUsage } from '../src/utils/commandLineUsage';
import * as assert from 'assert';
import { tmpdir } from 'os';
describe('/test/utils.test.ts', () => {
it('loadSpec', async () => {
const spec = loadSpec(__dirname);
assert(spec && spec.provider && spec.provider.name === 'ginkgo');
});
it('writeToSpec', async () => {
const result: any = writeToSpec(tmpdir(), {});
assert(Object.keys(result).length === 0);
const spec = loadSpec(__dirname);
writeToSpec(__dirname, spec);
});
it('commandLineUsage', async () => {
const result = commandLineUsage({
optionList: [
{
alias: 't',
name: 'test'
}
]
});
assert(/-t, --test/.test(result));
});
});
10 changes: 6 additions & 4 deletions packages/faas-cli-plugin-invoke/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
compareFileChange,
copyFiles,
CodeAny,
combineTsConfig
} from '@midwayjs/faas-util-ts-compile';
import { writeWrapper } from '@midwayjs/serverless-spec-builder';
import { createRuntime } from '@midwayjs/runtime-mock';
Expand Down Expand Up @@ -148,6 +149,7 @@ export class FaaSInvokePlugin extends BasePlugin {
}
lockMap[buildLockPath] = true;
this.skipTsBuild = true;
this.setStore('skipTsBuild', true);
this.core.debug('Auto skip ts compile');
return;
}
Expand Down Expand Up @@ -185,21 +187,21 @@ export class FaaSInvokePlugin extends BasePlugin {
buildRoot: this.buildDir,
tsCodeRoot: this.codeAnalyzeResult.tsCodeRoot,
incremental: this.options.incremental,
tsConfig: {
tsConfig: combineTsConfig({
compilerOptions: {
sourceRoot: this.codeAnalyzeResult.tsCodeRoot, // for sourceMap
},
},
}, this.options.tsConfig),
clean: this.options.clean,
});
} else {
await tsCompile(this.baseDir, {
tsConfigName: 'tsconfig.json',
tsConfig: {
tsConfig: combineTsConfig({
compilerOptions: {
sourceRoot: resolve(this.baseDir, 'src'), // for sourceMap
},
},
}, this.options.tsConfig),
clean: this.options.clean,
});
const dest = join(this.buildDir, 'dist');
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1585484280577
1585640953051
2 changes: 1 addition & 1 deletion packages/faas-util-ts-compile/test/fixtures/files/tmp/1.to
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1585484280679
1585640953155
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1585484280785
1585640953260
7 changes: 6 additions & 1 deletion packages/serverless-invoke/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,16 @@ export abstract class InvokeCore implements IInvoke {
},
clean: this.options.clean,
});
await move(join(baseDir, 'dist'), join(this.buildDir, 'dist'), opts);
const dest = join(this.buildDir, 'dist');
if (existsSync(dest)) {
await remove(dest);
}
await move(join(baseDir, 'dist'), dest, opts);
}
} catch (e) {
await remove(buildLogPath);
lockMap[buildLogPath] = false;
console.log(e);
throw new Error(`Typescript Build Error, Please Check Your FaaS Code!`);
}
lockMap[buildLogPath] = true;
Expand Down

0 comments on commit ac6b574

Please sign in to comment.