Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit d72db58

Browse files
committed
chore: add cosmiconfig as config provider
1 parent 96e96f9 commit d72db58

File tree

6 files changed

+191
-22
lines changed

6 files changed

+191
-22
lines changed

package-lock.json

Lines changed: 58 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
},
183183
"dependencies": {
184184
"convert-array-to-csv": "^2.0.0",
185+
"cosmiconfig": "^9.0.0",
185186
"lightning-flow-scanner-core": "4.45.0",
186187
"tabulator-tables": "^6.3.1",
187188
"uuid": "^11.0.5",

src/commands/handlers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { CacheProvider } from '../providers/cache-provider';
99
import { testdata } from '../store/testdata';
1010
import { OutputChannel } from '../providers/outputChannel';
1111

12+
const { USE_NEW_CONFIG: isUseNewConfig } = process.env;
13+
1214
export default class Commands {
1315
constructor(private context: vscode.ExtensionContext) {}
1416

@@ -30,6 +32,10 @@ export default class Commands {
3032
}
3133

3234
private async configRules() {
35+
if (isUseNewConfig) {
36+
await this.ruleConfiguration();
37+
return;
38+
}
3339
const allRules: core.IRuleDefinition[] = [
3440
...core.getBetaRules(),
3541
...core.getRules(),
@@ -94,6 +100,8 @@ export default class Commands {
94100
);
95101
}
96102

103+
private async ruleConfiguration() {}
104+
97105
private async debugView() {
98106
let results = testdata as unknown as core.ScanResult[];
99107
await CacheProvider.instance.set('results', results);

src/providers/config-provider.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { cosmiconfig, CosmiconfigResult } from 'cosmiconfig';
2+
import { IRulesConfig } from 'lightning-flow-scanner-core';
3+
4+
export class ConfigProvider {
5+
public async loadConfig(configPath?: string): Promise<IRulesConfig> {
6+
const moduleName = 'flow-scanner';
7+
const searchPlaces = [
8+
'package.json',
9+
`.${moduleName}.yaml`,
10+
`.${moduleName}.yml`,
11+
`.${moduleName}.json`,
12+
`config/.${moduleName}.yaml`,
13+
`config/.${moduleName}.yml`,
14+
`.flow-scanner`,
15+
];
16+
const explorer = cosmiconfig(moduleName, {
17+
searchPlaces,
18+
});
19+
let explorerResults: CosmiconfigResult;
20+
if (configPath) {
21+
// Forced config file name
22+
explorerResults = await explorer.load(configPath);
23+
}
24+
// Let cosmiconfig look for a config file
25+
explorerResults = explorerResults ?? (await explorer.search());
26+
return explorerResults?.config ?? {};
27+
}
28+
}

tests/commands/handlers.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,25 @@ describe('Commands', () => {
5252
CacheProvider.instance = instanceMock as any;
5353

5454
const outputMock = { logChannel: { debug: jest.fn() } };
55-
const outputSpy = jest.spyOn(OutputChannel, 'getInstance'); //.getInstance = outputMock as any;
55+
const outputSpy = jest.spyOn(OutputChannel, 'getInstance');
5656
outputSpy.mockReturnValue(outputMock as any);
5757

5858
const extensionContext = jest.fn();
5959
const command = new cmd.default(
6060
extensionContext as unknown as ExtensionContext
6161
);
6262

63-
await command['configRules']();
63+
await expect(async () => await command['configRules']()).not.toThrow();
6464
});
6565

66-
it('should read from configuration', async () => {});
66+
it('should read from configuration', async () => {
67+
const command = new cmd.default({} as any);
68+
await expect(command['ruleConfiguration']()).resolves.not.toThrow();
69+
});
6770

68-
it('should write to configuration', async () => {});
71+
it('should write to configuration', async () => {
72+
const command = new cmd.default({} as any);
73+
await expect(command['ruleConfiguration']()).resolves.not.toThrow();
74+
});
6975
});
7076
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { describe, it, expect, jest } from '@jest/globals';
2+
3+
import { ConfigProvider } from '../../src/providers/config-provider';
4+
import * as cosmi from 'cosmiconfig';
5+
6+
jest.mock('cosmiconfig');
7+
8+
describe('Config-Provider', () => {
9+
it('should be defined', () => {
10+
expect(ConfigProvider).toBeDefined();
11+
});
12+
13+
it('should not error when no config', async () => {
14+
const cosmiMock = {
15+
load: jest.fn(),
16+
search: jest.fn(),
17+
};
18+
const cosmiSpy = jest.spyOn(cosmi, 'cosmiconfig');
19+
cosmiSpy.mockReturnValue(cosmiMock as any);
20+
21+
const configProvider = new ConfigProvider();
22+
await expect(configProvider.loadConfig()).resolves.toStrictEqual({});
23+
});
24+
25+
it('should load config when directly passed from settings', async () => {
26+
const cosmiMock = {
27+
load: jest.fn().mockImplementation(() => ({
28+
config: {
29+
rules: {
30+
FlowName: {
31+
severity: 'error',
32+
},
33+
},
34+
exceptions: {},
35+
},
36+
filepath: '',
37+
})),
38+
search: jest.fn(),
39+
};
40+
const cosmiSpy = jest.spyOn(cosmi, 'cosmiconfig');
41+
cosmiSpy.mockReturnValue(cosmiMock as any);
42+
43+
const configProvider = new ConfigProvider();
44+
await expect(
45+
configProvider.loadConfig('some config')
46+
).resolves.toStrictEqual({
47+
rules: {
48+
FlowName: {
49+
severity: 'error',
50+
},
51+
},
52+
exceptions: {},
53+
});
54+
expect(cosmiMock.search).not.toHaveBeenCalled();
55+
});
56+
57+
it('should resolve a config via workspace directory', async () => {
58+
const cosmiMock = {
59+
load: jest.fn(),
60+
search: jest.fn().mockImplementation(() => ({
61+
config: {
62+
rules: {
63+
APIVersion: {
64+
severity: 'error',
65+
},
66+
},
67+
exceptions: {},
68+
},
69+
filepath: '',
70+
})),
71+
};
72+
const cosmiSpy = jest.spyOn(cosmi, 'cosmiconfig');
73+
cosmiSpy.mockReturnValue(cosmiMock as any);
74+
75+
const configProvider = new ConfigProvider();
76+
await expect(configProvider.loadConfig()).resolves.toStrictEqual({
77+
rules: {
78+
APIVersion: {
79+
severity: 'error',
80+
},
81+
},
82+
exceptions: {},
83+
});
84+
expect(cosmiMock.load).not.toHaveBeenCalled();
85+
});
86+
});

0 commit comments

Comments
 (0)