From ebb84a2c215d6af504841d448db14149cac4d155 Mon Sep 17 00:00:00 2001 From: Gao Yang Date: Sat, 16 May 2020 18:36:37 +0800 Subject: [PATCH] feat: command-core support user lifecycle config (#167) --- packages/faas-cli-command-core/src/core.ts | 48 +++++++++++++++++++ .../src/interface/commandHookCore.ts | 1 + .../faas-cli-command-core/test/core.test.ts | 19 ++++++++ .../test/fixtures/userLifecycle/index.js | 12 +++++ .../test/fixtures/userLifecycle/package.json | 7 +++ .../test/fixtures/userLifecycle/test.txt | 1 + 6 files changed, 88 insertions(+) create mode 100644 packages/faas-cli-command-core/test/fixtures/userLifecycle/index.js create mode 100644 packages/faas-cli-command-core/test/fixtures/userLifecycle/package.json create mode 100644 packages/faas-cli-command-core/test/fixtures/userLifecycle/test.txt diff --git a/packages/faas-cli-command-core/src/core.ts b/packages/faas-cli-command-core/src/core.ts index 4d15f99d..5cb8c49f 100644 --- a/packages/faas-cli-command-core/src/core.ts +++ b/packages/faas-cli-command-core/src/core.ts @@ -10,6 +10,8 @@ import { IProviderInstance } from './interface/provider'; import GetMap from './errorMap'; import { loadNpm } from './npm'; import { resolve } from 'path'; +import { exec } from 'child_process'; +import { readFileSync, existsSync } from 'fs'; const RegProviderNpm = /^npm:([\w]*):(.*)$/i; // npm providerName pkgName const RegProviderLocal = /^local:([\w]*):(.*)$/i; // local providerName pkgPath @@ -25,6 +27,8 @@ export class CommandHookCore implements ICommandHooksCore { private loadNpm: any; private preDebugTime: any; private execId: number = Math.ceil(Math.random() * 1000); + private userLifecycle: any = {}; + private cwd: string; store = new Map(); @@ -33,6 +37,7 @@ export class CommandHookCore implements ICommandHooksCore { if (!this.options.options) { this.options.options = {}; } + this.cwd = this.options.cwd || process.cwd(); this.loadNpm = loadNpm.bind(null, this); this.coreInstance = this.getCoreInstance(); @@ -134,6 +139,10 @@ export class CommandHookCore implements ICommandHooksCore { return this.displayHelp(commandsArray, commandInfo.usage); } for (const lifecycle of lifecycleEvents) { + if (this.userLifecycle && this.userLifecycle[lifecycle]) { + this.debug('User Lifecycle', lifecycle); + await this.execCommand(this.userLifecycle[lifecycle]); + } this.debug('Core Lifecycle', lifecycle); const hooks = this.hooks[lifecycle] || []; for (const hook of hooks) { @@ -165,6 +174,7 @@ export class CommandHookCore implements ICommandHooksCore { public async ready() { await this.loadNpmPlugins(); + await this.loadUserLifecycleExtends(); await this.asyncInit(); } @@ -391,6 +401,26 @@ export class CommandHookCore implements ICommandHooksCore { } } + // 获取用户的生命周期扩展 + private async loadUserLifecycleExtends() { + const pkgJsonFile = resolve(this.cwd, 'package.json'); + if (!existsSync(pkgJsonFile)) { + return; + } + try { + const pkgJson = JSON.parse(readFileSync(pkgJsonFile).toString()); + if ( + pkgJson && + pkgJson['midway-integration'] && + pkgJson['midway-integration'].lifecycle + ) { + this.userLifecycle = pkgJson['midway-integration'].lifecycle; + } + } catch (e) { + return; + } + } + private commandOptions(commandOptions, usage): any { if (!commandOptions) { return; @@ -492,4 +522,22 @@ export class CommandHookCore implements ICommandHooksCore { line: matchResult[3], }; } + + async execCommand(command: string) { + return new Promise((resolve, reject) => { + exec( + command, + { + cwd: this.cwd, + }, + error => { + if (error) { + reject(error); + return; + } + resolve(); + } + ); + }); + } } diff --git a/packages/faas-cli-command-core/src/interface/commandHookCore.ts b/packages/faas-cli-command-core/src/interface/commandHookCore.ts index b0646725..03eefdde 100644 --- a/packages/faas-cli-command-core/src/interface/commandHookCore.ts +++ b/packages/faas-cli-command-core/src/interface/commandHookCore.ts @@ -21,6 +21,7 @@ export interface IOptions { npm?: string; // 使用何种npm加速 stopLifecycle?: string; // 生命周期执行到什么时候终止,例如 invoke:invoke disableAutoLoad?: boolean; // 是否禁用自动加载插件 + cwd?: string; // command core 执行时cwd目录 } export interface ICommandHooksCore { diff --git a/packages/faas-cli-command-core/test/core.test.ts b/packages/faas-cli-command-core/test/core.test.ts index b235bc7c..3383090d 100644 --- a/packages/faas-cli-command-core/test/core.test.ts +++ b/packages/faas-cli-command-core/test/core.test.ts @@ -1,6 +1,8 @@ import { CommandHookCore } from '../src'; import TestPlugin from './plugins/test.invoke'; import * as assert from 'assert'; +import { join } from 'path'; +import { readFileSync } from 'fs'; describe('command-core', () => { it('stop lifecycle', async () => { @@ -19,4 +21,21 @@ describe('command-core', () => { await core.invoke(['invoke']); assert(result && result.length === 3); }); + it('user lifecycle', async () => { + const cwd = join(__dirname, './fixtures/userLifecycle'); + const tmpData = Date.now() + ''; + process.env.TEST_MIDWAY_CLI_CORE_TMPDATA = tmpData; + const core = new CommandHookCore({ + provider: 'test', + cwd, + options: { + verbose: true, + }, + }); + core.addPlugin(TestPlugin); + await core.ready(); + await core.invoke(['invoke']); + const testData = readFileSync(join(cwd, 'test.txt')).toString(); + assert(testData === tmpData); + }); }); diff --git a/packages/faas-cli-command-core/test/fixtures/userLifecycle/index.js b/packages/faas-cli-command-core/test/fixtures/userLifecycle/index.js new file mode 100644 index 00000000..7ef5c1ce --- /dev/null +++ b/packages/faas-cli-command-core/test/fixtures/userLifecycle/index.js @@ -0,0 +1,12 @@ +const { unlinkSync, existsSync, writeFileSync } = require('fs'); +const { join } = require('path'); +;(async () => { + await new Promise(resolve => { + setTimeout(resolve, 500); + }); + const file = join(__dirname, 'test.txt'); + if (existsSync(file)) { + unlinkSync(file); + } + writeFileSync(file, process.env.TEST_MIDWAY_CLI_CORE_TMPDATA || ''); +})(); \ No newline at end of file diff --git a/packages/faas-cli-command-core/test/fixtures/userLifecycle/package.json b/packages/faas-cli-command-core/test/fixtures/userLifecycle/package.json new file mode 100644 index 00000000..bcb0e3ac --- /dev/null +++ b/packages/faas-cli-command-core/test/fixtures/userLifecycle/package.json @@ -0,0 +1,7 @@ +{ + "midway-integration": { + "lifecycle": { + "before:invoke:one": "node ./index.js" + } + } +} \ No newline at end of file diff --git a/packages/faas-cli-command-core/test/fixtures/userLifecycle/test.txt b/packages/faas-cli-command-core/test/fixtures/userLifecycle/test.txt new file mode 100644 index 00000000..0b169cfd --- /dev/null +++ b/packages/faas-cli-command-core/test/fixtures/userLifecycle/test.txt @@ -0,0 +1 @@ +1589535981255 \ No newline at end of file