Skip to content

Commit f1c639e

Browse files
Resolve more linting and reduce app loads
Signed-off-by: Peter Zhu <zhujiaxi@amazon.com>
1 parent fba9a75 commit f1c639e

File tree

7 files changed

+26
-31
lines changed

7 files changed

+26
-31
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = {
2727
'max-classes-per-file': 'off',
2828
'no-new': 'off',
2929
"no-console": "off",
30+
"no-underscore-dangle": 'off',
3031
'max-len': ['error', { code: 160, ignoreComments: true }],
3132
},
3233
};

package-lock.json

+7-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"dedent": "^1.5.1",
2929
"js-yaml": "^4.1.0",
3030
"probot": "^13.0.1",
31-
"randomized-string": "^2.0.1",
3231
"randomstring": "^1.3.0",
3332
"ts-yaml": "^1.0.0"
3433
},
@@ -39,6 +38,7 @@
3938
"@types/jest": "^29.5.12",
4039
"@types/js-yaml": "^4.0.9",
4140
"@types/node": "^20.0.0",
41+
"@types/randomstring": "^1.3.0",
4242
"@typescript-eslint/eslint-plugin": "^7.18.0",
4343
"@typescript-eslint/parser": "^7.18.0",
4444
"eslint": "^8.57.1",

src/config/config.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { load, YAMLException } from 'js-yaml';
44
import { readFileSync, realpathSync } from 'fs';
55
import { ResourceData, OperationData } from './types';
66

7-
export abstract class Config {
7+
export class Config {
88
protected configType: string;
99

1010
protected configData: ResourceData | OperationData;
@@ -15,10 +15,10 @@ export abstract class Config {
1515
this.configType = configType;
1616
}
1717

18-
protected readConfig(filePath: string): any {
18+
protected static readConfig(filePath: string): any {
1919
const realFilePath = realpathSync(filePath);
2020
try {
21-
console.log(`Loading ${this.configType} config: ${realFilePath}`);
21+
console.log(`Loading config: ${realFilePath}`);
2222
// Easier to convert YAML to JSON then parse and validate
2323
const yaml2json = JSON.stringify(load(readFileSync(realFilePath, 'utf-8')));
2424
console.log(yaml2json);
@@ -29,10 +29,11 @@ export abstract class Config {
2929
} else {
3030
console.error('Error:', e);
3131
}
32+
return null;
3233
}
3334
}
3435

35-
protected validateConfig(data: ResourceData | OperationData, schema: any): void {
36+
protected static validateConfig(data: ResourceData | OperationData, schema: any): void {
3637
console.log('Validating Schema......');
3738
const ajv = new Ajv();
3839
addFormats(ajv);

src/config/operation-config.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,30 @@ export class OperationConfig extends Config {
4646

4747
constructor(configPath: string, app: Probot) {
4848
super('OperationConfig');
49-
this.configData = super.readConfig(configPath);
49+
this.configData = OperationConfig.readConfig(configPath);
5050
this.configSchema = OperationConfig.configSchema;
51-
super.validateConfig(this.configData, this.configSchema);
51+
OperationConfig.validateConfig(this.configData, this.configSchema);
5252
this.app = app;
5353
}
5454

5555
public getApp(): Probot {
5656
return this.app;
5757
}
5858

59-
private async _initTasks(taskDataArray: TaskData[]): Promise<Task[]> {
60-
const taskObjArray: Task[] = [];
61-
for (const taskData of taskDataArray) {
59+
private static async _initTasks(taskDataArray: TaskData[]): Promise<Task[]> {
60+
const taskObjArray = taskDataArray.map((taskData) => {
6261
const taskObj = new Task(taskData.call, taskData.args, taskData.name);
6362
console.log(`Setup Task: ${taskObj.getName()}`);
64-
taskObjArray.push(taskObj);
65-
}
63+
return taskObj;
64+
});
6665
return taskObjArray;
6766
}
6867

6968
public async initOperation(): Promise<Operation> {
7069
const opObj = new Operation(
7170
(this.configData as OperationData).name,
7271
(this.configData as OperationData).events,
73-
await this._initTasks((this.configData as OperationData).tasks),
72+
await OperationConfig._initTasks((this.configData as OperationData).tasks),
7473
);
7574
console.log(`Setup Operation: ${opObj.getName()}`);
7675
return opObj;

src/config/resource-config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ export class ResourceConfig extends Config {
7070

7171
constructor(configPath: string, octokit: ProbotOctokit) {
7272
super('ResourceConfig');
73-
this.configData = super.readConfig(configPath);
73+
this.configData = ResourceConfig.readConfig(configPath);
7474
this.configSchema = ResourceConfig.configSchema;
75-
super.validateConfig(this.configData, this.configSchema);
75+
ResourceConfig.validateConfig(this.configData, this.configSchema);
7676
this.octokit = octokit;
7777
}
7878

src/service/operation/task.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import randomstring from 'randomstring';
12
import { TaskArgData } from '../../config/types';
23

34
export class Task {
@@ -11,14 +12,9 @@ export class Task {
1112

1213
constructor(call: string, callArgs: TaskArgData, name?: string) {
1314
const callArray = call.trim().split('@');
14-
this.callName = callArray[0];
15-
this.callFunc = callArray[1];
15+
[this.callName, this.callFunc] = callArray;
1616
this.callArgs = callArgs;
17-
const randomString = require('randomstring');
18-
const namePostfix = randomString.generate({
19-
length: 8,
20-
charset: 'alphanumeric',
21-
});
17+
const namePostfix = randomstring.generate(8);
2218
this.name = name ? `${name}#${namePostfix}` : `${this.callName}#${namePostfix}`;
2319
}
2420

0 commit comments

Comments
 (0)