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

fix: Fix #47 #48

Merged
merged 5 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/faas-cli-command-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"typings": "dist/index.d.ts",
"dependencies": {
"@midwayjs/serverless-spec-builder": "^0.2.20",
"command-line-usage": "^6.1.0",
"deepmerge": "^4.2.2",
"minimist": "^1.2.0"
},
Expand Down
48 changes: 48 additions & 0 deletions packages/faas-cli-command-core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from 'path';
import { loadSpec } from './utils/loadSpec';
import { CommandHookCore } from './core';
import { PluginManager } from './pluginManager';
import * as commandLineUsage from 'command-line-usage';

export class BaseCLI {
argv: any;
Expand All @@ -26,6 +27,7 @@ export class BaseCLI {
provider: this.providerName,
options: this.argv,
log: this.loadLog(),
displayUsage: this.displayUsage.bind(this),
extensions: this.loadExtensions(),
});
}
Expand Down Expand Up @@ -61,6 +63,52 @@ export class BaseCLI {
return console;
}

// 展示帮助信息
displayUsage(commandsArray, usage, coreInstance) {
const log = this.loadLog();
let commandList: any = {};
if (commandsArray && commandsArray.length) {
commandList = {
header: commandsArray.join(' '),
optionList: Object.keys(usage || {}).map((name => {
const usageInfo = usage[name] || {};
return {
name,
description: usageInfo.usage,
alias: usageInfo.shortcut,
type: Boolean
};
})),
};
} else {
commandList = [];
coreInstance.instances.forEach((plugin) => {
if (plugin.commands) {
Object.keys(plugin.commands).forEach(command => {
const commandInfo = plugin.commands[command];
if (!commandInfo || !commandInfo.lifecycleEvents) {
return;
}
commandList.push({
header: command,
content: commandInfo.usage,
optionList: Object.keys(commandInfo.options || {}).map((name => {
const usageInfo = commandInfo.options[name] || {};
return {
name,
description: usageInfo.usage,
alias: usageInfo.shortcut,
type: Boolean
};
}))
});
});
}
});
}
log.log(commandLineUsage(commandList));
}

error(errMsg) {
console.log('errMsg', errMsg);
process.exit(1);
Expand Down
29 changes: 29 additions & 0 deletions packages/faas-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,38 @@ export class CLI extends BaseCLI {
async loadPlugins() {
await this.checkProvider();
await super.loadPlugins();
await this.loadDefaultOptions();
}

async loadDefaultOptions() {
if (this.commands.length) {
return;
}

if (this.argv.v || this.argv.version) {
this.displayVersion();
}
}

displayVersion() {
let version = '';
try {
version = require('../package.json').version;
} catch (E) {}
const log = this.loadLog();
log.log(`@midwayjs/faas-cli v${version}`);
}

displayUsage(commandsArray, usage, coreInstance) {
this.displayVersion();
super.displayUsage(commandsArray, usage, coreInstance);
}

async checkProvider() {
// ignore f -v / f -h / f create
if (!this.commands.length || this.commands[0] === 'create' || this.argv.h) {
return;
}
if (!this.spec.provider) {
this.spec.provider = { name: '', runtime: '' };
}
Expand Down
2 changes: 0 additions & 2 deletions packages/serverless-invoke/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ export async function waitDebug(port) {
return sendDebug;
}

export const Debug_Tag = 'midway-faas-local::debug::0x90906';

export const exportMidwayFaaS = (() => {
const midwayModuleName = process.env.MidwayModuleName || '@midwayjs/faas';
const faasPath = join(process.cwd(), './node_modules/', midwayModuleName);
Expand Down