diff --git a/packages/terraform/package.json b/packages/terraform/package.json index 3b2e796f..ec6f7616 100644 --- a/packages/terraform/package.json +++ b/packages/terraform/package.json @@ -18,6 +18,6 @@ "builders": "./executors.json", "generators": "./generators.json", "dependencies": { - "@evops/hcl-terraform-parser": "^1.0.0" + "hcl2-json-parser": "^1.0.1" } } diff --git a/packages/terraform/src/graph.ts b/packages/terraform/src/graph.ts index 77a43120..3fad2b96 100644 --- a/packages/terraform/src/graph.ts +++ b/packages/terraform/src/graph.ts @@ -1,9 +1,10 @@ -import * as hclParser from '@evops/hcl-terraform-parser' import { CreateDependencies, + logger, RawProjectGraphDependency, workspaceRoot } from '@nx/devkit' +import * as hcl2JsonParser from 'hcl2-json-parser' import * as fs from 'node:fs/promises' import * as path from 'node:path' import { DependencyType } from 'nx/src/config/project-graph' @@ -27,11 +28,20 @@ export const createDependencies: CreateDependencies = async (_, ctx) => { 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 + let parsed: hcl2JsonParser.HclDef + + try { + parsed = await hcl2JsonParser.parseToObject(data.toString()) + } catch (e) { + logger.warn( + `Failed to parse .tf file ${file.file}. Error: ${e.message}` + ) + continue + } + + for (const moduleCall of Object.values(parsed.module ?? [])) { + const depSourcePathRel = moduleCall[0]?.source if (!isLocalPath(depSourcePathRel)) { continue diff --git a/packages/terraform/src/hcl-terraform-parser.d.ts b/packages/terraform/src/hcl-terraform-parser.d.ts deleted file mode 100644 index cfe978a4..00000000 --- a/packages/terraform/src/hcl-terraform-parser.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -declare module '@evops/hcl-terraform-parser' { - export type HclDef = { - module_calls: { - [key: string]: { - source: string - } - } - } - - export function parse(data: Buffer): HclDef -} diff --git a/packages/terraform/src/hcl2-json-parser.d.ts b/packages/terraform/src/hcl2-json-parser.d.ts new file mode 100644 index 00000000..118dcef8 --- /dev/null +++ b/packages/terraform/src/hcl2-json-parser.d.ts @@ -0,0 +1,27 @@ +declare module 'hcl2-json-parser' { + export type HclDef = { + data?: { + [data: string]: { + [dataName: string]: [{ [propertyName: string]: any }] + } + } + + resource?: { + [resource: string]: { + [resourceName: string]: [{ [propertyName: string]: any }] + } + } + + terraform?: [ + { + backend?: [{ [propertyName: string]: any }] + } + ] + + module?: { + [moduleName: string]: [{ source: string }] + } + } + + export function parseToObject(data: string): Promise +} diff --git a/yarn.lock b/yarn.lock index d858666c..73007ec5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5692,13 +5692,6 @@ __metadata: languageName: node linkType: hard -"@evops/hcl-terraform-parser@npm:^1.0.0": - version: 1.0.0 - resolution: "@evops/hcl-terraform-parser@npm:1.0.0" - checksum: 10/d48b92b594a04a10a513e9e2984ac48437348ff23ff0e898e6fdb612715bb0a20c66d176b57f2b828c7c8da1cd282763aed152bcf17c00f67ae2c061c78212fd - languageName: node - linkType: hard - "@fastify/busboy@npm:^2.0.0": version: 2.1.1 resolution: "@fastify/busboy@npm:2.1.1" @@ -7349,14 +7342,6 @@ __metadata: languageName: unknown linkType: soft -"@nx-extend/terraform@workspace:packages/terraform": - version: 0.0.0-use.local - resolution: "@nx-extend/terraform@workspace:packages/terraform" - dependencies: - "@evops/hcl-terraform-parser": "npm:^1.0.0" - languageName: unknown - linkType: soft - "@nx-extend/translations@workspace:packages/translations": version: 0.0.0-use.local resolution: "@nx-extend/translations@workspace:packages/translations" @@ -8015,6 +8000,14 @@ __metadata: languageName: node linkType: hard +"@pulasthibandara/nx-extend-terraform@workspace:packages/terraform": + version: 0.0.0-use.local + resolution: "@pulasthibandara/nx-extend-terraform@workspace:packages/terraform" + dependencies: + hcl2-json-parser: "npm:^1.0.1" + languageName: unknown + linkType: soft + "@radix-ui/colors@npm:1.0.1": version: 1.0.1 resolution: "@radix-ui/colors@npm:1.0.1" @@ -13199,6 +13192,13 @@ __metadata: languageName: node linkType: hard +"assertion-error@npm:^1.1.0": + version: 1.1.0 + resolution: "assertion-error@npm:1.1.0" + checksum: 10/fd9429d3a3d4fd61782eb3962ae76b6d08aa7383123fca0596020013b3ebd6647891a85b05ce821c47d1471ed1271f00b0545cf6a4326cf2fc91efcc3b0fbecf + languageName: node + linkType: hard + "assign-symbols@npm:^1.0.0": version: 1.0.0 resolution: "assign-symbols@npm:1.0.0" @@ -13940,6 +13940,13 @@ __metadata: languageName: node linkType: hard +"browser-stdout@npm:^1.3.1": + version: 1.3.1 + resolution: "browser-stdout@npm:1.3.1" + checksum: 10/ac70a84e346bb7afc5045ec6f22f6a681b15a4057447d4cc1c48a25c6dedb302a49a46dd4ddfb5cdd9c96e0c905a8539be1b98ae7bc440512152967009ec7015 + languageName: node + linkType: hard + "browserslist-to-esbuild@npm:1.2.0": version: 1.2.0 resolution: "browserslist-to-esbuild@npm:1.2.0" @@ -14280,7 +14287,7 @@ __metadata: languageName: node linkType: hard -"camelcase@npm:^6.2.0": +"camelcase@npm:^6.0.0, camelcase@npm:^6.2.0": version: 6.3.0 resolution: "camelcase@npm:6.3.0" checksum: 10/8c96818a9076434998511251dcb2761a94817ea17dbdc37f47ac080bd088fc62c7369429a19e2178b993497132c8cbcf5cc1f44ba963e76782ba469c0474938d @@ -14348,6 +14355,32 @@ __metadata: languageName: node linkType: hard +"chai-as-promised@npm:^7.1.1": + version: 7.1.2 + resolution: "chai-as-promised@npm:7.1.2" + dependencies: + check-error: "npm:^1.0.2" + peerDependencies: + chai: ">= 2.1.2 < 6" + checksum: 10/be372540dad92ef85cde3954bc0e9b0b33e4e6454f3740b17bfb16e36eda638911619089c05a4e4f2bf6722563bf893bb78c2af59b318c23abb2199e5c20ca1f + languageName: node + linkType: hard + +"chai@npm:^4.3.10": + version: 4.5.0 + resolution: "chai@npm:4.5.0" + dependencies: + assertion-error: "npm:^1.1.0" + check-error: "npm:^1.0.3" + deep-eql: "npm:^4.1.3" + get-func-name: "npm:^2.0.2" + loupe: "npm:^2.3.6" + pathval: "npm:^1.1.1" + type-detect: "npm:^4.1.0" + checksum: 10/cde341aee15b0a51559c7cfc20788dcfb4d586a498cfb93b937bb568fd45c777b73b1461274be6092b6bf868adb4e3a63f3fec13c89f7d8fb194f84c6fa42d5f + languageName: node + linkType: hard + "chalk@npm:3.0.0": version: 3.0.0 resolution: "chalk@npm:3.0.0" @@ -14467,6 +14500,15 @@ __metadata: languageName: node linkType: hard +"check-error@npm:^1.0.2, check-error@npm:^1.0.3": + version: 1.0.3 + resolution: "check-error@npm:1.0.3" + dependencies: + get-func-name: "npm:^2.0.2" + checksum: 10/e2131025cf059b21080f4813e55b3c480419256914601750b0fee3bd9b2b8315b531e551ef12560419b8b6d92a3636511322752b1ce905703239e7cc451b6399 + languageName: node + linkType: hard + "cheerio-select@npm:^2.1.0": version: 2.1.0 resolution: "cheerio-select@npm:2.1.0" @@ -16275,6 +16317,13 @@ __metadata: languageName: node linkType: hard +"decamelize@npm:^4.0.0": + version: 4.0.0 + resolution: "decamelize@npm:4.0.0" + checksum: 10/b7d09b82652c39eead4d6678bb578e3bebd848add894b76d0f6b395bc45b2d692fb88d977e7cfb93c4ed6c119b05a1347cef261174916c2e75c0a8ca57da1809 + languageName: node + linkType: hard + "decimal.js@npm:^10.4.2": version: 10.4.3 resolution: "decimal.js@npm:10.4.3" @@ -16335,6 +16384,15 @@ __metadata: languageName: node linkType: hard +"deep-eql@npm:^4.1.3": + version: 4.1.4 + resolution: "deep-eql@npm:4.1.4" + dependencies: + type-detect: "npm:^4.0.0" + checksum: 10/f04f4d581f044a824a6322fe4f68fbee4d6780e93fc710cd9852cbc82bfc7010df00f0e05894b848abbe14dc3a25acac44f424e181ae64d12f2ab9d0a875a5ef + languageName: node + linkType: hard + "deep-equal@npm:~1.0.1": version: 1.0.1 resolution: "deep-equal@npm:1.0.1" @@ -16698,6 +16756,13 @@ __metadata: languageName: node linkType: hard +"diff@npm:^5.2.0": + version: 5.2.0 + resolution: "diff@npm:5.2.0" + checksum: 10/01b7b440f83a997350a988e9d2f558366c0f90f15be19f4aa7f1bb3109a4e153dfc3b9fbf78e14ea725717017407eeaa2271e3896374a0181e8f52445740846d + languageName: node + linkType: hard + "dir-glob@npm:^3.0.1": version: 3.0.1 resolution: "dir-glob@npm:3.0.1" @@ -19085,6 +19150,13 @@ __metadata: languageName: node linkType: hard +"get-func-name@npm:^2.0.1, get-func-name@npm:^2.0.2": + version: 2.0.2 + resolution: "get-func-name@npm:2.0.2" + checksum: 10/3f62f4c23647de9d46e6f76d2b3eafe58933a9b3830c60669e4180d6c601ce1b4aa310ba8366143f55e52b139f992087a9f0647274e8745621fa2af7e0acf13b + languageName: node + linkType: hard + "get-intrinsic@npm:^1.0.2, get-intrinsic@npm:^1.1.1, get-intrinsic@npm:^1.1.3": version: 1.2.1 resolution: "get-intrinsic@npm:1.2.1" @@ -19378,7 +19450,7 @@ __metadata: languageName: node linkType: hard -"glob@npm:^8.0.3": +"glob@npm:^8.0.3, glob@npm:^8.1.0": version: 8.1.0 resolution: "glob@npm:8.1.0" dependencies: @@ -19927,6 +19999,17 @@ __metadata: languageName: node linkType: hard +"hcl2-json-parser@npm:^1.0.1": + version: 1.0.1 + resolution: "hcl2-json-parser@npm:1.0.1" + dependencies: + chai: "npm:^4.3.10" + chai-as-promised: "npm:^7.1.1" + mocha: "npm:^10.2.0" + checksum: 10/77add37ace18312fd2829f6e2b3f78d07604b83e135862a44bbdf4f3e0e932de0adde9abd65b2f5d4dd05c43bddec1f56201223de56490b1228c07ee584ca276 + languageName: node + linkType: hard + "he@npm:^1.2.0": version: 1.2.0 resolution: "he@npm:1.2.0" @@ -21120,6 +21203,13 @@ __metadata: languageName: node linkType: hard +"is-plain-obj@npm:^2.1.0": + version: 2.1.0 + resolution: "is-plain-obj@npm:2.1.0" + checksum: 10/cec9100678b0a9fe0248a81743041ed990c2d4c99f893d935545cfbc42876cbe86d207f3b895700c690ad2fa520e568c44afc1605044b535a7820c1d40e38daa + languageName: node + linkType: hard + "is-plain-obj@npm:^3.0.0": version: 3.0.0 resolution: "is-plain-obj@npm:3.0.0" @@ -23500,6 +23590,15 @@ __metadata: languageName: node linkType: hard +"loupe@npm:^2.3.6": + version: 2.3.7 + resolution: "loupe@npm:2.3.7" + dependencies: + get-func-name: "npm:^2.0.1" + checksum: 10/635c8f0914c2ce7ecfe4e239fbaf0ce1d2c00e4246fafcc4ed000bfdb1b8f89d05db1a220054175cca631ebf3894872a26fffba0124477fcb562f78762848fb1 + languageName: node + linkType: hard + "lower-case-first@npm:^1.0.0": version: 1.0.2 resolution: "lower-case-first@npm:1.0.2" @@ -24882,7 +24981,7 @@ __metadata: languageName: node linkType: hard -"minimatch@npm:^5.0.1": +"minimatch@npm:^5.0.1, minimatch@npm:^5.1.6": version: 5.1.6 resolution: "minimatch@npm:5.1.6" dependencies: @@ -25068,6 +25167,37 @@ __metadata: languageName: node linkType: hard +"mocha@npm:^10.2.0": + version: 10.7.3 + resolution: "mocha@npm:10.7.3" + dependencies: + ansi-colors: "npm:^4.1.3" + browser-stdout: "npm:^1.3.1" + chokidar: "npm:^3.5.3" + debug: "npm:^4.3.5" + diff: "npm:^5.2.0" + escape-string-regexp: "npm:^4.0.0" + find-up: "npm:^5.0.0" + glob: "npm:^8.1.0" + he: "npm:^1.2.0" + js-yaml: "npm:^4.1.0" + log-symbols: "npm:^4.1.0" + minimatch: "npm:^5.1.6" + ms: "npm:^2.1.3" + serialize-javascript: "npm:^6.0.2" + strip-json-comments: "npm:^3.1.1" + supports-color: "npm:^8.1.1" + workerpool: "npm:^6.5.1" + yargs: "npm:^16.2.0" + yargs-parser: "npm:^20.2.9" + yargs-unparser: "npm:^2.0.0" + bin: + _mocha: bin/_mocha + mocha: bin/mocha.js + checksum: 10/5757aeb320df2507338bfba41731070ce16d27177c5876672fff4bcc4f7b7bcf1afe6ec761bfded43a5d28032d7b797b8b905b5b44c9420203f3ee71457732c1 + languageName: node + linkType: hard + "mrmime@npm:^1.0.0": version: 1.0.1 resolution: "mrmime@npm:1.0.1" @@ -25089,7 +25219,7 @@ __metadata: languageName: node linkType: hard -"ms@npm:2.1.3, ms@npm:^2.0.0, ms@npm:^2.1.1": +"ms@npm:2.1.3, ms@npm:^2.0.0, ms@npm:^2.1.1, ms@npm:^2.1.3": version: 2.1.3 resolution: "ms@npm:2.1.3" checksum: 10/aa92de608021b242401676e35cfa5aa42dd70cbdc082b916da7fb925c542173e36bce97ea3e804923fe92c0ad991434e4a38327e15a1b5b5f945d66df615ae6d @@ -26736,6 +26866,13 @@ __metadata: languageName: node linkType: hard +"pathval@npm:^1.1.1": + version: 1.1.1 + resolution: "pathval@npm:1.1.1" + checksum: 10/b50a4751068aa3a5428f5a0b480deecedc6f537666a3630a0c2ae2d5e7c0f4bf0ee77b48404441ec1220bef0c91625e6030b3d3cf5a32ab0d9764018d1d9dbb6 + languageName: node + linkType: hard + "pause@npm:0.0.1": version: 0.0.1 resolution: "pause@npm:0.0.1" @@ -29973,6 +30110,15 @@ __metadata: languageName: node linkType: hard +"serialize-javascript@npm:^6.0.2": + version: 6.0.2 + resolution: "serialize-javascript@npm:6.0.2" + dependencies: + randombytes: "npm:^2.1.0" + checksum: 10/445a420a6fa2eaee4b70cbd884d538e259ab278200a2ededd73253ada17d5d48e91fb1f4cd224a236ab62ea7ba0a70c6af29fc93b4f3d3078bf7da1c031fde58 + languageName: node + linkType: hard + "serve-handler@npm:^6.1.5": version: 6.1.5 resolution: "serve-handler@npm:6.1.5" @@ -32108,6 +32254,13 @@ __metadata: languageName: node linkType: hard +"type-detect@npm:^4.0.0, type-detect@npm:^4.1.0": + version: 4.1.0 + resolution: "type-detect@npm:4.1.0" + checksum: 10/e363bf0352427a79301f26a7795a27718624c49c576965076624eb5495d87515030b207217845f7018093adcbe169b2d119bb9b7f1a31a92bfbb1ab9639ca8dd + languageName: node + linkType: hard + "type-fest@npm:^0.20.2": version: 0.20.2 resolution: "type-fest@npm:0.20.2" @@ -33622,6 +33775,13 @@ __metadata: languageName: node linkType: hard +"workerpool@npm:^6.5.1": + version: 6.5.1 + resolution: "workerpool@npm:6.5.1" + checksum: 10/b1b00139fe62f2ebec556a2af8085bf6e7502ad26cf2a4dcb34fb4408b2e68aa12c88b0a50cb463b24f2806d60fa491fc0da933b56ec3b53646aeec0025d14cb + languageName: node + linkType: hard + "wrap-ansi-cjs@npm:wrap-ansi@^7.0.0, wrap-ansi@npm:^7.0.0": version: 7.0.0 resolution: "wrap-ansi@npm:7.0.0" @@ -33890,14 +34050,26 @@ __metadata: languageName: node linkType: hard -"yargs-parser@npm:^20.2.2": +"yargs-parser@npm:^20.2.2, yargs-parser@npm:^20.2.9": version: 20.2.9 resolution: "yargs-parser@npm:20.2.9" checksum: 10/0188f430a0f496551d09df6719a9132a3469e47fe2747208b1dd0ab2bb0c512a95d0b081628bbca5400fb20dbf2fabe63d22badb346cecadffdd948b049f3fcc languageName: node linkType: hard -"yargs@npm:^16.1.0, yargs@npm:^16.1.1": +"yargs-unparser@npm:^2.0.0": + version: 2.0.0 + resolution: "yargs-unparser@npm:2.0.0" + dependencies: + camelcase: "npm:^6.0.0" + decamelize: "npm:^4.0.0" + flat: "npm:^5.0.2" + is-plain-obj: "npm:^2.1.0" + checksum: 10/68f9a542c6927c3768c2f16c28f71b19008710abd6b8f8efbac6dcce26bbb68ab6503bed1d5994bdbc2df9a5c87c161110c1dfe04c6a3fe5c6ad1b0e15d9a8a3 + languageName: node + linkType: hard + +"yargs@npm:^16.1.0, yargs@npm:^16.1.1, yargs@npm:^16.2.0": version: 16.2.0 resolution: "yargs@npm:16.2.0" dependencies: