Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
sawa-zen committed Nov 6, 2024
0 parents commit ffdef69
Show file tree
Hide file tree
Showing 7 changed files with 595 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
5 changes: 5 additions & 0 deletions .gptignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
yarn.lock
dist
.gptignore
output.txt
91 changes: 91 additions & 0 deletions getRepositoryContents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as fs from "fs";
import * as path from "path";
import { minimatch } from "minimatch";

async function getIgnoreList(ignoreFilePath: string): Promise<string[]> {
let ignoreList: string[] = [];

try {
const data = await fs.promises.readFile(ignoreFilePath, "utf-8");
const lines = data.split(/\r?\n/);

for (let line of lines) {
line = line.trim().replace(/\\/g, "/");
if (line) {
ignoreList.push(line);
}
}
} catch (error) {
// エラー処理(必要に応じて追加)
}
return ignoreList;
}

function shouldIgnore(filePath: string, ignoreList: string[]): boolean {
const normalizedFilePath = filePath.split(path.sep).join("/");
for (const pattern of ignoreList) {
if (minimatch(normalizedFilePath, pattern)) {
return true;
}
}
return false;
}

export interface FileContent {
filePath: string;
content: string;
}

export async function getRepositoryContents(repoPath: string): Promise<FileContent[]> {
const fileContents: FileContent[] = [];

let ignoreFilePath = path.join(repoPath, ".gptignore");

let ignoreList: string[] = [];
try {
await fs.promises.access(ignoreFilePath, fs.constants.F_OK);
ignoreList = await getIgnoreList(ignoreFilePath);
} catch {
const here = path.dirname(__filename);
ignoreFilePath = path.join(here, ".gptignore");
try {
await fs.promises.access(ignoreFilePath, fs.constants.F_OK);
ignoreList = await getIgnoreList(ignoreFilePath);
} catch {
ignoreList = [];
}
}

async function walkDirectory(currentPath: string) {
const entries = await fs.promises.readdir(currentPath, {
withFileTypes: true,
});
for (const entry of entries) {
const fullPath = path.join(currentPath, entry.name);
const relativeFilePath = path.relative(repoPath, fullPath).split(path.sep).join("/");

if (shouldIgnore(relativeFilePath, ignoreList)) {
continue;
}

if (entry.isDirectory()) {
await walkDirectory(fullPath);
} else if (entry.isFile()) {
try {
const contentsBuffer = await fs.promises.readFile(fullPath);
const contents = contentsBuffer.toString("utf8");
fileContents.push({
filePath: relativeFilePath,
content: contents,
});
} catch (error) {
// エラー処理(必要に応じて追加)
}
}
}
}

await walkDirectory(repoPath);

return fileContents;
}
148 changes: 148 additions & 0 deletions output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
[
{
filePath: 'getRepositoryContents.ts',
content: 'import * as fs from "fs";\n' +
'import * as path from "path";\n' +
'import { minimatch } from "minimatch";\n' +
'\n' +
'async function getIgnoreList(ignoreFilePath: string): Promise<string[]> {\n' +
' let ignoreList: string[] = [];\n' +
'\n' +
' try {\n' +
' const data = await fs.promises.readFile(ignoreFilePath, "utf-8");\n' +
' const lines = data.split(/\\r?\\n/);\n' +
'\n' +
' for (let line of lines) {\n' +
' line = line.trim().replace(/\\\\/g, "/");\n' +
' if (line) {\n' +
' ignoreList.push(line);\n' +
' }\n' +
' }\n' +
' } catch (error) {\n' +
' // エラー処理(必要に応じて追加)\n' +
' }\n' +
' return ignoreList;\n' +
'}\n' +
'\n' +
'function shouldIgnore(filePath: string, ignoreList: string[]): boolean {\n' +
' const normalizedFilePath = filePath.split(path.sep).join("/");\n' +
' for (const pattern of ignoreList) {\n' +
' if (minimatch(normalizedFilePath, pattern)) {\n' +
' return true;\n' +
' }\n' +
' }\n' +
' return false;\n' +
'}\n' +
'\n' +
'export interface FileContent {\n' +
' filePath: string;\n' +
' content: string;\n' +
'}\n' +
'\n' +
'export async function getRepositoryContents(repoPath: string): Promise<FileContent[]> {\n' +
' const fileContents: FileContent[] = [];\n' +
'\n' +
' let ignoreFilePath = path.join(repoPath, ".gptignore");\n' +
'\n' +
' let ignoreList: string[] = [];\n' +
' try {\n' +
' await fs.promises.access(ignoreFilePath, fs.constants.F_OK);\n' +
' ignoreList = await getIgnoreList(ignoreFilePath);\n' +
' } catch {\n' +
' const here = path.dirname(__filename);\n' +
' ignoreFilePath = path.join(here, ".gptignore");\n' +
' try {\n' +
' await fs.promises.access(ignoreFilePath, fs.constants.F_OK);\n' +
' ignoreList = await getIgnoreList(ignoreFilePath);\n' +
' } catch {\n' +
' ignoreList = [];\n' +
' }\n' +
' }\n' +
'\n' +
' async function walkDirectory(currentPath: string) {\n' +
' const entries = await fs.promises.readdir(currentPath, {\n' +
' withFileTypes: true,\n' +
' });\n' +
' for (const entry of entries) {\n' +
' const fullPath = path.join(currentPath, entry.name);\n' +
' const relativeFilePath = path.relative(repoPath, fullPath).split(path.sep).join("/");\n' +
'\n' +
' if (shouldIgnore(relativeFilePath, ignoreList)) {\n' +
' continue;\n' +
' }\n' +
'\n' +
' if (entry.isDirectory()) {\n' +
' await walkDirectory(fullPath);\n' +
' } else if (entry.isFile()) {\n' +
' try {\n' +
' const contentsBuffer = await fs.promises.readFile(fullPath);\n' +
' const contents = contentsBuffer.toString("utf8");\n' +
' fileContents.push({\n' +
' filePath: relativeFilePath,\n' +
' content: contents,\n' +
' });\n' +
' } catch (error) {\n' +
' // エラー処理(必要に応じて追加)\n' +
' }\n' +
' }\n' +
' }\n' +
' }\n' +
'\n' +
' await walkDirectory(repoPath);\n' +
'\n' +
' return fileContents;\n' +
'}\n'
},
{
filePath: 'main.ts',
content: 'import { getRepositoryContents } from "./getRepositoryContents";\n' +
'\n' +
'async function main() {\n' +
' const repoPath = "./";\n' +
' const repositoryContents = await getRepositoryContents(repoPath);\n' +
'\n' +
' // 取得したリポジトリの内容をコンソールに出力\n' +
' console.log(repositoryContents);\n' +
'\n' +
' // あるいは、AIへのコンテキストとして使用\n' +
' // ai.process(repositoryContents);\n' +
'}\n' +
'\n' +
'main();\n'
},
{
filePath: 'package.json',
content: '{\n' +
' "name": "gpt-code-checker",\n' +
' "version": "1.0.0",\n' +
' "main": "index.js",\n' +
' "license": "MIT",\n' +
' "devDependencies": {\n' +
' "@types/minimatch": "^5.1.2",\n' +
' "@types/node": "^22.9.0",\n' +
' "minimatch": "^10.0.1",\n' +
' "typescript": "^5.6.3"\n' +
' },\n' +
' "dependencies": {\n' +
' "ts-node": "^10.9.2"\n' +
' }\n' +
'}\n'
},
{
filePath: 'tsconfig.json',
content: '{\n' +
' "compilerOptions": {\n' +
' "target": "ES2020",\n' +
' "module": "CommonJS",\n' +
' "outDir": "dist",\n' +
' "strict": true,\n' +
' "esModuleInterop": true,\n' +
' "moduleResolution": "node",\n' +
' "forceConsistentCasingInFileNames": true,\n' +
' "types": ["node"]\n' +
' },\n' +
' "include": ["**/*.ts"],\n' +
' "exclude": ["node_modules"]\n' +
'}\n'
}
]
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "gpt-code-checker",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"gpt-tokenizer": "^2.6.0",
"minimatch": "^10.0.1",
"openai": "^4.0.0",
"ts-node": "^10.9.2"
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/minimatch": "^5.1.2",
"@types/node": "^22.9.0",
"typescript": "^5.6.3"
}
}
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"types": ["node"],
"skipLibCheck": true
},
"include": ["**/*.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit ffdef69

Please sign in to comment.