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

Commit

Permalink
fix: code ana package (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Apr 3, 2020
1 parent d45ff3f commit 6e11d0f
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 51 deletions.
7 changes: 5 additions & 2 deletions packages/faas-cli-command-core/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as minimist from 'minimist';
import { join } from 'path';
import { loadSpec } from './utils/loadSpec';
import { loadSpec, getSpecFile } from './utils/loadSpec';
import { commandLineUsage } from './utils/commandLineUsage';
import { CommandHookCore } from './core';
import { PluginManager } from './pluginManager';
Expand All @@ -10,6 +10,7 @@ export class BaseCLI {
providerName: string;
core: any;
spec: any;
specFile: any;
commands: string[];
cwd = process.cwd();

Expand All @@ -25,6 +26,7 @@ export class BaseCLI {
this.core = new CommandHookCore({
config: {
servicePath: this.cwd,
specFile: this.specFile
},
commands: this.commands,
service: this.spec,
Expand Down Expand Up @@ -65,7 +67,8 @@ export class BaseCLI {
}

loadSpec() {
this.spec = loadSpec(this.cwd);
this.specFile = getSpecFile(this.cwd);
this.spec = loadSpec(this.cwd, this.specFile);
}

// 加载命令行输出及报错
Expand Down
8 changes: 4 additions & 4 deletions packages/faas-cli-command-core/src/utils/loadSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const getSpecFile = baseDir => {
return {};
};

export const loadSpec = baseDir => {
const specFile = getSpecFile(baseDir);
export const loadSpec = (baseDir, specFileInfo?) => {
const specFile = specFileInfo || getSpecFile(baseDir);
if (!specFile || !specFile.type) {
return {};
}
Expand All @@ -28,8 +28,8 @@ export const loadSpec = baseDir => {
}
};

export const writeToSpec = (baseDir, specResult) => {
const specFile = getSpecFile(baseDir);
export const writeToSpec = (baseDir, specResult, specFileInfo?) => {
const specFile = specFileInfo || getSpecFile(baseDir);
if (!specFile || !specFile.type) {
return {};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/faas-cli-plugin-invoke/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BasePlugin, getSpecFile } from '@midwayjs/fcli-command-core';
import { BasePlugin } from '@midwayjs/fcli-command-core';
import { AnalyzeResult, Locator } from '@midwayjs/locate';
import {
compareFileChange,
Expand Down Expand Up @@ -85,6 +85,7 @@ export class FaaSInvokePlugin extends BasePlugin {
if (this.options.clean !== false) {
this.options.clean = true;
}
this.setStore('defaultTmpFaaSOut', this.defaultTmpFaaSOut);
}

getLock(lockKey) {
Expand Down Expand Up @@ -175,7 +176,7 @@ export class FaaSInvokePlugin extends BasePlugin {
await this.waitForLock(this.buildLockPath);
}

const specFile = getSpecFile(this.baseDir).path;
const specFile = this.core.config.specFile.path;
const relativeTsCodeRoot = relative(this.baseDir, this.codeAnalyzeResult.tsCodeRoot) || '.';
if (existsSync(buildLockPath)) {
this.fileChanges = await compareFileChange(
Expand Down
6 changes: 4 additions & 2 deletions packages/faas-cli-plugin-invoke/src/invoke.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommandHookCore, loadSpec } from '@midwayjs/fcli-command-core';
import { CommandHookCore, loadSpec, getSpecFile } from '@midwayjs/fcli-command-core';
import { FaaSInvokePlugin } from './index';

export interface InvokeOptions {
Expand All @@ -15,12 +15,14 @@ export interface InvokeOptions {

export async function invoke (options: InvokeOptions) {
const baseDir = options.functionDir;
const specFile = getSpecFile(baseDir);
const core = new CommandHookCore({
config: {
servicePath: baseDir,
specFile
},
commands: ['invoke'],
service: loadSpec(baseDir),
service: loadSpec(baseDir, specFile),
provider: '',
options: {
function: options.functionName,
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-invoke/test/noyaml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('/test/noyaml.test.ts', () => {
await remove(join(baseDir, './.faas_debug_tmp'));
const result: any = await (invoke as any)({
functionDir: join(__dirname, 'fixtures/noYaml'),
functionName: 'service-handler'
functionName: 'service'
});
assert(result.body === 'hello world');
});
Expand Down
1 change: 1 addition & 0 deletions packages/faas-cli-plugin-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"midway-bin": "^2.0.0"
},
"devDependencies": {
"@midwayjs/faas": "^0.2.61",
"@midwayjs/serverless-spec-builder": "^0.2.61"
},
"engines": {
Expand Down
47 changes: 29 additions & 18 deletions packages/faas-cli-plugin-package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import * as micromatch from 'micromatch';
import { commonPrefix, formatLayers } from './utils';
import {
copyFiles,
CodeAny
} from '@midwayjs/faas-util-ts-compile';
import { compileWithOptions, compileInProject } from '@midwayjs/mwcc';
import { exec } from 'child_process';
import * as archiver from 'archiver';
import { AnalyzeResult, Locator } from '@midwayjs/locate';
import { tmpdir } from 'os';

export class PackagePlugin extends BasePlugin {
core: any;
Expand All @@ -36,7 +38,6 @@ export class PackagePlugin extends BasePlugin {
this.servicePath,
'.serverless'
));
cacheSpec: any;
codeAnalyzeResult: AnalyzeResult;
integrationDistTempDirectory = 'integration_dist'; // 一体化构建的临时目录
zipCodeDefaultName = 'serverless.zip';
Expand All @@ -48,7 +49,9 @@ export class PackagePlugin extends BasePlugin {
'cleanup', // 清理构建目录
'installDevDep', // 安装开发期依赖
'copyFile', // 拷贝文件: package.include 和 shared content
'codeAnalysis', // 代码分析
'tscompile', // 编译函数 'package:after:tscompile'
'checkAggregation', // 检测高密度部署
'generateSpec', // 生成对应平台的描述文件,例如 serverless.yml 等
'generateEntry', // 生成对应平台的入口文件
'installLayer', // 安装layer
Expand Down Expand Up @@ -94,19 +97,19 @@ export class PackagePlugin extends BasePlugin {
'package:cleanup': this.cleanup.bind(this),
'package:installDevDep': this.installDevDep.bind(this),
'package:copyFile': this.copyFile.bind(this),
'package:codeAnalysis': this.codeAnalysis.bind(this),
'package:installLayer': this.installLayer.bind(this),
'package:installDep': this.installDep.bind(this),
'package:checkAggregation': this.checkAggregation.bind(this),
'package:package': this.package.bind(this),
'before:package:finalize': this.finalize.bind(this),
'package:tscompile': this.tsCompile.bind(this),
};

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

async cleanup() {
if (!this.core.config.specFile) {
this.core.config.specFile = getSpecFile(this.servicePath);
}
process.chdir(this.servicePath);
// 修改构建目标目录
if (this.options.buildDir) {
Expand Down Expand Up @@ -303,6 +306,18 @@ export class PackagePlugin extends BasePlugin {
this.core.cli.log(` - Dependencies install complete`);
}

async codeAnalysis() {
if (this.core.service.functions) {
return this.core.service.functions;
}
const newSpec: any = await CodeAny({
spec: this.core.service,
baseDir: this.servicePath,
sourceDir: this.codeAnalyzeResult.tsCodeRoot
});
this.core.service.functions = newSpec.functions;
}

async tsCompile() {
const isTsDir = existsSync(join(this.servicePath, 'tsconfig.json'));
this.core.cli.log('Building Midway FaaS directory files...');
Expand Down Expand Up @@ -429,7 +444,7 @@ export class PackagePlugin extends BasePlugin {
}

// 合并高密度部署
assignAggregationToFunctions() {
async checkAggregation() {
// 只在部署阶段生效
const commands = this.core.processedInput.commands;
if (
Expand Down Expand Up @@ -553,17 +568,13 @@ export class PackagePlugin extends BasePlugin {
{ http: { method: 'get', path: currentPath } },
];
}
const specFile = getSpecFile(this.servicePath);
this.cacheSpec = {
specFile,
specData: readFileSync(specFile.path),
};
writeToSpec(this.servicePath, this.core.service);
}

finalize() {
if (this.cacheSpec) {
writeFileSync(this.cacheSpec.specFile.path, this.cacheSpec.specData);
}
const tmpSpecFile = resolve(tmpdir(), `aggre-${Date.now()}/f.yml`);
await ensureFile(tmpSpecFile);

this.core.config.specFile.path = tmpSpecFile;
writeToSpec(this.servicePath, this.core.service, this.core.config.specFile);
}

finalize() {}
}
22 changes: 8 additions & 14 deletions packages/faas-cli-plugin-package/test/aggregation.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { CommandHookCore, loadSpec } from '@midwayjs/fcli-command-core';
import { PackagePlugin } from '../src/index';
import { AliyunFCPlugin } from '../../faas-cli-plugin-fc/src/index';
import { resolve } from 'path';
import { existsSync } from 'fs-extra';
import { transform } from '@midwayjs/serverless-spec-builder';
import { existsSync, remove } from 'fs-extra';
import * as assert from 'assert';

describe('/test/package.test.ts', () => {
describe('integration project build', () => {
it('aggregation package', async () => {
const baseDir = resolve(__dirname, './fixtures/aggregation');
const buildDir = resolve(baseDir, './.serverless');
await remove(buildDir);
const core = new CommandHookCore({
config: {
servicePath: baseDir,
Expand All @@ -20,20 +22,12 @@ describe('/test/package.test.ts', () => {
log: console,
});
core.addPlugin(PackagePlugin);
core.addPlugin(AliyunFCPlugin);
await core.ready();
await core.invoke(['package']);
const ymlPath = resolve(baseDir, '.serverless/f.yml');
assert(existsSync(ymlPath));
const ymlData = transform(ymlPath);
const funcNames = Object.keys(ymlData.functions);
assert(funcNames.length === 4);
assert(ymlData.functions.aggregationapi);
assert(ymlData.functions.aggregationapi.events[0].http.path === '/api/*');
assert(ymlData.functions.render2);
assert(ymlData.functions.aggregationrenderNot2);
assert(ymlData.functions.aggregationrenderNot2.events[0].http.path === '/*');
assert(ymlData.functions.aggregationnormal);
assert(ymlData.functions.aggregationnormal._allAggred.length === 2);
assert(existsSync(resolve(buildDir, 'aggregationapi.js')));
assert(existsSync(resolve(buildDir, 'aggregationnormal.js')));
assert(existsSync(resolve(buildDir, 'aggregationrenderNot2.js')));
});
});
});
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"dependencies": {
"@midwayjs/faas": "*"
}
}
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"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@midwayjs/faas": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
service: serverless-midway-test

provider:
name: aliyun
runtime: nodejs8
credentials: ./.aliyun_credentials # path must be absolute
14 changes: 14 additions & 0 deletions packages/faas-cli-plugin-package/test/fixtures/noYaml/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Func, Inject, Provide } from '@midwayjs/decorator';
import { FaaSContext } from '@midwayjs/faas';

@Provide()
@Func('service.handler')
export class Service {

@Inject()
ctx: FaaSContext; // context

async handler() {
return 'hello world';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"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",
"sourceRoot": ""
},
"exclude": [
"dist",
"node_modules",
"test"
]
}
33 changes: 33 additions & 0 deletions packages/faas-cli-plugin-package/test/noyaml.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { CommandHookCore, loadSpec } from '@midwayjs/fcli-command-core';
import { PackagePlugin } from '../src/index';
import { AliyunFCPlugin } from '../../faas-cli-plugin-fc/src/index';
import { resolve } from 'path';
import { remove } from 'fs-extra';
import { transform } from '@midwayjs/serverless-spec-builder';
import * as assert from 'assert';

describe('/test/noyaml.test.ts', () => {
describe('integration project build', () => {
it('aggregation package', async () => {
const baseDir = resolve(__dirname, 'fixtures/noYaml');
const buildDir = resolve(baseDir, './.serverless');
await remove(buildDir);
const core = new CommandHookCore({
config: {
servicePath: baseDir,
},
commands: ['package'],
service: loadSpec(baseDir),
provider: 'aliyun',
options: {},
log: console,
});
core.addPlugin(PackagePlugin);
core.addPlugin(AliyunFCPlugin);
await core.ready();
await core.invoke(['package']);
const yaml = transform(resolve(buildDir, 'template.yml'));
assert(yaml.Resources['serverless-midway-test']['service'].Properties.Handler === 'service.handler');
});
});
});
Loading

0 comments on commit 6e11d0f

Please sign in to comment.