Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blobモデルの実装 #3

Merged
merged 16 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ npm run lint:fix
# run the formatter
npm run format:check
npm run format:fix

# run all check or fix
npm run check
npm run fix

```

## publish the package
Expand Down
11 changes: 10 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export default [
allowExpressions: true,
},
],
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-unused-vars": [
"warn",
{
argsIgnorePattern: "^_",
},
],
"@typescript-eslint/array-type": [
"error",
{
Expand All @@ -61,6 +66,10 @@ export default [
{
files: ["**/*.js", "**/*.mjs"],
...eslint.configs.recommended,
//追加でルールを指定
rules: {
"no-unused-vars": ["error", { argsIgnorePattern: "^_.*$" }],
},
languageOptions: {
Copy link
Contributor Author

@hyphen-o hyphen-o Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo
_から始まる変数はunused扱いされたくないので,srcとdistどちらも無視するように設定しました

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good-badge
ナイスです!こういう環境整えるの大事

あとでyotsu sanにもsyncしておきましょう!
めっちゃ厳密なこと言うとPR分けた方が良いかもしれませんが、個人的にはこのくらいのサイズのPRならシュッと混ぜちゃっててもいいかなって思いました

sourceType: "module",
globals: {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"lint:fix": "eslint --fix .",
"format:check": "prettier --check .",
"format:fix": "prettier --write .",
"check": "npm run format:check && npm run lint:check",
"fix": "npm run format:fix && npm run lint:fix",
"build": "tsc",
"watch": "tsc --watch"
},
Expand Down
23 changes: 23 additions & 0 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { readFileSync } from "node:fs";

import { coloredLog } from "../functions/colored-log.js";
import { BlobObject } from "../models/blob-object.js";

export const add = (options: Array<string>): void => {
const filePath = options[0];

//引数にファイルパスが含まれていなかった場合の処理
if (!filePath) {
console.log("Nothing specified, nothing added.");
coloredLog({
text: "hint: Maybe you wanted to say 'git add XXX'?",
color: "yellow",
});
return;
}

const content = readFileSync(filePath);

const blobObject = new BlobObject(content);
blobObject.dumpBlobObject();
};
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { add } from "./add.js";
import { log } from "./log.js";

export const validCommand = {
add: add,
log: log,
help: () => {
console.log("Available commands:");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const displayCommitHistory = (
});
};

export const log = (): void => {
export const log = (_options?: Array<string>): void => {
const headHash = extractHeadHash();

if (!headHash) {
Expand Down
12 changes: 7 additions & 5 deletions src/functions/colored-log.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const colors = {
//https://qiita.com/shuhei/items/a61b4324fd5dbc1af79b
yellow: "\u001b[33m",
red: "\u001b[31m",
};

export const coloredLog = ({
text,
color,
}: {
text: string;
color?: "yellow";
color?: keyof typeof colors;
}): void => {
const RESET = "\u001b[0m";
const colors = {
//https://qiita.com/shuhei/items/a61b4324fd5dbc1af79b
yellow: "\u001b[33m",
};

if (color && color in colors) {
console.log(colors[color] + text + RESET);
Expand Down
17 changes: 17 additions & 0 deletions src/functions/generate-object-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { join } from "node:path";

import { GIT_OBJECTS } from "../constants.js";

export const generateObjectPath = (
hash: string,
): {
dirPath: string;
filePath: string;
} => {
const dirPath = join(GIT_OBJECTS, hash.slice(0, 2));
const filePath = join(GIT_OBJECTS, hash.slice(0, 2), hash.slice(2));
return {
dirPath,
filePath,
};
};
31 changes: 31 additions & 0 deletions src/models/blob-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createHash } from "node:crypto";
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import { deflateSync } from "node:zlib";

import { generateObjectPath } from "../functions/generate-object-path.js";

export class BlobObject {
constructor(private readonly content: Buffer) {}

public dumpBlobObject = (): void => {
const header = Buffer.from(`blob ${this.content.length.toString()}\x00`);
const store = Buffer.concat([
Uint8Array.from(header),
Uint8Array.from(this.content),
]);

//16進数表示のため,hexに変換
const hash = createHash("sha1")
.update(Uint8Array.from(store))
.digest("hex");

const { dirPath, filePath } = generateObjectPath(hash);
const compressedBlobObject = deflateSync(Uint8Array.from(store));

if (existsSync(filePath)) return;

if (!existsSync(dirPath)) mkdirSync(dirPath);

writeFileSync(filePath, Uint8Array.from(compressedBlobObject));
};
}
4 changes: 3 additions & 1 deletion src/mygit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const mygit = async (argv: Array<string>): Promise<void> => {
: undefined;

if (runCommand) {
runCommand();
const options = argv.slice(3);

runCommand(options);
} else {
console.log(`mygit: '${command}' is not a valid mygit command.\n`);
validCommand.help();
Expand Down