This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix:cli cwd & support aliyun fc deploy * fix: deploy deps version
- Loading branch information
1 parent
6b3bde3
commit f8dbaf8
Showing
21 changed files
with
563 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }, | ||
]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Oops, something went wrong.