-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve dependency tree and file edits
- Loading branch information
1 parent
4bbfc78
commit e2bbc8b
Showing
18 changed files
with
420 additions
and
1,085 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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,44 @@ | ||
import { NanoAPIAnnotation } from "./types"; | ||
|
||
export function getNanoApiAnnotationFromCommentValue(comment: string) { | ||
const nanoapiRegex = /@nanoapi|((method|path|group):([^ ]+))/g; | ||
const matches = comment.match(nanoapiRegex); | ||
// remove first match, which is the @nanoapi identifier | ||
matches?.shift(); | ||
|
||
if (matches && matches.length > 0) { | ||
return matches.reduce((acc, match) => { | ||
// key, first element when split with ":" | ||
const key = match.split(":")[0]; | ||
// value, everything else | ||
const value = match.split(":").slice(1).join(":"); | ||
return { ...acc, [key]: value }; | ||
}, {} as NanoAPIAnnotation); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export function replaceCommentFromAnnotation( | ||
comment: string, | ||
annotation: NanoAPIAnnotation, | ||
) { | ||
const commentRegex = /@nanoapi\s*(.*)/g; | ||
|
||
// Construct the new annotation string | ||
let newAnnotation = "@nanoapi"; | ||
if (annotation.method) { | ||
newAnnotation += ` method:${annotation.method}`; | ||
} | ||
if (annotation.path) { | ||
newAnnotation += ` path:${annotation.path}`; | ||
} | ||
if (annotation.group) { | ||
newAnnotation += ` group:${annotation.group}`; | ||
} | ||
|
||
// Replace the old annotation with the new annotation | ||
const updatedComment = comment.replace(commentRegex, newAnnotation); | ||
|
||
return updatedComment; | ||
} |
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,75 @@ | ||
import fs from "fs"; | ||
import Parser from "tree-sitter"; | ||
import { cleanupJavascriptFile } from "./languages/javascript/cleanup"; | ||
import { extractJavascriptFileImports } from "./languages/javascript/imports"; | ||
|
||
import { resolveFilePath } from "./file"; | ||
import { Group } from "./types"; | ||
import { getParserLanguageFromFile } from "./treeSitter"; | ||
|
||
export function cleanupFile(filePath: string, group: Group) { | ||
const language = getParserLanguageFromFile(filePath); | ||
const parser = new Parser(); | ||
parser.setLanguage(language); | ||
|
||
const sourceCode = fs.readFileSync(filePath, "utf8"); | ||
|
||
let dependencies: string[] = []; | ||
if (["javascript", "typescript"].includes(language.name)) { | ||
dependencies = extractJavascriptFileImports(parser, sourceCode); | ||
} else { | ||
throw new Error(`Unsupported language: ${language.language}`); | ||
} | ||
|
||
// Check if we can resolve the path for each dependency. If we cannot, we need to remove it | ||
const invalidDependencies = dependencies.filter( | ||
(dep) => !resolveFilePath(dep, filePath), | ||
); | ||
|
||
let updatedSourceCode: string; | ||
|
||
if (["javascript", "typescript"].includes(language.name)) { | ||
updatedSourceCode = cleanupJavascriptFile( | ||
parser, | ||
sourceCode, | ||
group, | ||
invalidDependencies, | ||
); | ||
} else { | ||
throw new Error(`Unsupported language: ${language.language}`); | ||
} | ||
|
||
fs.writeFileSync(filePath, updatedSourceCode, "utf8"); | ||
} | ||
|
||
export function removeIndexesFromSourceCode( | ||
sourceCode: string, | ||
indexesToRemove: { startIndex: number; endIndex: number }[], | ||
) { | ||
let newSourceCode = sourceCode; | ||
|
||
// sort to start removing from the of the file end | ||
indexesToRemove.sort((a, b) => b.startIndex - a.startIndex); | ||
|
||
indexesToRemove.forEach(({ startIndex, endIndex }) => { | ||
newSourceCode = | ||
newSourceCode.slice(0, startIndex) + newSourceCode.slice(endIndex); | ||
}); | ||
|
||
return newSourceCode; | ||
} | ||
|
||
export function replaceIndexesFromSourceCode( | ||
sourceCode: string, | ||
indexesToReplace: { startIndex: number; endIndex: number; text: string }[], | ||
) { | ||
// sort to start removing from the end of the file | ||
indexesToReplace.sort((a, b) => b.startIndex - a.startIndex); | ||
|
||
indexesToReplace.forEach(({ startIndex, endIndex, text }) => { | ||
sourceCode = | ||
sourceCode.slice(0, startIndex) + text + sourceCode.slice(endIndex); | ||
}); | ||
|
||
return sourceCode; | ||
} |
Oops, something went wrong.