-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parse and auto detect TF module dependencies
- Loading branch information
1 parent
96e391d
commit 6cd3739
Showing
5 changed files
with
91 additions
and
1 deletion.
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
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,66 @@ | ||
import * as hclParser from '@evops/hcl-terraform-parser' | ||
import { | ||
CreateDependencies, | ||
RawProjectGraphDependency, | ||
workspaceRoot | ||
} from '@nx/devkit' | ||
import * as fs from 'node:fs/promises' | ||
import * as path from 'node:path' | ||
import { DependencyType } from 'nx/src/config/project-graph' | ||
|
||
const isLocalPath = (path: string) => { | ||
return path.startsWith('./') || path.startsWith('../') | ||
} | ||
|
||
export const createDependencies: CreateDependencies = async (_, ctx) => { | ||
const results: RawProjectGraphDependency[] = [] | ||
|
||
const projectRootsToProject: [projectRoot: string, name: string][] = | ||
Object.entries(ctx.projects).map(([name, project]) => [project.root, name]) | ||
|
||
for (const project of Object.keys(ctx.projects)) { | ||
// find tf files to process in the project | ||
const tfFilesToProcess = | ||
ctx.filesToProcess.projectFileMap[project]?.filter((file) => | ||
file.file.endsWith('.tf') | ||
) ?? [] | ||
|
||
for (const file of tfFilesToProcess) { | ||
const data = await fs.readFile(file.file) | ||
const hclFile = hclParser.parse(data) | ||
const moduleCalls = hclFile['module_calls'] | ||
|
||
for (const moduleCall of Object.values(moduleCalls)) { | ||
const depSourcePathRel = moduleCall.source | ||
|
||
if (!isLocalPath(depSourcePathRel)) { | ||
continue | ||
} | ||
|
||
const depSourceAbs = path.resolve(file.file, depSourcePathRel) | ||
|
||
const depSourceRelativeToWorkspace = path.relative( | ||
workspaceRoot, | ||
depSourceAbs | ||
) | ||
|
||
const targetProject = projectRootsToProject.find(([root]) => | ||
depSourceRelativeToWorkspace.startsWith(root) | ||
)?.[1] | ||
|
||
if (!targetProject || targetProject === project) { | ||
continue | ||
} | ||
|
||
results.push({ | ||
type: DependencyType.static, | ||
source: project, | ||
target: targetProject, | ||
sourceFile: file.file | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return results | ||
} |
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,11 @@ | ||
declare module '@evops/hcl-terraform-parser' { | ||
export type HclDef = { | ||
module_calls: { | ||
[key: string]: { | ||
source: string | ||
} | ||
} | ||
} | ||
|
||
export function parse(data: Buffer): HclDef | ||
} |
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 @@ | ||
export * from './graph' |
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