diff options
| author | 2023-04-28 01:36:44 +0800 | |
|---|---|---|
| committer | 2023-04-28 01:36:44 +0800 | |
| commit | dd84b9d64fb98746a230cd24233ff50a562c39c9 (patch) | |
| tree | b583261ef00b3afe72ec4d6dacb31e57779a6faf /packages/eslint-plugin-turbo/lib/utils | |
| parent | 0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff) | |
| download | HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip | |
Diffstat (limited to 'packages/eslint-plugin-turbo/lib/utils')
| -rw-r--r-- | packages/eslint-plugin-turbo/lib/utils/getEnvVarDependencies.ts | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/packages/eslint-plugin-turbo/lib/utils/getEnvVarDependencies.ts b/packages/eslint-plugin-turbo/lib/utils/getEnvVarDependencies.ts new file mode 100644 index 0000000..a57e5eb --- /dev/null +++ b/packages/eslint-plugin-turbo/lib/utils/getEnvVarDependencies.ts @@ -0,0 +1,75 @@ +import { getTurboConfigs } from "@turbo/utils"; + +function findDependsOnEnvVars({ + dependencies, +}: { + dependencies?: Array<string>; +}) { + if (dependencies) { + return ( + dependencies + // filter for dep env vars + .filter((dep) => dep.startsWith("$")) + // remove leading $ + .map((envVar) => envVar.slice(1, envVar.length)) + ); + } + + return []; +} + +function getEnvVarDependencies({ + cwd, +}: { + cwd: string | undefined; +}): Record<string, Set<string>> | null { + const turboConfigs = getTurboConfigs(cwd); + + if (!turboConfigs.length) { + return null; + } + + const envVars: Record<string, Set<string>> = { + "//": new Set(), + }; + + turboConfigs.forEach((turboConfig) => { + const { config, workspacePath, isRootConfig } = turboConfig; + + const key = isRootConfig ? "//" : workspacePath; + if (!envVars[key]) { + envVars[key] = new Set(); + } + + // handle globals + if (!("extends" in config)) { + const { globalDependencies = [], globalEnv = [] } = config; + + const keys = [ + ...findDependsOnEnvVars({ + dependencies: globalDependencies, + }), + ...globalEnv, + ]; + keys.forEach((k) => envVars[key].add(k)); + } + + // handle pipelines + const { pipeline = {} } = config; + Object.values(pipeline).forEach(({ env, dependsOn }) => { + if (dependsOn) { + findDependsOnEnvVars({ dependencies: dependsOn }).forEach((k) => + envVars[key].add(k) + ); + } + + if (env) { + env.forEach((k) => envVars[key].add(k)); + } + }); + }); + + return envVars; +} + +export default getEnvVarDependencies; |
