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

Commit

Permalink
fix: load spec (#119)
Browse files Browse the repository at this point in the history
* fix: load spec

* fix: process.cwd

Co-authored-by: Harry Chen <czy88840616@gmail.com>
  • Loading branch information
echosoar and czy88840616 committed Apr 8, 2020
1 parent f188125 commit 66df38c
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/faas-cli-command-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ export class CommandHookCore implements ICommandHooksCore {
this.preDebugTime = now;
}
const diffTime = Number((now - this.preDebugTime) / 1000).toFixed(2);
this.preDebugTime = now;
this.getLog().log('[Verbose]', `${diffTime}s`, ...args);
}
}
2 changes: 1 addition & 1 deletion packages/faas-cli-command-core/src/utils/loadSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const getSpecFile = baseDir => {
'f.yaml',
'serverless.yml',
'serverless.yaml',
].find(spec => existsSync(resolve(baseDir, spec)));
].find(spec => existsSync(resolve(baseDir || process.cwd(), spec)));
if (specPath) {
return {
type: 'yaml',
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-invoke/src/getFuncList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface IGetFuncList {
}

export async function getFuncList (options: IGetFuncList) {
const baseDir = options.functionDir;
const baseDir = options.functionDir || process.cwd();
const specFile = getSpecFile(baseDir);
const core = new CommandHookCore({
config: {
Expand Down
4 changes: 2 additions & 2 deletions packages/faas-cli-plugin-invoke/src/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export interface InvokeOptions {
}

export async function invoke (options: InvokeOptions) {
const baseDir = options.functionDir;
const specFile = getSpecFile(baseDir || process.cwd());
const baseDir = options.functionDir || process.cwd();
const specFile = getSpecFile(baseDir);
const core = new CommandHookCore({
config: {
servicePath: baseDir,
Expand Down
36 changes: 34 additions & 2 deletions packages/faas-cli-plugin-package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class PackagePlugin extends BasePlugin {
this.servicePath,
'.serverless'
));
defaultTmpFaaSOut = resolve(this.midwayBuildPath, 'faas_tem_out');
codeAnalyzeResult: AnalyzeResult;
integrationDistTempDirectory = 'integration_dist'; // 一体化构建的临时目录
zipCodeDefaultName = 'serverless.zip';
Expand Down Expand Up @@ -102,6 +103,7 @@ export class PackagePlugin extends BasePlugin {
'package:installDep': this.installDep.bind(this),
'package:checkAggregation': this.checkAggregation.bind(this),
'package:package': this.package.bind(this),
'after:package:generateEntry': this.defaultGenerateEntry.bind(this),
'before:package:finalize': this.finalize.bind(this),
'package:tscompile': this.tsCompile.bind(this),
};
Expand Down Expand Up @@ -181,6 +183,7 @@ export class PackagePlugin extends BasePlugin {
}
await remove(this.midwayBuildPath);
await ensureDir(this.midwayBuildPath);
this.setStore('defaultTmpFaaSOut', this.defaultTmpFaaSOut);
}

async installDevDep() {
Expand Down Expand Up @@ -313,8 +316,12 @@ export class PackagePlugin extends BasePlugin {
const newSpec: any = await CodeAny({
spec: this.core.service,
baseDir: this.servicePath,
sourceDir: this.codeAnalyzeResult.tsCodeRoot
sourceDir: [
this.codeAnalyzeResult.tsCodeRoot,
resolve(this.defaultTmpFaaSOut, 'src')
]
});
this.core.debug('CcdeAnalysis', newSpec);
this.core.service.functions = newSpec.functions;
}

Expand All @@ -334,7 +341,7 @@ export class PackagePlugin extends BasePlugin {
} else {
await compileInProject(this.servicePath, join(this.midwayBuildPath, 'dist'), undefined, { compilerOptions: { sourceRoot: '../src' } });
}
const tmpOutDir = resolve(this.midwayBuildPath, 'faas_tmp_out/src');
const tmpOutDir = resolve(this.defaultTmpFaaSOut, 'src');
if (existsSync(tmpOutDir)) {
await compileWithOptions(this.servicePath, join(this.midwayBuildPath, 'dist'), {
compilerOptions: { rootDir: tmpOutDir },
Expand All @@ -344,6 +351,31 @@ export class PackagePlugin extends BasePlugin {
this.core.cli.log(` - Build Midway FaaS complete`);
}

// 生成默认入口
async defaultGenerateEntry() {
const functions = this.core.service.functions || {};
for (const func in functions) {
const handlerConf = functions[func];
if (handlerConf._ignore) {
continue;
}
const [handlerFileName] = handlerConf.handler.split('.');
const othEnterFile = [
join(this.defaultTmpFaaSOut, handlerFileName + '.js'),
join(this.core.config.servicePath, handlerFileName + '.js'),
].find((file) => existsSync(file));
if (othEnterFile) {
const fileName = join(this.midwayBuildPath, `${handlerFileName}.js`);
await copy(othEnterFile, fileName);
this.core.debug('Use user entry', othEnterFile);
}
}
if (existsSync(this.defaultTmpFaaSOut)) {
this.core.debug('Tmp Out Dir Removed');
await remove(this.defaultTmpFaaSOut);
}
}

async package() {
this.core.cli.log('Package artifact...');
// 跳过打包
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {BasePlugin} from '@midwayjs/fcli-command-core';
import { resolve } from 'path';
import { copy } from 'fs-extra';
export class FaaSTmpOutPlugin extends BasePlugin {
hooks = {
'after:package:cleanup': async () => {
const tmpDir = this.getStore('defaultTmpFaaSOut', 'PackagePlugin');
console.log('tmpDIr', tmpDir);
await copy(resolve(__dirname, 'test.ts'), resolve(tmpDir, 'src/test.ts'));
await copy(resolve(__dirname, 'test.js'), resolve(tmpDir, 'test.js'));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('test.js');
10 changes: 10 additions & 0 deletions packages/faas-cli-plugin-package/test/fixtures/plugins/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Func, Provide } from '@midwayjs/decorator';

@Provide()
export class Test {

@Func('test.hand')
async handler() {
return 'hello world';
}
}
13 changes: 12 additions & 1 deletion packages/faas-cli-plugin-package/test/noyaml.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CommandHookCore, loadSpec } from '@midwayjs/fcli-command-core';
import { PackagePlugin } from '../src/index';
import { AliyunFCPlugin } from '../../faas-cli-plugin-fc/src/index';
import { FaaSTmpOutPlugin } from './fixtures/plugins/faas_tmp_out';
import { resolve } from 'path';
import { remove } from 'fs-extra';
import { remove, existsSync, readFileSync } from 'fs-extra';
import { transform } from '@midwayjs/serverless-spec-builder';
import * as assert from 'assert';

Expand All @@ -24,10 +25,20 @@ describe('/test/noyaml.test.ts', () => {
});
core.addPlugin(PackagePlugin);
core.addPlugin(AliyunFCPlugin);
core.addPlugin(FaaSTmpOutPlugin);
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');
assert(yaml.Resources['serverless-midway-test']['test-hand'].Properties.Handler === 'test.hand');
assert(!existsSync(resolve(buildDir, 'faas_tmp_out')));
assert(
/console.log\('test\.js'\)/.test(
readFileSync(
resolve(buildDir, 'test.js')
).toString()
)
);
});
});
});

0 comments on commit 66df38c

Please sign in to comment.