-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: unused files tracking * benchmark: publish benchmark results from node_20.x * benchmark: publish benchmark results from node_18.x * feat: unused files tracking * benchmark: publish benchmark results from node_20.x * benchmark: publish benchmark results from node_18.x * feat: show unused dependencies in the cli * fix: config propagation * docs: add changeset * feat: add unused http api * docs: unused * benchmark: publish benchmark results from node_20.x * benchmark: publish benchmark results from node_18.x --------- Co-authored-by: skott_bot <skott.devtool@gmail.com>
- Loading branch information
1 parent
4d19c97
commit b13d3c9
Showing
23 changed files
with
644 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
"skott": minor | ||
--- | ||
|
||
Allow unused files to be tracked and reported. From the CLI, `--showUnusedFiles` can be used to report unused files. From the API, a new `collectUnusedFiles` method is accessible through the graph API: | ||
|
||
```js | ||
import skott from "skott"; | ||
|
||
const instance = await skott(); | ||
const unusedFiles = instance.useGraph().collectUnusedFiles(); | ||
``` | ||
|
||
This version also includes a fix for a bug related to `--trackBuiltinDependencies` and `--trackThirdPartyDependencies` that were not propagated anymore (since 0.34.0) when being provided from the CLI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import type { DiGraph } from "digraph-js"; | ||
|
||
import type { SkottConfig } from "../skott.js"; | ||
|
||
import type { SkottNode } from "./node.js"; | ||
|
||
export const CollectLevel = { | ||
Deep: "deep", | ||
Shallow: "shallow" | ||
} as const; | ||
|
||
export type CollectLevelValues = | ||
(typeof CollectLevel)[keyof typeof CollectLevel]; | ||
|
||
const skottToDiGraphTraversal = { | ||
deepFirst: "dfs", | ||
shallowFirst: "bfs" | ||
} as const; | ||
|
||
export class GraphApi<T> { | ||
constructor( | ||
private readonly graph: DiGraph<SkottNode<T>>, | ||
private readonly config: SkottConfig<T> | ||
) { | ||
this.getFileNode = this.getFileNode.bind(this); | ||
this.getNodes = this.getNodes.bind(this); | ||
this.traverseFiles = this.traverseFiles.bind(this); | ||
this.collectFilesDependencies = this.collectFilesDependencies.bind(this); | ||
this.collectFilesDependingOn = this.collectFilesDependingOn.bind(this); | ||
this.collectUnusedFiles = this.collectUnusedFiles.bind(this); | ||
this.hasCircularDependencies = this.hasCircularDependencies.bind(this); | ||
this.findCircularDependencies = this.findCircularDependencies.bind(this); | ||
this.findLeaves = this.findLeaves.bind(this); | ||
} | ||
|
||
getFileNode(id: string): SkottNode<T> { | ||
return this.getNodes()[id]; | ||
} | ||
|
||
getNodes() { | ||
return this.graph.toDict(); | ||
} | ||
|
||
*traverseFiles(options?: { | ||
rootFile?: string; | ||
moduleImportsCollection?: "deepFirst" | "shallowFirst"; | ||
}): Generator<SkottNode<T>, void, void> { | ||
const rootNode = options?.rootFile; | ||
const moduleImportsCollection = | ||
options?.moduleImportsCollection ?? "shallowFirst"; | ||
|
||
const traversal = skottToDiGraphTraversal[moduleImportsCollection]; | ||
|
||
if (rootNode) { | ||
return yield* this.graph.traverse({ | ||
rootVertexId: rootNode, | ||
traversal | ||
}); | ||
} | ||
|
||
return yield* this.graph.traverse({ | ||
traversal | ||
}); | ||
} | ||
|
||
collectFilesDependencies( | ||
rootFile: string, | ||
collectLevel: CollectLevelValues | ||
): SkottNode<T>[] { | ||
if (collectLevel === CollectLevel.Shallow) { | ||
return this.graph.getChildren(rootFile); | ||
} | ||
|
||
const nodes = this.getNodes(); | ||
const childrenIds = Array.from(this.graph.getDeepChildren(rootFile)); | ||
const dependencies = []; | ||
|
||
for (const id of childrenIds) { | ||
dependencies.push(nodes[id]); | ||
} | ||
|
||
return dependencies; | ||
} | ||
|
||
collectFilesDependingOn( | ||
rootFile: string, | ||
collectLevel: CollectLevelValues | ||
): SkottNode<T>[] { | ||
if (collectLevel === CollectLevel.Shallow) { | ||
return this.graph.getParents(rootFile); | ||
} | ||
|
||
const nodes = this.getNodes(); | ||
const parentIds = Array.from(this.graph.getDeepParents(rootFile)); | ||
const dependingOn = []; | ||
|
||
for (const id of parentIds) { | ||
dependingOn.push(nodes[id]); | ||
} | ||
|
||
return dependingOn; | ||
} | ||
|
||
collectUnusedFiles(): Array<SkottNode["id"]> { | ||
const leaves = this.findLeaves(); | ||
const unused = []; | ||
|
||
for (const leaf of leaves) { | ||
const node = this.getFileNode(leaf); | ||
const noNodesDependingOn = | ||
this.collectFilesDependingOn(leaf, CollectLevel.Deep).length === 0; | ||
if (noNodesDependingOn) { | ||
unused.push(node.id); | ||
} | ||
} | ||
|
||
return unused; | ||
} | ||
|
||
hasCircularDependencies(): boolean { | ||
return this.graph.hasCycles({ | ||
maxDepth: this.config.circularMaxDepth ?? Number.POSITIVE_INFINITY | ||
}); | ||
} | ||
|
||
findCircularDependencies(): Array<Array<SkottNode["id"]>> { | ||
return this.graph.findCycles({ | ||
maxDepth: this.config.circularMaxDepth ?? Number.POSITIVE_INFINITY | ||
}); | ||
} | ||
|
||
findLeaves(): Array<SkottNode["id"]> { | ||
const nodes = this.getNodes(); | ||
const leaves = []; | ||
|
||
for (const node of Object.values(nodes)) { | ||
if (node.adjacentTo.length === 0) { | ||
leaves.push(node.id); | ||
} | ||
} | ||
|
||
return leaves; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import kleur from "kleur"; | ||
|
||
import type { InputConfig, RuntimeConfig } from "../config.js"; | ||
import { createRuntimeConfig } from "../instance.js"; | ||
|
||
export function toRuntimeConfigOrDie<T>(input: InputConfig<T>): RuntimeConfig { | ||
try { | ||
return createRuntimeConfig(input); | ||
} catch (error) { | ||
// @ts-expect-error | ||
console.log(`\n ${kleur.bold().red(error.message)}`); | ||
|
||
return process.exit(1); | ||
} | ||
} |
Oops, something went wrong.