diff --git a/src/commands/add.ts b/src/commands/add.ts index 785ddb6..b1d3a58 100644 --- a/src/commands/add.ts +++ b/src/commands/add.ts @@ -1,9 +1,9 @@ -import { readFileSync } from "node:fs"; +import { readFile } from "node:fs/promises"; import { coloredLog } from "../functions/colored-log.js"; import { BlobObject } from "../models/blob-object.js"; -export const add = (options: Array): void => { +export const add = async (options: Array): Promise => { const filePath = options[0]; //引数にファイルパスが含まれていなかった場合の処理 @@ -16,8 +16,8 @@ export const add = (options: Array): void => { return; } - const content = readFileSync(filePath); + const content = await readFile(filePath); const blobObject = new BlobObject(content); - blobObject.dumpBlobObject(); + await blobObject.dumpBlobObject(); }; diff --git a/src/commands/log.ts b/src/commands/log.ts index 2c86fca..8340dda 100644 --- a/src/commands/log.ts +++ b/src/commands/log.ts @@ -1,37 +1,40 @@ -import { existsSync, readFileSync } from "node:fs"; +import { readFile } from "node:fs/promises"; import { join } from "path"; import { GIT_DIR } from "../constants.js"; import { coloredLog } from "../functions/colored-log.js"; +import { exists } from "../functions/exists.js"; import { Commit, CommitFieldType } from "../models/commit.js"; -const extractHeadHash = (): string | undefined => { +const extractHeadHash = async (): Promise => { const headPath = join(GIT_DIR, "HEAD"); - if (!existsSync(headPath)) { + if (!(await exists(headPath))) { return; } - const headText = readFileSync(headPath).toString("utf-8"); + const headText = await readFile(headPath).then((head) => + head.toString("utf-8"), + ); const refPrefix = "ref: "; //ブランチ名かコミットハッシュのどちらをHEADに持つかを識別して出し分ける if (headText.startsWith(refPrefix)) { - return readFileSync( + return await readFile( join(GIT_DIR, headText.slice(refPrefix.length)).trim(), "utf-8", - ).trim(); + ).then((path) => path.trim()); } else { return headText.trim(); } }; -const getCommitHistory = ( +const getCommitHistory = async ( hash: string, history: Array = [], -): Array => { +): Promise> => { const commit = new Commit(); - commit.setCommit(hash); + await commit.setCommit(hash); const commitData = commit.getCommit(); const currentHistory = [...history, commitData]; @@ -57,15 +60,15 @@ export const displayCommitHistory = ( }); }; -export const log = (_options?: Array): void => { - const headHash = extractHeadHash(); +export const log = async (_options?: Array): Promise => { + const headHash = await extractHeadHash(); if (!headHash) { console.log("there is no commit."); return; } - const commitHistory = getCommitHistory(headHash); + const commitHistory = await getCommitHistory(headHash); displayCommitHistory(commitHistory); }; diff --git a/src/functions/exists.ts b/src/functions/exists.ts index 90197a1..05c9089 100644 --- a/src/functions/exists.ts +++ b/src/functions/exists.ts @@ -6,4 +6,4 @@ export const exists = async (filePath: string): Promise => { () => true, () => false, ); -} \ No newline at end of file +}; diff --git a/src/functions/read-file.ts b/src/functions/read-file.ts deleted file mode 100644 index aba55f9..0000000 --- a/src/functions/read-file.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { readFileSync } from "node:fs"; -import { inflateSync } from "node:zlib"; - -export const readFile = (path: string): string => { - const file = new Uint8Array(readFileSync(path)); - - return inflateSync(file).toString(); -}; diff --git a/src/functions/read-git-object.ts b/src/functions/read-git-object.ts new file mode 100644 index 0000000..f146a90 --- /dev/null +++ b/src/functions/read-git-object.ts @@ -0,0 +1,8 @@ +import { readFile } from "node:fs/promises"; +import { inflateSync } from "node:zlib"; + +export const readGitObject = async (path: string): Promise => { + const file = Uint8Array.from(await readFile(path)); + + return inflateSync(file).toString(); +}; diff --git a/src/models/blob-object.ts b/src/models/blob-object.ts index 7d61275..349e493 100644 --- a/src/models/blob-object.ts +++ b/src/models/blob-object.ts @@ -1,13 +1,14 @@ import { createHash } from "node:crypto"; -import { existsSync, mkdirSync, writeFileSync } from "node:fs"; +import { mkdir, writeFile } from "node:fs/promises"; import { deflateSync } from "node:zlib"; +import { exists } from "../functions/exists.js"; import { generateObjectPath } from "../functions/generate-object-path.js"; export class BlobObject { constructor(private readonly content: Buffer) {} - public dumpBlobObject = (): void => { + public dumpBlobObject = async (): Promise => { const header = Buffer.from(`blob ${this.content.length.toString()}\x00`); const store = Buffer.concat([ Uint8Array.from(header), @@ -22,10 +23,10 @@ export class BlobObject { const { dirPath, filePath } = generateObjectPath(hash); const compressedBlobObject = deflateSync(Uint8Array.from(store)); - if (existsSync(filePath)) return; + if (await exists(filePath)) return; - if (!existsSync(dirPath)) mkdirSync(dirPath); + if (!(await exists(dirPath))) await mkdir(dirPath); - writeFileSync(filePath, Uint8Array.from(compressedBlobObject)); + await writeFile(filePath, Uint8Array.from(compressedBlobObject)); }; } diff --git a/src/models/commit.ts b/src/models/commit.ts index 8886539..0cd4bc8 100644 --- a/src/models/commit.ts +++ b/src/models/commit.ts @@ -1,7 +1,7 @@ import { join } from "node:path"; import { GIT_OBJECTS } from "../constants.js"; -import { readFile } from "../functions/read-file.js"; +import { readGitObject } from "../functions/read-git-object.js"; export interface CommitFieldType { tree: string; @@ -28,8 +28,8 @@ export class Commit { this.hash = ""; } - public setCommit = (hash: string): void => { - const content = this.getCommitContent(hash); + public setCommit = async (hash: string): Promise => { + const content = await this.getCommitContent(hash); this.parseCommit(hash, content); }; @@ -44,13 +44,13 @@ export class Commit { }; }; - private getCommitContent = (hash: string): string => { + private getCommitContent = async (hash: string): Promise => { const dirName = hash.slice(0, 2); const fileName = hash.slice(2); const path = join(GIT_OBJECTS, dirName, fileName); - return readFile(path); + return await readGitObject(path); }; private parseCommit = (hash: string, content: string): void => { diff --git a/src/mygit.ts b/src/mygit.ts index f5b8bb2..0f3f9f7 100644 --- a/src/mygit.ts +++ b/src/mygit.ts @@ -17,7 +17,7 @@ export const mygit = async (argv: Array): Promise => { if (runCommand) { const options = argv.slice(3); - runCommand(options); + await runCommand(options); } else { console.log(`mygit: '${command}' is not a valid mygit command.\n`); validCommand.help();