Skip to content

Commit 10f0470

Browse files
authored
CM-40774, CM-40773, CM-40772 - Code refactoring (#104)
1 parent 883295d commit 10f0470

File tree

113 files changed

+1723
-1427
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1723
-1427
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules
33
# don't lint build output
44
dist
5+
out

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = {
4040
'format': ['camelCase', 'UPPER_CASE', 'PascalCase'],
4141
},
4242
],
43+
'@typescript-eslint/restrict-plus-operands': 'error',
4344
// FIXME(MarshalX): refactor code and enable these rules:
4445
'no-unused-vars': 0,
4546
'camelcase': 0,
@@ -49,7 +50,6 @@ module.exports = {
4950
'@typescript-eslint/no-unsafe-return': 0,
5051
'@typescript-eslint/no-unsafe-argument': 0,
5152
'@typescript-eslint/no-explicit-any': 0,
52-
'@typescript-eslint/restrict-plus-operands': 0,
5353
'@typescript-eslint/restrict-template-expressions': 0,
5454
'@typescript-eslint/no-unsafe-call': 0,
5555
},

.github/workflows/lint_test_build.yml renamed to .github/workflows/lint_build.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Lint, test and build
1+
name: Lint and build
22

33
on: [ push ]
44

@@ -33,21 +33,15 @@ jobs:
3333
- name: Code Linting
3434
run: npm run lint
3535

36-
- name: Run tests
37-
run: |
38-
sudo apt-get install xvfb
39-
export DISPLAY=:99.0
40-
Xvfb -ac :99 -screen 0 1920x1080x16 &
41-
npm run test
42-
4336
- name: Build extension
4437
env:
4538
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
4639
run: |
4740
npm install -g @vscode/vsce
4841
vsce package
4942
mv *.vsix cycode-extension.vsix
50-
- name: Upload extension package
43+
44+
- name: Upload extension package to artifacts
5145
uses: actions/upload-artifact@v3
5246
with:
5347
name: cycode-extension.vsix

LICENSE.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Cycode Ltd.
3+
Copyright (c) 2024 Cycode Ltd.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package.json

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@
220220
"type": "webview",
221221
"id": "cycode.view.main",
222222
"name": "scan",
223-
"when": "cycode:auth.isAuthed && (!cycode:scan.hasDetections || !cycode:treeView.isShowed)"
223+
"when": "cycode:auth.isAuthed && (!cycode:scan.hasAnyDetections || !cycode:treeView.isShowed)"
224224
},
225225
{
226226
"type": "tree",
227227
"id": "cycode.view.tree",
228228
"name": "Scan Results",
229-
"when": "cycode:scan.hasDetections && cycode:treeView.isShowed"
229+
"when": "cycode:scan.hasAnyDetections && cycode:treeView.isShowed"
230230
}
231231
]
232232
},
@@ -241,15 +241,11 @@
241241
}
242242
},
243243
"scripts": {
244-
"vscode:prepublish": "yarn run package",
245-
"compile": "webpack",
244+
"lint": "eslint src --ext ts",
246245
"watch": "webpack --watch",
246+
"compile": "webpack",
247247
"package": "webpack --mode production",
248-
"compile-tests": "tsc -p . --outDir out",
249-
"watch-tests": "tsc -p . -w --outDir out",
250-
"pretest": "yarn run compile-tests && yarn run compile && yarn run lint",
251-
"lint": "eslint src --ext ts",
252-
"test": "node ./out/test/runTest.js"
248+
"vscode:prepublish": "yarn run package"
253249
},
254250
"devDependencies": {
255251
"@sentry/webpack-plugin": "^2.21.1",
@@ -276,8 +272,10 @@
276272
"dependencies": {
277273
"@sentry/node": "^8.20.0",
278274
"decompress": "^4.2.1",
275+
"reflect-metadata": "^0.2.2",
279276
"semver": "7.5.4",
280277
"shelljs": "0.8.5",
281-
"showdown": "^2.1.0"
278+
"showdown": "^2.1.0",
279+
"tsyringe": "^4.8.0"
282280
}
283281
}

src/cli-wrapper/runner.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {spawn} from 'child_process';
22
import * as os from 'os';
3-
import {extensionOutput} from '../logging/extension-output';
43
import {CommandResult, RunCliArgs, RunCliResult} from './types';
4+
import {container} from 'tsyringe';
5+
import {ILoggerService} from '../services/logger-service';
6+
import {LoggerServiceSymbol} from '../symbols';
57

68
const parseResult = (out: string): object => {
79
let result = {};
@@ -17,6 +19,7 @@ const parseResult = (out: string): object => {
1719
};
1820

1921
export const getRunnableCliCommand = (args: RunCliArgs): RunCliResult => {
22+
const logger = container.resolve<ILoggerService>(LoggerServiceSymbol);
2023
const {
2124
cliPath,
2225
cliEnv,
@@ -25,7 +28,7 @@ export const getRunnableCliCommand = (args: RunCliArgs): RunCliResult => {
2528
printToOutput,
2629
} = args;
2730

28-
extensionOutput.info(
31+
logger.debug(
2932
`Running command: "${cliPath} ${commandParams.join(' ')}"`
3033
);
3134

@@ -39,7 +42,7 @@ export const getRunnableCliCommand = (args: RunCliArgs): RunCliResult => {
3942
});
4043

4144
const getCancelPromise = () => new Promise<void>((resolve) => {
42-
extensionOutput.info(
45+
logger.debug(
4346
`Killing child process: "${cliPath} ${commandParams.join(' ')}"`
4447
);
4548

@@ -58,7 +61,7 @@ export const getRunnableCliCommand = (args: RunCliArgs): RunCliResult => {
5861
}
5962
stderr += data.toString();
6063
if (printToOutput) {
61-
extensionOutput.debug(data.toString());
64+
logger.debug(data.toString());
6265
}
6366
};
6467

@@ -67,13 +70,13 @@ export const getRunnableCliCommand = (args: RunCliArgs): RunCliResult => {
6770

6871
childProcess.on('exit', (code: number) => {
6972
// exit occurs earlier than close
70-
extensionOutput.debug(`Command exited with code: ${code}`);
73+
logger.debug(`Command exited with code: ${code}`);
7174
exitCode = code;
7275
});
7376

7477
childProcess.on('close', (code: number) => {
7578
// we receive all "data" events before close
76-
extensionOutput.debug(`Streams of a command have been closed with code: ${code}`);
79+
logger.debug(`Streams of a command have been closed with code: ${code}`);
7780
resolve({
7881
exitCode: exitCode,
7982
stderr: stderr,
@@ -92,7 +95,7 @@ export const getRunnableCliCommand = (args: RunCliArgs): RunCliResult => {
9295

9396
childProcess.stdout?.on('data', (data) => {
9497
if (printToOutput) {
95-
extensionOutput.info(`Command stdout: ${data.toString()}`);
98+
logger.debug(`Command stdout: ${data.toString()}`);
9699
}
97100

98101
if (!data) {

src/commands/auth-command.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as vscode from 'vscode';
2+
import {config, validateConfig} from '../utils/config';
3+
import {auth} from '../services/auth';
4+
5+
export default () => {
6+
if (validateConfig()) {
7+
return;
8+
}
9+
10+
const params = {
11+
config,
12+
workspaceFolderPath:
13+
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath || '',
14+
};
15+
16+
auth(params);
17+
};

src/commands/iac-scan-command.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as vscode from 'vscode';
2+
import TrayNotifications from '../utils/tray-notifications';
3+
import {config, validateConfig} from '../utils/config';
4+
import {iacScan} from '../services/scanners/iac-scanner';
5+
import {container} from 'tsyringe';
6+
import {IExtensionService} from '../services/extension-service';
7+
import {ExtensionServiceSymbol} from '../symbols';
8+
9+
export default () => {
10+
// scan the current open document if opened
11+
12+
if (!vscode.window.activeTextEditor?.document ||
13+
vscode.window?.activeTextEditor?.document?.uri.scheme === 'output'
14+
) {
15+
TrayNotifications.showMustBeFocusedOnFile();
16+
17+
return;
18+
}
19+
20+
if (validateConfig()) {
21+
return;
22+
}
23+
24+
const extension = container.resolve<IExtensionService>(ExtensionServiceSymbol);
25+
26+
iacScan(
27+
{
28+
config,
29+
pathToScan: vscode.window.activeTextEditor.document.fileName,
30+
workspaceFolderPath: vscode.workspace.workspaceFolders?.[0]?.uri.fsPath,
31+
diagnosticCollection: extension.diagnosticCollection,
32+
onDemand: true,
33+
},
34+
extension.treeView,
35+
);
36+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as vscode from 'vscode';
2+
import {config, validateConfig} from '../utils/config';
3+
import {iacScan} from '../services/scanners/iac-scanner';
4+
import {container} from 'tsyringe';
5+
import {IExtensionService} from '../services/extension-service';
6+
import {ExtensionServiceSymbol} from '../symbols';
7+
8+
export default () => {
9+
if (validateConfig()) {
10+
return;
11+
}
12+
13+
const projectPath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
14+
if (!projectPath) {
15+
return;
16+
}
17+
18+
const extension = container.resolve<IExtensionService>(ExtensionServiceSymbol);
19+
20+
iacScan(
21+
{
22+
config,
23+
pathToScan: projectPath,
24+
workspaceFolderPath: projectPath,
25+
diagnosticCollection: extension.diagnosticCollection,
26+
onDemand: true,
27+
},
28+
extension.treeView,
29+
);
30+
};

src/commands/ignore-command.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as vscode from 'vscode';
2+
import {IgnoreCommandConfig} from '../types/commands';
3+
import {config, validateConfig} from '../utils/config';
4+
import {ignore} from '../services/ignore';
5+
import {container} from 'tsyringe';
6+
import {IExtensionService} from '../services/extension-service';
7+
import {ExtensionServiceSymbol} from '../symbols';
8+
9+
export default async (ignoreConfig: IgnoreCommandConfig) => {
10+
if (validateConfig()) {
11+
return;
12+
}
13+
14+
const extension = container.resolve<IExtensionService>(ExtensionServiceSymbol);
15+
16+
await ignore({
17+
config,
18+
workspaceFolderPath: vscode.workspace.workspaceFolders?.[0]?.uri.fsPath,
19+
ignoreConfig,
20+
diagnosticCollection: extension.diagnosticCollection,
21+
treeView: extension.treeView,
22+
});
23+
};
24+

0 commit comments

Comments
 (0)