Commit 5066fc47 authored by anycodes's avatar anycodes
Browse files

update output format

parent 4bff8391
Showing with 127 additions and 115 deletions
+127 -115
#!/usr/bin/env node
require('../lib/set/language');
{
"name": "@serverless-devs/s",
"version": "2.0.54",
"version": "2.0.55",
"description": "Serverless devs tool, serverless developer tool, supports Alibaba cloud, AWS, azure, baidu cloud, Huawei cloud, Google cloud and Tencent cloud.",
"homepage": "https://www.serverless-devs.com",
"keywords": [
......
/** @format */
import { getCredential, loadComponent } from '@serverless-devs/core';
import { logger } from '../utils';
import {getCredential, loadComponent} from '@serverless-devs/core';
import {logger} from '../utils';
import yaml from 'js-yaml';
import path from 'path';
import os from 'os';
import fs from 'fs';
export interface CliParams {
component: string;
command: string;
access: string;
props: string;
component: string;
command: string;
access: string;
props: string;
}
export default class CliManager {
protected inputs: CliParams;
protected inputs: CliParams;
constructor(inputs: CliParams) {
this.inputs = inputs;
}
constructor(inputs: CliParams) {
this.inputs = inputs;
}
async init(): Promise<any> {
let result = '';
try {
let { component, command, access, props } = this.inputs;
// 获取密钥信息
let credentials = {};
try {
const accessFile = path.join(os.homedir(), '.s', 'access.yaml');
const accessFileInfo = yaml.load(fs.readFileSync(accessFile, 'utf8') || '{}');
if (accessFileInfo[access]) {
credentials = await getCredential(access);
}
} catch (e) {
credentials = {};
}
const componentInstance = await loadComponent(component, null, { access });
if (componentInstance) {
if (!command) {
if (componentInstance['index']) {
command = 'index';
} else {
command = 'cli-help-options';
}
}
if (command === 'cli-help-options') {
if (componentInstance.__doc) {
const docResult = componentInstance.__doc();
logger.info(`\n${docResult}`);
} else {
getTempCommandStr(commands: string, length: number) {
const commandsLength = commands.length
const tempArray = new Array(length - commandsLength).fill(' ')
return `${commands}${tempArray.join('')} : `
}
async init(): Promise<any> {
let result = '';
try {
let {component, command, access, props} = this.inputs;
// 获取密钥信息
let credentials = {};
try {
let componentPathYaml = path.join(componentInstance.__path, 'publish.yml');
if (!(await fs.existsSync(componentPathYaml))) {
componentPathYaml = path.join(componentInstance.__path, 'publish.yaml');
}
const publishYamlInfor = await yaml.load(fs.readFileSync(componentPathYaml, 'utf8'));
logger.info(`Help Information:
${publishYamlInfor['Name']}@${publishYamlInfor['Version']}: ${publishYamlInfor['Description']}
${yaml.dump(publishYamlInfor['Commands'])}
${publishYamlInfor['HomePage'] ? '🧭 More information: ' + publishYamlInfor['HomePage'] + '\n' : ''}`);
const accessFile = path.join(os.homedir(), '.s', 'access.yaml');
const accessFileInfo = yaml.load(fs.readFileSync(accessFile, 'utf8') || '{}');
if (accessFileInfo[access]) {
credentials = await getCredential(access);
}
} catch (e) {
logger.info('No document set');
credentials = {};
}
}
return 'help';
}
if (componentInstance[command]) {
let tempProp = {};
try {
tempProp = JSON.parse(props || '{}');
} catch (e) {
throw new Error('-p/--prop parameter format error');
}
try {
result =
(await componentInstance[command]({
props: tempProp,
Properties: tempProp,
Credentials: credentials,
credentials: credentials,
appName: 'default',
Project: {
ProjectName: 'default',
projectName: 'default',
component: component,
Component: component,
provider: undefined,
Provider: undefined,
accessAlias: access || 'default',
AccessAlias: access || 'default',
},
project: {
component: '',
access: access || 'default',
projectName: '',
},
command: command,
Command: command,
args: process.env.temp_params,
argsObj: process['temp_params'],
Args: process.env.temp_params,
ArgsObj: process['temp_params'],
path: {
configPath: '',
},
Path: {
ConfigPath: '',
},
})) || {};
const componentInstance = await loadComponent(component, null, {access});
if (componentInstance) {
if (!command) {
if (componentInstance['index']) {
command = 'index';
} else {
command = 'cli-help-options';
}
}
if (command === 'cli-help-options') {
if (componentInstance.__doc && componentInstance.__doc().length > 10) {
const docResult = componentInstance.__doc();
console.log(`${docResult}`);
} else {
try {
let componentPathYaml = path.join(componentInstance.__path, 'publish.yml');
if (!(await fs.existsSync(componentPathYaml))) {
componentPathYaml = path.join(componentInstance.__path, 'publish.yaml');
}
const publishYamlInfor = await yaml.load(fs.readFileSync(componentPathYaml, 'utf8'));
console.log(`\n ${publishYamlInfor['Name']}@${publishYamlInfor['Version']}: ${publishYamlInfor['Description']}\n`);
let tempLength = 0
if (publishYamlInfor['Commands']) {
for (const item in publishYamlInfor['Commands']) {
if (item.length > tempLength) {
tempLength = item.length
}
}
for (const item in publishYamlInfor['Commands']) {
console.log(` ${this.getTempCommandStr(item, tempLength)} ${publishYamlInfor['Commands'][item]}`)
}
console.log(`\n ${publishYamlInfor['HomePage'] ? '🧭 More information: ' + publishYamlInfor['HomePage'] + '\n' : ''}`)
}
} catch (e) {
logger.error('Help information could not be found');
}
}
return 'help';
}
if (componentInstance[command]) {
let tempProp = {};
try {
tempProp = JSON.parse(props || '{}');
} catch (e) {
throw new Error('-p/--prop parameter format error');
}
try {
result =
(await componentInstance[command]({
props: tempProp,
Properties: tempProp,
Credentials: credentials,
credentials: credentials,
appName: 'default',
Project: {
ProjectName: 'default',
projectName: 'default',
component: component,
Component: component,
provider: undefined,
Provider: undefined,
accessAlias: access || 'default',
AccessAlias: access || 'default',
},
project: {
component: '',
access: access || 'default',
projectName: '',
},
command: command,
Command: command,
args: process.env.temp_params,
argsObj: process['temp_params'],
Args: process.env.temp_params,
ArgsObj: process['temp_params'],
path: {
configPath: '',
},
Path: {
ConfigPath: '',
},
})) || {};
let outResult = yaml.dump(JSON.parse(JSON.stringify(result)));
let outResult = yaml.dump(JSON.parse(JSON.stringify(result)));
logger.success(Object.keys(result).length === 0 ? `End of method: ${command}` : outResult);
} catch (e) {
logger.error(`Failed to execute:\n
logger.success(Object.keys(result).length === 0 ? `End of method: ${command}` : outResult);
} catch (e) {
logger.error(`Failed to execute:\n
❌ Message: ${e.message}
🧭 You can get help for this component by [s ${component} -h]
😈 If you have questions, please tell us: https://github.com/Serverless-Devs/Serverless-Devs/issues\n`);
process.exit(-1);
}
} else {
logger.error(`Failed to execute:\n
process.exit(-1);
}
} else {
logger.error(`Failed to execute:\n
❌ Message: Component ${component} does not include [${command}] method
🧭 You can get help for this component by [s ${component} -h]
😈 If you have questions, please tell us: https://github.com/Serverless-Devs/Serverless-Devs/issues\n`);
process.exit(-1);
}
}
} catch (e) {
logger.error(`Failed to execute:\n
process.exit(-1);
}
}
} catch (e) {
logger.error(`Failed to execute:\n
❌ Message: ${e.message}
🧭 You can get more component on: https://github.com/Serverless-Devs/package-awesome
😈 If you have questions, please tell us: https://github.com/Serverless-Devs/Serverless-Devs/issues\n`);
process.exit(-1);
process.exit(-1);
}
return result;
}
return result;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment