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

Commit

Permalink
feat: faas-cli default plugin manager
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Dec 25, 2019
1 parent caa17ff commit ec6cf5f
Show file tree
Hide file tree
Showing 22 changed files with 701 additions and 326 deletions.
27 changes: 26 additions & 1 deletion packages/command-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

基于插件生命周期 + hook的内核

## 内核使用文档
```js
import CoreClass from 'command-core';
const core = new CoreClass({
config: { // 会挂载到 core.coreInstance.config 上
servicePath: baseDir,
},
commands: ['invoke'], // 默认命令,多级命令依次传入数组
service: this.spec, // 会挂载到 core.coreInstance.service 上
provider: 'providerName', // 会比对与插件中的provider是都一致来决定插件是否加载
options: this.argv, // 参数,会作为第二个参数传递给插件构造函数,例如 { function: 'index' }
log: console, // 输出及错误捕获
displayUsage: func // 自定义如何展示帮助 displayUsage(commandsArray, usage, this)
});
core.addPlugin(Plugin); // 载入插件,插件支持 class / 'npm:provider:packageName' / 'local:provider:path' 三种形式
await core.ready(); // 等待初始化
await core.invoke(); // 执行默认命令
```

**core.coreInstance** 会作为第一个参数传递给插件的构造函数,上面挂载了各种方法及属性,详见 [./src/interface/commandHookCore.ts](./src/interface/commandHookCore.ts#L25) ICoreInstance

**options** 作为第二个参数传递给插件构造函数


## 插件开发文档

提供了 `BasePlugin` 插件基类,可以继承此基类编写插件
Expand Down Expand Up @@ -41,9 +65,10 @@ import CommandHookCore from 'command-core';
const core = new CommandHookCore({
provider: 'providerName',
options: {},
commands: ['invoke'],
log: console
});
core.addPlugin(Plugin); // 载入你的插件
await core.ready(); // 等待初始化
await core.invoke(['command']); // 执行对应的命令
await core.invoke(); // 执行对应的命令
```
8 changes: 6 additions & 2 deletions packages/command-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@ export = class CommandHookCore implements ICommandHooksCore {
allowEntryPoints 为是否可以调用 entryPoints
*/
public async invoke(
commandsArray: string[],
commandsArray?: string[],
allowEntryPoints?: boolean,
options?: any
) {
if (commandsArray == null) {
commandsArray = this.options.commands;
}
commandsArray = [].concat(commandsArray);
const commandInfo = this.getCommand(commandsArray, allowEntryPoints);
const lifecycleEvents = this.loadLifecycle(
commandInfo.commandName,
Expand Down Expand Up @@ -332,7 +336,7 @@ export = class CommandHookCore implements ICommandHooksCore {
// 加载npm包插件
private async loadNpmPlugins() {
for (const npmPath of this.npmPlugin) {
await this.loadNpm(npmPath);
await this.loadNpm(npmPath, this.options.npm);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/command-core/src/interface/commandHookCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface IOptions {
[extensionName: string]: any;
};
displayUsage?: any;
npm?: string;
}

export interface ICommandHooksCore {
Expand All @@ -27,7 +28,7 @@ export interface ICoreInstance {
cli: ILog | Console;
config: any;
getProvider(providerName: string): IProviderInstance;
invoke(commandsArray: string[], allowEntryPoints?: boolean, options?: any);
invoke(commandsArray?: string[], allowEntryPoints?: boolean, options?: any);
pluginManager: ICommandHooksCore;
service: {
provider: {
Expand Down
4 changes: 4 additions & 0 deletions packages/command-core/src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ if (!existsSync(commandHookCoreBasePkg)) {
writeFileSync(commandHookCoreBasePkg, `{}`);
}

export const getCoreBaseDir = () => {
return commandHookCoreBaseNodeModules;
};

async function getNpmPath(
scope: any,
npmName: string,
Expand Down
4 changes: 2 additions & 2 deletions packages/faas-cli/bin/fun.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
'use strict';
const CliClass = require('../src');
const cli = new CliClass(process.argv);
const CliClass = require('../dist');
const cli = new CliClass.Cli(process.argv);
cli.start();
12 changes: 9 additions & 3 deletions packages/faas-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
"main": "dist/index",
"typings": "dist/index.d.ts",
"dependencies": {
"minimist": "^1.2.0",
"@midwayjs/spec-builder": "^0.1.5",
"@midwayjs/command-core": "^0.1.5",
"serverless-midway-plugin": "^0.1.5"
"@midwayjs/faas": "^0.1.5",
"@midwayjs/runtime-mock": "^0.1.5",
"@midwayjs/spec-builder": "^0.1.5",
"minimist": "^1.2.0",
"ora": "^4.0.3",
"ts-node": "^8.5.2",
"urllib": "^2.34.1",
"websocket": "^1.0.30"
},
"bin": {
"mf": "bin/fun.js",
"f": "bin/fun.js"
},
"devDependencies": {
"@types/mocha": "^5.2.5",
"midway-bin": "1"
},
"files": [
Expand Down
41 changes: 23 additions & 18 deletions packages/faas-cli/src/index.js → packages/faas-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
const minimist = require('minimist');
const CoreClass = require('@midwayjs/command-core');
const { transform } = require('@midwayjs/spec-builder');
const MidwayPlugin = require('serverless-midway-plugin');
const { existsSync } = require('fs');
const { join } = require('path');
import { loadSpec } from './utils/loadSpec';
import CommandPlugin from './plugins/pluginManager';

const baseDir = process.cwd();
class Cli {
export * from './plugins/invoke/main';
export class Cli {
argv: any;
providerName: string;
core: any;
spec: any;
commands: string[];
constructor(argv) {
this.argv = minimist(argv.slice(2));
this.commands = [].concat(this.argv._);
this.loadSpec();
this.providerName = (this.spec.provider && this.spec.provider.name) || '';
this.core = new CoreClass({
config: {
servicePath: baseDir,
},
commands: this.argv._,
commands: this.commands,
service: this.spec,
provider: this.providerName,
options: this.argv,
Expand All @@ -25,20 +32,19 @@ class Cli {
}

loadDefaultPlugin() {
this.core.addPlugin(MidwayPlugin);
if (!this.commands || !this.commands.length) {
return;
}
switch (this.commands[0]) {
case 'plugin':
this.core.addPlugin(CommandPlugin);
return;
}
this.core.addPlugin('npm::serverless-midway-plugin');
}

loadSpec() {
const specPath = [
'f.yml',
'f.yaml',
'serverless.yml',
'serverless.yaml',
].find(spec => existsSync(join(baseDir, spec)));
if (!specPath) {
this.error('need f.yml');
}
this.spec = transform(specPath);
this.spec = loadSpec(baseDir);
}

error(errMsg) {
Expand Down Expand Up @@ -79,7 +85,6 @@ class Cli {

async start() {
await this.core.ready();
await this.core.invoke(this.argv._);
await this.core.invoke();
}
}
module.exports = Cli;
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,35 @@ import { Local } from './local';
functionName,
argsData,
isDebug,
trigger
starter,
eventPath,
eventName,
handler,
layers
] = process.argv.slice(2);

if (isDebug) {
await waitDebug(isDebug);
}

let layersObj: any = null;
if (layers) {
try {
layersObj = JSON.parse(layers);
} catch (E) {}
}

try {
const local = new Local({
functionName,
trigger: trigger === 'undefined' ? undefined : trigger,
// trigger: trigger === 'undefined' ? undefined : trigger,
starter,
event: {
path: eventPath,
name: eventName
},
handler,
layers: layersObj
});

const args = argsData ? [].concat(JSON.parse(argsData)) : [];
Expand Down
Loading

0 comments on commit ec6cf5f

Please sign in to comment.