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

Commit

Permalink
feat: add cli deploy plugins (#11)
Browse files Browse the repository at this point in the history
* fix:cli cwd & support aliyun fc deploy
* fix: deploy deps version
  • Loading branch information
echosoar authored and czy88840616 committed Jan 7, 2020
1 parent 6b3bde3 commit f8dbaf8
Show file tree
Hide file tree
Showing 21 changed files with 563 additions and 12 deletions.
3 changes: 1 addition & 2 deletions packages/faas-cli-command-core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ export class BaseCLI {
core: any;
spec: any;
commands: string[];
cwd;
cwd = process.cwd();

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.cwd = process.cwd();
this.core = new CommandHookCore({
config: {
servicePath: this.cwd,
Expand Down
13 changes: 12 additions & 1 deletion packages/faas-cli-command-core/src/interface/commandHookCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,24 @@ export interface ICoreInstance {
invoke(commandsArray?: string[], allowEntryPoints?: boolean, options?: any);
pluginManager: ICommandHooksCore;
service: {
service?: {
name: string;
};
provider: {
name: string;
};
functions: object;
layers: object;
layers?: object;
resources: object;
custom: any;
package?: any;
aggregation?: {
[aggregationName: string]: {
deployOrigin?: boolean;
functions: string[];
};
};
globalDependencies?: any;
};
processedInput: {
options: any;
Expand Down
26 changes: 26 additions & 0 deletions packages/faas-cli-plugin-deploy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@midwayjs/fcli-plugin-deploy",
"version": "0.2.1",
"main": "dist/index",
"typings": "dist/index.d.ts",
"dependencies": {
"@midwayjs/fcli-command-core": "^0.2.0"
},
"devDependencies": {
"midway-bin": "1"
},
"files": [
"src",
"dist"
],
"scripts": {
"build": "npm run lint && midway-bin build -c",
"lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json src/**/*.ts test/**/*.ts",
"test": "npm run lint && NODE_ENV=test midway-bin test --ts --full-trace",
"debug": "npm run lint && NODE_ENV=test midway-bin test --ts --full-trace --inspect-brk=9229",
"cov": "NODE_ENV=unittest midway-bin cov --ts",
"clean": "midway-bin clean",
"autod": "midway-bin autod"
},
"gitHead": "b67e2753cbdcc91813067ba2a1bb1ce7e85a3dff"
}
118 changes: 118 additions & 0 deletions packages/faas-cli-plugin-deploy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { BasePlugin, ICoreInstance } from '@midwayjs/fcli-command-core';
import { commonPrefix } from './utils';
export class DeployPlugin extends BasePlugin {
core: ICoreInstance;
options: any;
commands = {
deploy: {
usage: 'Deploy to online',
lifecycleEvents: ['deploy'],
options: {},
},
};

constructor(core, options) {
super(core, options);
this.assignAggregationToFunctions();
}

// 合并高密度部署
assignAggregationToFunctions() {
// 只在部署阶段生效
if (!this.core.processedInput.commands || !this.core.processedInput.commands.length || this.core.processedInput.commands[0] !== 'deploy') {
return;
}
if (
!this.core.service.aggregation ||
!this.core.service.functions
) {
return;
}

if (
!this.core.service.custom ||
!this.core.service.custom.customDomain ||
!this.core.service.custom.customDomain.domainName
) {
console.warn(
'If using aggregation deploy, please configure custom domain'
);
return;
}

this.core.cli.log('Aggregation Deploy');
const allAggregationPaths = [];
for (const aggregationName in this.core.service.aggregation) {
const aggregationFuncName = `aggregation${aggregationName}`;
this.core.service.functions[
aggregationFuncName
] = this.core.service.aggregation[aggregationName];
this.core.service.functions[
aggregationFuncName
].handler = `${aggregationFuncName}.handler`;
this.core.service.functions[
aggregationFuncName
]._isAggregation = true;
if (!this.core.service.functions[aggregationFuncName].events) {
this.core.service.functions[aggregationFuncName].events = [];
}
// 忽略原始方法,不再单独进行部署
const deployOrigin = this.core.service.aggregation[aggregationName]
.deployOrigin;

const allPaths = [];
let handlers = [];
if (this.core.service.aggregation[aggregationName].functions) {
handlers = this.core.service.aggregation[
aggregationName
].functions
.map((functionName: string) => {
const functions = this.core.service.functions;
const func = functions[functionName];
if (!func || !func.events) {
return;
}
const httpEventIndex = func.events.findIndex(
(event: any) => !!event.http
);
if (httpEventIndex === -1) {
return;
}
const httpEvent = func.events[httpEventIndex];
if (!httpEvent || !httpEvent.http.path) {
return;
}
allPaths.push(httpEvent.http.path);
if (!deployOrigin) {
// 不把原有的函数进行部署
this.core.service.functions[functionName]._ignore = true;
}
return {
path: httpEvent.http.path,
handler: func.handler,
};
})
.filter((func: any) => !!func);
}

let currentPath = commonPrefix(allPaths);
currentPath = currentPath ? `${currentPath}/*` : '/*';
this.core.cli.log(
` - using '${currentPath}' to deploy '${allPaths.join(`', '`)}'`
);
if (allAggregationPaths.indexOf(currentPath) !== -1) {
console.error(
`Cannot use the same prefix '${currentPath}' for aggregation deployment`
);
process.exit();
}
allAggregationPaths.push(currentPath);
this.core.service.functions[
aggregationFuncName
]._handlers = handlers;
this.core.service.functions[aggregationFuncName].events = [
{ http: { method: 'get', path: currentPath } },
];
}
}
}
30 changes: 30 additions & 0 deletions packages/faas-cli-plugin-deploy/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

function commonPrefixUtil(str1: string, str2: string): string {
let result = '';
const n1 = str1.length;
const n2 = str2.length;

for (let i = 0, j = 0; i <= n1 - 1 && j <= n2 - 1; i++, j++) {
if (str1[i] !== str2[j]) {
break;
}
result += str1[i];
}
return result;
}

export function commonPrefix(arr: string[]): string {
let prefix: string = arr && arr[0] || '';
const n = arr && arr.length || 0;
for (let i = 1; i <= n - 1; i++) {
prefix = commonPrefixUtil(prefix, arr[i].replace(/([^\/])$/, '$1/'));
}
if (!prefix || prefix === '/') {
return prefix;
}
const result = prefix.replace(/\/[^\/]*$/ig, '') || '/';
if (result && !/^\//.test(result)) {
return '/' + result;
}
return result;
}
22 changes: 22 additions & 0 deletions packages/faas-cli-plugin-deploy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compileOnSave": true,
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"inlineSourceMap":true,
"noImplicitThis": true,
"noUnusedLocals": true,
"stripInternal": true,
"pretty": true,
"declaration": true,
"outDir": "dist"
},
"exclude": [
"dist",
"node_modules",
"test"
]
}
29 changes: 29 additions & 0 deletions packages/faas-cli-plugin-fc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@midwayjs/fcli-plugin-fc",
"version": "0.2.1",
"main": "dist/index",
"typings": "dist/index.d.ts",
"dependencies": {
"@midwayjs/fcli-command-core": "^0.2.0",
"@alicloud/fun": "^3.1.3",
"@midwayjs/serverless-spec-builder": "^0.2.0",
"ejs": "^3.0.1"
},
"devDependencies": {
"midway-bin": "1"
},
"files": [
"src",
"dist"
],
"scripts": {
"build": "npm run lint && midway-bin build -c",
"lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json src/**/*.ts test/**/*.ts",
"test": "npm run lint && NODE_ENV=test midway-bin test --ts --full-trace",
"debug": "npm run lint && NODE_ENV=test midway-bin test --ts --full-trace --inspect-brk=9229",
"cov": "NODE_ENV=unittest midway-bin cov --ts",
"clean": "midway-bin clean",
"autod": "midway-bin autod"
},
"gitHead": "b67e2753cbdcc91813067ba2a1bb1ce7e85a3dff"
}
Loading

0 comments on commit f8dbaf8

Please sign in to comment.