-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(plugin): add dependencies list to context
As explained on the issue #124 This commit adds the projects dependencies list on the plugin context Before: ```typescript import { hasRubyDependency } from "./dependencies.ts"; const isDependency = await hasRubyDependency(context, "rubocop"); ``` At every import and function call the application would go on the dependencies files(package.json, Pipfile, etc ...) and search for the requested dependencies. This made the application very slow in addition to harming the organization of the code with many unnecessary imports. After: ```typescript const isDependency = await context.dependencies.includes("rubocop"); ``` Now the dependency list is loaded before the introspection and only need to read the necessary dependencies files once. Resolves: #124
- Loading branch information
1 parent
279d9be
commit bbe6513
Showing
25 changed files
with
120 additions
and
751 deletions.
There are no files selected for viewing
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,43 @@ | ||
import { Context } from "../../../../deps.ts"; | ||
|
||
const denoDepRegex = /\/\/.*\/(?<DependencyName>.+?)(?=\/|@)/gm; | ||
|
||
function notEmpty<TValue>(value: TValue | null | undefined): value is TValue { | ||
return value !== null && value !== undefined; | ||
} | ||
|
||
export const jsNodeDependency = async (context: Context) => { | ||
const dependencies: string[] = []; | ||
|
||
for await (const file of context.files.each("**/package.json")) { | ||
const packageJson = await context.files.readJSON(file.path); | ||
|
||
const nodeDeps = packageJson?.dependencies; | ||
if (nodeDeps) { | ||
dependencies.push(...Object.keys(nodeDeps)); | ||
} | ||
|
||
const nodeDepsDev = packageJson?.devDependencies; | ||
if (nodeDepsDev) { | ||
dependencies.push(...Object.keys(nodeDepsDev)); | ||
} | ||
} | ||
|
||
return dependencies; | ||
}; | ||
|
||
export const jsDenoDependency = async (context: Context) => { | ||
for await (const file of await context.files.each("**/deps.ts")) { | ||
const denoDepsText = await context.files.readText(file.path); | ||
|
||
const depsDeno: string[] = Array.from( | ||
denoDepsText.matchAll(denoDepRegex), | ||
(match) => !match.groups ? null : match.groups.DependencyName, | ||
).filter(notEmpty); | ||
|
||
if (depsDeno) { | ||
return depsDeno; | ||
} | ||
} | ||
return []; | ||
}; |
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,3 @@ | ||
export * from "./javascript.ts"; | ||
export * from "./python.ts"; | ||
export * from "./ruby.ts"; |
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,27 @@ | ||
import { Context, introspectors } from "../../../deps.ts"; | ||
import { | ||
jsDenoDependency, | ||
jsNodeDependency, | ||
pythonDependency, | ||
rubyDependency, | ||
} from "./dependencies/mod.ts"; | ||
|
||
type StacksList = ((typeof introspectors)[number]["name"])[]; | ||
|
||
export async function listDependencies( | ||
stackList: StacksList, | ||
context: Context, | ||
): Promise<string[]> { | ||
for (const stack of stackList) { | ||
switch (stack) { | ||
case "javascript": | ||
return await jsNodeDependency(context) || | ||
await jsDenoDependency(context); | ||
case "python": | ||
return await pythonDependency(context); | ||
case "ruby": | ||
return await rubyDependency(context); | ||
} | ||
} | ||
return []; | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.