From 3a93c1c81382e06670ca93269341149c4fe301f3 Mon Sep 17 00:00:00 2001 From: "Alexander J. Vincent" Date: Thu, 21 Mar 2024 20:56:20 -0700 Subject: [PATCH] #61, starting StringStringMap --- use-cases/build/StringStringMap.ts | 44 +++++++++++++++++++++++++-- use-cases/build/sharedProject.ts | 48 ++++++++++++++++++++++++++++++ use-cases/dist/StringStringMap.ts | 18 +++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 use-cases/build/sharedProject.ts create mode 100644 use-cases/dist/StringStringMap.ts diff --git a/use-cases/build/StringStringMap.ts b/use-cases/build/StringStringMap.ts index 6fb99b6..78b1fe6 100644 --- a/use-cases/build/StringStringMap.ts +++ b/use-cases/build/StringStringMap.ts @@ -1,5 +1,45 @@ -export default function buildStringStringMap(): Promise +import path from "path"; +import { + distDir, + project, + removeDistFile, + //getExistingSourceFile, +} from "./sharedProject.js"; +import { SourceFile } from "ts-morph"; + +export default async function buildStringStringMap(): Promise { - console.log("Hello World!"); + await removeDistFile("StringStringMap.ts"); + + /* This is a starting point, based on our needs: + - hashing two keys into one + - retrieving two keys from one + - storing the hashed keys and values in a private map + */ + const moduleFile: SourceFile = project.createSourceFile( + path.join(distDir, "StringStringMap.ts"), + ` +interface StringStringKey { + readonly firstKey: string, + readonly secondKey: string +} + +export default class StringStringMap { + static #hashKeys(firstKey: string, secondKey: string): string { + return JSON.stringify({firstKey, secondKey}); + } + + static #parseKeys(hashedKey: string): [string, string] + { + const { firstKey, secondKey } = JSON.parse(hashedKey) as StringStringKey; + return [firstKey, secondKey]; + } + + readonly #hashMap = new Map; +} + `.trim() + ); + + await moduleFile.save(); return Promise.resolve(); } diff --git a/use-cases/build/sharedProject.ts b/use-cases/build/sharedProject.ts new file mode 100644 index 0000000..41c1c86 --- /dev/null +++ b/use-cases/build/sharedProject.ts @@ -0,0 +1,48 @@ +import fs from "fs/promises"; +import path from "path"; +import url from "url"; + +import { + ModuleKind, + ModuleResolutionKind, + Project, + type ProjectOptions, + ScriptTarget, + SourceFile, +} from "ts-morph"; + +const TSC_CONFIG: ProjectOptions = { + "compilerOptions": { + "lib": ["es2022"], + "module": ModuleKind.ESNext, + "target": ScriptTarget.ESNext, + "moduleResolution": ModuleResolutionKind.NodeNext, + "sourceMap": true, + "declaration": true, + }, + skipAddingFilesFromTsConfig: true, + skipFileDependencyResolution: true, +}; + +export const stageDir = path.normalize(path.join(url.fileURLToPath(import.meta.url), "../../")); +export const distDir = path.join(stageDir, "dist"); + +export const project = new Project(TSC_CONFIG); + +export function getFullPath(pathToLocalFile: string): string { + return path.join(stageDir, "dist", pathToLocalFile); +} + +export async function removeDistFile( + pathToLocalFile: string +): Promise +{ + await fs.rm(getFullPath(pathToLocalFile), { force: true, recursive: true }); +} + +export function getExistingSourceFile( + absolutePathToFile: string +): SourceFile +{ + return project.getSourceFile(absolutePathToFile) ?? project.addSourceFileAtPath(absolutePathToFile); +} diff --git a/use-cases/dist/StringStringMap.ts b/use-cases/dist/StringStringMap.ts new file mode 100644 index 0000000..4f4807c --- /dev/null +++ b/use-cases/dist/StringStringMap.ts @@ -0,0 +1,18 @@ +interface StringStringKey { + readonly firstKey: string, + readonly secondKey: string +} + +export default class StringStringMap { + static #hashKeys(firstKey: string, secondKey: string): string { + return JSON.stringify({firstKey, secondKey}); + } + + static #parseKeys(hashedKey: string): [string, string] + { + const { firstKey, secondKey } = JSON.parse(hashedKey) as StringStringKey; + return [firstKey, secondKey]; + } + + readonly #hashMap = new Map; +} \ No newline at end of file