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

feat: command-core support user lifecycle config #167

Merged
merged 3 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/faas-cli-command-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -165,6 +174,7 @@ export class CommandHookCore implements ICommandHooksCore {

public async ready() {
await this.loadNpmPlugins();
await this.loadUserLifecycleExtends();
await this.asyncInit();
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IOptions {
npm?: string; // 使用何种npm加速
stopLifecycle?: string; // 生命周期执行到什么时候终止,例如 invoke:invoke
disableAutoLoad?: boolean; // 是否禁用自动加载插件
cwd?: string; // command core 执行时cwd目录
}

export interface ICommandHooksCore {
Expand Down
19 changes: 19 additions & 0 deletions packages/faas-cli-command-core/test/core.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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 || '');
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"midway-integration": {
"lifecycle": {
"before:invoke:one": "node ./index.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1589535981255