Skip to content

Commit

Permalink
style: apply mwts (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed May 8, 2020
1 parent 52cbed5 commit fe3a74a
Show file tree
Hide file tree
Showing 106 changed files with 882 additions and 673 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./node_modules/mwts/",
"ignorePatterns": ["node_modules", "dist", "fixtures"],
"env": {
"mocha": true
}
}
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install && npm install codecov
- run: npm run bootstrap
- run: npm run lint
- run: npm run build --if-present
- run: npm run cov
- name: Upload coverage to Codecov
Expand Down
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('mwts/.prettierrc.json')
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"gh-pages": "^1.2.0",
"lerna": "3",
"lerna-relinker": "^1.4.0",
"mwts": "^1.0.5",
"opencollective": "^1.0.3",
"opencollective-postinstall": "^2.0.2",
"rimraf": "^3.0.0",
Expand All @@ -33,7 +34,9 @@
"build": "sh scripts/build.sh",
"prettier": "prettier --write 'packages/**/*.ts'",
"authors": "git log --format='%aN <%aE>' | sort -u > AUTHORS",
"site": "vuepress dev docs"
"site": "vuepress dev docs",
"lint": "lerna exec mwts check",
"lint:fix": "lerna exec mwts fix"
},
"keywords": [
"serverless",
Expand Down
12 changes: 6 additions & 6 deletions packages/egg-layer/framework/app/extend/context.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

module.exports = {
get fctx() {
if (!this.request.fctx) {
return this.app;
}
return this.request.fctx;
},
get fctx() {
if (!this.request.fctx) {
return this.app;
}
return this.request.fctx;
},
};
37 changes: 18 additions & 19 deletions packages/egg-layer/framework/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
const egg = require('egg');

const EGG_PATH = Symbol.for('egg#eggPath');
exports.getFramework = (framework) => {
const customerEgg = framework ? require(framework) : egg;
function extendsBase(BaseClass) {
return class LayerWrapper extends BaseClass {
get [EGG_PATH]() {
return __dirname;
}
createAnonymousContext(req) {
const ctx = super.createAnonymousContext(req);
if (req && req.fctx) {
ctx.request.fctx = req.fctx;
}
return ctx;
}

exports.getFramework = framework => {
const customerEgg = framework ? require(framework) : egg;
function extendsBase(BaseClass) {
return class LayerWrapper extends BaseClass {
get [EGG_PATH]() {
return __dirname;
}
createAnonymousContext(req) {
const ctx = super.createAnonymousContext(req);
if (req && req.fctx) {
ctx.request.fctx = req.fctx;
}
}
exports.Agent = extendsBase(customerEgg.Agent)
exports.Application = extendsBase(customerEgg.Application);
}
return ctx;
}
};
}
exports.Agent = extendsBase(customerEgg.Agent);
exports.Application = extendsBase(customerEgg.Application);
};
4 changes: 3 additions & 1 deletion packages/egg-layer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export = (engine: RuntimeEngine) => {
const packageJSON = require(resolve(baseDir, 'package.json'));
framework = packageJSON.egg && packageJSON.egg.framework;
const localFrameWorkPath = resolve(__dirname, '../framework');
require(localFrameWorkPath).getFramework(framework && resolve(baseDir, 'node_modules', framework));
require(localFrameWorkPath).getFramework(
framework && resolve(baseDir, 'node_modules', framework)
);
eggApp = await start({
baseDir,
framework: localFrameWorkPath,
Expand Down
28 changes: 18 additions & 10 deletions packages/egg-layer/test/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BaseRuntimeEngine, Runtime, ServerlessBaseRuntime } from '@midwayjs/runtime-engine';
import {
BaseRuntimeEngine,
Runtime,
ServerlessBaseRuntime,
} from '@midwayjs/runtime-engine';
import * as http from 'http';
import { exec } from 'child_process';

Expand All @@ -17,7 +21,7 @@ export class MockRuntime {
options: {
layers?: any[];
functionDir?: string;
runtime?: Runtime
runtime?: Runtime;
} = {}
) {
this.options = options;
Expand All @@ -26,6 +30,7 @@ export class MockRuntime {

async start() {
process.env.ENTRY_DIR = this.options.functionDir;
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
if (this.runtime.on) {
this.runtime.on('error', err => {
Expand Down Expand Up @@ -53,7 +58,7 @@ export class MockRuntime {
}

async invoke(...args) {
return this.runtime.invokeDataHandler.apply(this.runtime, args);
return this.runtime.invokeDataHandler(...args);
}

async invokeHTTP(data) {
Expand All @@ -68,17 +73,20 @@ export class MockRuntime {
});
}

this.httpServer.listen(0, (err) => {
this.httpServer.listen(0, err => {
if (err) {
reject(err);
} else {
exec(`curl 127.0.0.1:${this.httpServer.address().port}`, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
exec(
`curl 127.0.0.1:${this.httpServer.address().port}`,
(error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
resolve(stdout);
}
resolve(stdout);
});
);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-command-core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class BaseCLI {
this.core = new CommandHookCore({
config: {
servicePath: this.cwd,
specFile: this.specFile
specFile: this.specFile,
},
commands: this.commands,
service: this.spec,
Expand Down
50 changes: 31 additions & 19 deletions packages/faas-cli-command-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export class CommandHookCore implements ICommandHooksCore {

private getCommand(commandsArray: string[], allowEntryPoints?: boolean): any {
let command: string | undefined = '';
// tslint:disable-next-line: no-this-assignment
// eslint-disable-next-line @typescript-eslint/no-this-alias
let cmdObj: any = this;
const commandPath: string[] = [];
const parentCommandList: string[] = [];
Expand Down Expand Up @@ -357,7 +357,11 @@ export class CommandHookCore implements ICommandHooksCore {
// 加载本地插件
private async loadLocalPlugin(localPath) {
try {
if (this.options.config && this.options.config.servicePath && /^\./.test(localPath)) {
if (
this.options.config &&
this.options.config.servicePath &&
/^\./.test(localPath)
) {
localPath = resolve(this.options.config.servicePath, localPath);
}
this.debug('Core Local Plugin', localPath);
Expand Down Expand Up @@ -433,51 +437,59 @@ export class CommandHookCore implements ICommandHooksCore {
}

debug(...args) {

const verbose = this.options.options.V || this.options.options.verbose || process.env.MIDWAY_FAAS_VERBOSE;
const verbose =
this.options.options.V ||
this.options.options.verbose ||
process.env.MIDWAY_FAAS_VERBOSE;
if (!verbose) {
return;
return;
}

const now = Date.now();
if (!this.preDebugTime) {
this.preDebugTime = now;
this.preDebugTime = now;
}
const { type, path, line } = this.getStackTrace();
let stack = '';
if (type) {
if (typeof verbose === 'string' && type !== verbose) {
return;
}
stack = `(${type}:${path}:${line})`;
if (typeof verbose === 'string' && type !== verbose) {
return;
}
stack = `(${type}:${path}:${line})`;
}
const diffTime = Number((now - this.preDebugTime) / 1000).toFixed(2);
this.preDebugTime = now;
this.getLog().log('[Verbose]', this.execId, `+${diffTime}s`, ...args, stack);
this.getLog().log(
'[Verbose]',
this.execId,
`+${diffTime}s`,
...args,
stack
);
}

getStackTrace() {
if (!Error.captureStackTrace) {
return {};
return {};
}
const obj: any = {};
Error.captureStackTrace(obj, this.getStackTrace);
if (!obj.stack || !obj.stack.split) {
return {};
return {};
}
const stackStr = obj.stack.split('\n');
if (!stackStr || !stackStr[2]) {
return {};
return {};
}
const matchReg = /(?:-plugin-|\/faas-cli-command-)(\w+)\/(.*?):(\d+):\d+/;
if (!matchReg.test(stackStr[2])) {
return {};
return {};
}
const matchResult = matchReg.exec(stackStr[2]);
return {
type: matchResult[1],
path: matchResult[2],
line: matchResult[3]
}
type: matchResult[1],
path: matchResult[2],
line: matchResult[3],
};
}
}
4 changes: 2 additions & 2 deletions packages/faas-cli-command-core/src/errorMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ErrorMap = {
pluginType: (info: string) => {
return {
info,
message: `only support npm / local / class plugin`,
message: 'only support npm / local / class plugin',
};
},
};
Expand All @@ -41,7 +41,7 @@ export default <T>(type: string, info: T): ReturnValue<T> => {
if (!error) {
return {
info,
message: `error`,
message: 'error',
};
}
return error(info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface ICoreInstance {
service: {
experimentalFeatures?: {
[featureName: string]: any;
},
};
service?: {
name: string;
};
Expand Down
1 change: 1 addition & 0 deletions packages/faas-cli-command-core/src/interface/provider.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IProviderInstance {}
7 changes: 4 additions & 3 deletions packages/faas-cli-command-core/src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (!existsSync(commandHookCoreBaseDir)) {
mkdirSync(commandHookCoreBaseDir, '0777');
}
if (!existsSync(commandHookCoreBasePkg)) {
writeFileSync(commandHookCoreBasePkg, `{}`);
writeFileSync(commandHookCoreBasePkg, '{}');
}

export const getCoreBaseDir = () => {
Expand All @@ -38,8 +38,9 @@ async function getNpmPath(

scope.coreInstance.cli.log(`Installing ${npmName}`);
execSync(
`cd ${commandHookCoreBaseDir};${npmRegistry ||
'npm'} i ${npmName} --production`
`cd ${commandHookCoreBaseDir};${
npmRegistry || 'npm'
} i ${npmName} --production`
);

return globalNpmPath;
Expand Down
11 changes: 7 additions & 4 deletions packages/faas-cli-command-core/src/pluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class PluginManager extends BasePlugin {
);
this.core.cli.log(` - installed '${plugin}'`);
} else {
this.core.cli.log(`please user plugin -i=<plugin name>`);
this.core.cli.log('please user plugin -i=<plugin name>');
}
}

Expand All @@ -104,7 +104,9 @@ export class PluginManager extends BasePlugin {
let version = '';
try {
version = JSON.parse(readFileSync(pkgJson).toString()).version;
} catch (E) {}
} catch (E) {
/** ignore */
}
return version;
}

Expand All @@ -114,8 +116,9 @@ export class PluginManager extends BasePlugin {
const dir: string = getCoreBaseDir();
await this.execPromise(`cd ${dir};rm -rf ${plugin}`);
await this.execPromise(
`cd ${join(dir, '../')};${this.options.npm ||
'npm'} install ${newPlugin} --production`
`cd ${join(dir, '../')};${
this.options.npm || 'npm'
} install ${newPlugin} --production`
);
const newVersion = this.getPluginVersion(plugin);
this.core.cli.log(
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-command-core/test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('command-core', () => {
result.push(msg);
},
},
stopLifecycle: 'invoke:one'
stopLifecycle: 'invoke:one',
});
core.addPlugin(TestPlugin);
await core.ready();
Expand Down
4 changes: 2 additions & 2 deletions packages/faas-cli-command-core/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ describe('errorMap', () => {
);
assert(
ErrorMap('localPlugin', { path: 'test', err: { message: '4' } })
.message === `load local plugin 'test' error '4'`
.message === "load local plugin 'test' error '4'"
);
assert(
ErrorMap('npmPlugin', { path: 'test', err: { message: '5' } }).message ===
`load npm plugin 'test' error '5'`
"load npm plugin 'test' error '5'"
);
assert(
ErrorMap('pluginType', {}).message ===
Expand Down
1 change: 0 additions & 1 deletion packages/faas-cli-command-core/test/plugins/store.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { BasePlugin } from '../../src';

class StoreGet extends BasePlugin {
Expand Down
Loading

0 comments on commit fe3a74a

Please sign in to comment.