-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a file manager, that tracks currently only on enums and in …
…which file these are stored
- Loading branch information
1 parent
58338c8
commit cb455f9
Showing
3 changed files
with
96 additions
and
26 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
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,36 @@ | ||
import { normalize } from "path" | ||
import { TypeDeclaration } from "../models/TypeDeclaration" | ||
import { L } from "./logger" | ||
|
||
export class FileManager { | ||
private files: Map<string, Array<string>> = new Map() | ||
|
||
public addTypeDeclarationToFile(file: string, declaration: TypeDeclaration) { | ||
const lp = [`<addTypeDeclarationToFile>`, file, declaration.identifier] | ||
L.d(...lp) | ||
const path = normalize(file) | ||
if (!this.files.has(path)) { | ||
L.d(...lp, "!hasPath", "we add an empty array on that entry") | ||
this.files.set(path, []) | ||
} | ||
|
||
const declarations = this.files.get(path)! // we know this because of the previous !has | ||
if (!declarations.includes(declaration.identifier)) { | ||
L.d(...lp, "the identifier is not included", "added the identifier to the array") | ||
declarations?.push(declaration.identifier) | ||
} else { | ||
L.d(...lp, "the identifier is already") | ||
} | ||
} | ||
|
||
public getFilePathOf(declaration: TypeDeclaration) { | ||
for (const entry of this.files) { | ||
const declarations = entry[1] | ||
if (declarations.includes(declaration.identifier)) { | ||
return entry[0] | ||
} | ||
} | ||
|
||
return null | ||
} | ||
} |
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,24 @@ | ||
import test from "ava" | ||
import { resolve } from "path" | ||
import { tmpdir } from "os" | ||
import { FileManager } from "../../src/utils/FileManager" | ||
import { StringTypeDeclaration } from "../../src/models/StringTypeDeclaration" | ||
|
||
const dir = tmpdir() | ||
|
||
test("fileManager should add a declaration to a file a path", (t) => { | ||
const manager = new FileManager() | ||
const declaration = new StringTypeDeclaration({ default: false, exported: true, identifier: "Hello" }) | ||
manager.addTypeDeclarationToFile(resolve(dir, "a.ts"), declaration) | ||
t.is(manager.getFilePathOf(declaration), resolve(dir, "a.ts")) | ||
}) | ||
|
||
test("fileManager should add two declarations to the same file", (t) => { | ||
const manager = new FileManager() | ||
const declaration1 = new StringTypeDeclaration({ default: false, exported: true, identifier: "Hello1" }) | ||
const declaration2 = new StringTypeDeclaration({ default: false, exported: true, identifier: "Hello2" }) | ||
manager.addTypeDeclarationToFile(resolve(dir, "a.ts"), declaration1) | ||
manager.addTypeDeclarationToFile(resolve(dir, "a.ts"), declaration2) | ||
t.is(manager.getFilePathOf(declaration1), resolve(dir, "a.ts")) | ||
t.is(manager.getFilePathOf(declaration2), resolve(dir, "a.ts")) | ||
}) |