Skip to content

Commit 2e09b66

Browse files
CLIS-78 Adds method to scan folder from dependency scanner and updates README.md
1 parent 55c9e0c commit 2e09b66

File tree

7 files changed

+46
-19
lines changed

7 files changed

+46
-19
lines changed

Diff for: README.md

+30-12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ On the other hand, if you need to install the module in your own Node.js project
2626
Running the bare command will list the available sub-commands:
2727

2828
```Usage: scanoss-js [options] [command]
29+
Usage: scanoss-js [options] [command]
2930
3031
The SCANOSS JS package provides a simple, easy to consume module for interacting with SCANOSS APIs/Engine.
3132
@@ -35,17 +36,19 @@ Options:
3536
3637
Commands:
3738
scan [options] <source> Scan a folder/file
39+
dep [options] <source> Scan for dependencies
40+
wfp [options] <source> Generates fingerprints for a folder/file
3841
help [command] display help for command
3942
```
4043

4144
From there it is possible to scan a source code folder:
4245

4346
`scanoss-js scan -o scan-output.json <source-folder>`
4447

45-
## Package Usage
48+
## SDK Usage
49+
The SDK provides a simple way to interact with the Scanoss APIs from your JS code. Here are two examples for performing code scanning and dependency scanning
4650

47-
The Scanoss package can be used programmatically as a standard Node module.
48-
A simple example that scans two files and writes the result in the project folder is shown below:
51+
### Code Scanning
4952

5053
```typescript
5154
// Import as ES6
@@ -77,10 +80,8 @@ const scannerInput = {
7780
scanner.scan([scannerInput]);
7881
```
7982

80-
### Events
81-
82-
The module provides a set of events that can be used to trigger actions.
83-
Some events are shown in the example above.
83+
The scanner object provides a set of events that can be used to trigger custom actions.
84+
These events are listed in the table above and were previously mentioned.
8485

8586
| Event Name | Description |
8687
| ------------------- | ----------------------------------- |
@@ -89,10 +90,27 @@ Some events are shown in the example above.
8990
| DISPATCHER_NEW_DATA | New data received but not persisted |
9091
| RESULTS_APPENDED | Results added to scan report file |
9192

92-
# Build and publish the module
9393

94-
In order to build and publish the package is a requisite to have installed `yarn`. For more information https://yarnpkg.com/getting-started/install
94+
### Dependency Scanning
95+
```typescript
96+
import { DependencyScanner, DependencyScannerCfg } from "scanoss";
97+
98+
const main = async () => {
99+
100+
const dependencyScanner = new DependencyScanner();
101+
102+
//Scan a full folder
103+
const results = await dependencyScanner.scanFolder("./node_modules")
104+
105+
//Scan specific files
106+
//const results = await dependencyScanner.scan(["./package.json", "package-lock.json"])
107+
108+
}
109+
110+
main();
111+
```
112+
# Build and test the module
95113

96-
- `yarn install` will install the dependencies.
97-
- `yarn build` will build the module.
98-
- `yarn publish` will publish the module.
114+
- `npm install` will install the dependencies.
115+
- `npm run build` will build the module.
116+
- `npm run test` will publish the module.

Diff for: package-lock.json

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

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scanoss",
3-
"version": "0.8.8",
3+
"version": "0.9.0",
44
"description": "The SCANOSS JS package provides a simple, easy to consume module for interacting with SCANOSS APIs/Engine.",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",

Diff for: src/sdk/Dependencies/DependencyScanner.ts

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { LocalDependencies } from "./LocalDependency/LocalDependency";
55
import { DependencyScannerCfg } from "./DependencyScannerCfg";
66
import { IDependencyResponse } from "./DependencyTypes";
77
import { PackageURL } from "packageurl-js";
8+
import fs from 'fs';
9+
import { Tree } from '../tree/Tree';
810

911
export class DependencyScanner {
1012

@@ -29,6 +31,13 @@ export class DependencyScanner {
2931
this.localDependency = new LocalDependencies();
3032
}
3133

34+
public async scanFolder(path: string) {
35+
if (!(await fs.promises.lstat(path)).isDirectory()) throw new Error("Specified path is not a directory");
36+
const tree = new Tree(path)
37+
tree.build();
38+
return await this.scan(tree.getFileList())
39+
}
40+
3241

3342
public async scan(files: Array<string>): Promise<IDependencyResponse> {
3443
let localDependencies = await this.localDependency.search(files);

Diff for: src/sdk/tree/File.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export default class File extends Node {
1414
return null;
1515
}
1616

17-
public getFiles(f: Filter): Array<string> {
18-
if (!f || !f.evaluate(this)) return [];
17+
public getFiles(f?: Filter): Array<string> {
18+
if (f && !f.evaluate(this)) return [];
1919
return [this.getPath()];
2020
}
2121

Diff for: src/sdk/tree/Folder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default class Folder extends Node {
4242
}
4343

4444
public getFiles(f?: Filter): Array<string> {
45-
if (!f || !f.evaluate(this)) return [];
45+
if (f && !f.evaluate(this)) return [];
4646

4747
const files: Array<string> = [];
4848
this.children.forEach((child) => {

Diff for: src/sdk/tree/Node.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default abstract class Node {
1717

1818
public abstract getNode(path: string): Node;
1919

20-
public abstract getFiles(f: Filter): Array<string>;
20+
public abstract getFiles(f?: Filter): Array<string>;
2121

2222
public getName(): string {
2323
return this.label;

0 commit comments

Comments
 (0)