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

Commit

Permalink
fix: file compare & npm options (#62)
Browse files Browse the repository at this point in the history
* fix: file compare & npm options

* fix: f invoke --clean=false

* fix: clean options boolean value

* test: compare test

* fix: invoke compare
  • Loading branch information
echosoar committed Feb 25, 2020
1 parent 607a579 commit beb50f8
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/faas-cli-command-core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class BaseCLI {
log: this.loadLog(),
displayUsage: this.displayUsage.bind(this),
extensions: this.loadExtensions(),
...this.coverCoreOptions(),
});
}

Expand All @@ -54,6 +55,11 @@ export class BaseCLI {
return {};
}

// 覆盖默认的 core options
coverCoreOptions() {
return {};
}

loadSpec() {
this.spec = loadSpec(this.cwd);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/faas-cli-command-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@ export class CommandHookCore implements ICommandHooksCore {
// 加载本地插件
private async loadLocalPlugin(localPath) {
try {
const plugin = require(localPath);
let plugin = require(localPath);
if (typeof plugin === 'object') {
plugin = plugin[Object.keys(plugin)[0]];
}
this.addPlugin(plugin);
} catch (e) {
this.error('localPlugin', { path: localPath, err: e });
Expand All @@ -339,7 +342,7 @@ export class CommandHookCore implements ICommandHooksCore {
// 加载npm包插件
private async loadNpmPlugins() {
for (const npmPath of this.npmPlugin) {
await this.loadNpm(npmPath, this.options.npm);
await this.loadNpm(npmPath, this.options.options.npm || this.options.npm);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/faas-cli-plugin-invoke/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export class InvokePlugin extends BasePlugin {
functionName,
debug: this.options.debug,
data: this.options.data || '{}',
handler: funcConf.handler
handler: funcConf.handler,
clean: this.options.clean ? this.options.clean !== 'false' : null
};
return invoke(options);
}
Expand Down
1 change: 1 addition & 0 deletions packages/serverless-invoke/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"fs-extra": "^8.1.0",
"ts-node": "^8.5.2",
"urllib": "^2.34.1",
"globby": "^10.0.1",
"websocket": "^1.0.30"
},
"devDependencies": {
Expand Down
20 changes: 17 additions & 3 deletions packages/serverless-invoke/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
2. tsc编译用户代码到dist目录
3. 开源版: 【创建runtime、创建trigger】封装为平台invoke包,提供getInvoke方法,会传入args与入口方法,返回invoke方法
*/
import { FaaSStarterClass, cleanTarget } from './utils';
import { join, resolve } from 'path';
import { existsSync, move } from 'fs-extra';
import { FaaSStarterClass, cleanTarget, compareFileChange } from './utils';
import { join, resolve, relative } from 'path';
import { existsSync, move, writeFileSync, ensureFileSync } from 'fs-extra';
import { loadSpec } from '@midwayjs/fcli-command-core';
import { writeWrapper } from '@midwayjs/serverless-spec-builder';
import { AnalyzeResult, Locator } from '@midwayjs/locate';
Expand Down Expand Up @@ -95,6 +95,20 @@ export abstract class InvokeCore implements IInvoke {
tsBuildRoot: debugRoot,
});
this.buildDir = this.codeAnalyzeResult.tsBuildRoot;
const buildLogPath = resolve(this.buildDir, '.faasTSBuildTime.log');
if (existsSync(buildLogPath)) {
const fileChanges = await compareFileChange(
[ `${relative(baseDir, this.codeAnalyzeResult.tsCodeRoot)}/**/*` ],
[ buildLogPath ],
{ cwd: baseDir }
);
if (!fileChanges || !fileChanges.length) {
console.log('- Auto skip ts compile');
return;
}
}
ensureFileSync(buildLogPath);
writeFileSync(buildLogPath, `ts build at ${Date.now()}`);
// clean directory first
if (this.options.clean) {
await cleanTarget(this.buildDir);
Expand Down
34 changes: 34 additions & 0 deletions packages/serverless-invoke/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const urllib = require('urllib');
const WebSocketClient = require('websocket').client;
import * as globby from 'globby';
import { tmpdir } from 'os';
import { existsSync, readFileSync, remove, unlinkSync, writeFileSync } from 'fs-extra';
import { join, resolve } from 'path';
Expand Down Expand Up @@ -137,3 +138,36 @@ export const cleanTarget = async (p: string) => {
await remove(p);
}
};

// 符合sourceGlob条件的文件中 是否存在 比所有符合toGlob条件的文件 要新的文件
// 返回 fromGlob 中更新的文件
export const compareFileChange = async (fromGlob: string[], toGlob: string[], options?: any) => {
options = options || {};
if (!options.cwd) {
options.cwd = process.cwd();
}
options.stats = true;
const fromFiles: any = await globby(fromGlob, options);
const toFiles: any = await globby(toGlob, options);

if (!fromFiles || !fromFiles.length) {
return [];
}

if (!toFiles || !toFiles.length) {
return fromFiles.map((file: any) => file.path);
}
let latestFilesChangeTime = 0;
for (const file of toFiles) {
if (file.stats.mtimeMs > latestFilesChangeTime) {
latestFilesChangeTime = file.stats.mtimeMs;
}
}
const result = [];
for (const file of fromFiles) {
if (file.stats.mtimeMs > latestFilesChangeTime) {
result.push(file.path);
}
}
return result;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1582631158247
1 change: 1 addition & 0 deletions packages/serverless-invoke/test/fixtures/baseApp/tmp/1.to
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1582631158351
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1582631158456
21 changes: 21 additions & 0 deletions packages/serverless-invoke/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { compareFileChange } from '../src/utils';
import * as assert from 'assert';
import { writeFileSync } from 'fs';
import { resolve } from 'path';
describe('/test/utils.test.ts', () => {
it('compareFileChange', async () => {
const timeout = (fileName: string) => {
return new Promise((res) => {
setTimeout(() => {
writeFileSync(resolve(__dirname, fileName), `${Date.now()}`);
res(true);
}, 100);
});
};
await timeout('./fixtures/baseApp/tmp/1.from');
await timeout('./fixtures/baseApp/tmp/1.to');
await timeout('./fixtures/baseApp/tmp/2.from');
const result = await compareFileChange(['./fixtures/baseApp/tmp/*.from'], ['./fixtures/baseApp/tmp/*.to'], { cwd: __dirname });
assert(result && result[0] === './fixtures/baseApp/tmp/2.from');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exports.handler = asyncWrapper(async (...args) => {
}

if (handler) {
return starter.handleInvokeWrapper(handler.hanlder)(ctx);
return starter.handleInvokeWrapper(handler.handler)(ctx);
}
return 'unhandler path: ' + ctxPath + '; handlerInfo: ' + JSON.stringify(allHandlers);
})(...args);
Expand Down

0 comments on commit beb50f8

Please sign in to comment.