{"version":3,"file":"impl-C3f_H_77.mjs","names":[],"sources":["../src/commands/policy/lint/impl.ts"],"sourcesContent":["import path from 'node:path';\n\nimport colors from 'colors';\n\nimport type { LocalContext } from '../../../context.js';\nimport { doneInputValidation } from '../../../lib/cli/done-input-validation.js';\nimport { inquirerConfirmBoolean } from '../../../lib/helpers/inquirer.js';\nimport { logger } from '../../../logger.js';\nimport { assertOpaInstalled, runOpa, runOPACapture } from '../helpers/index.js';\n\n/** CLI flags for `transcend policy lint`. */\nexport interface LintCommandFlags {\n  /** Directory containing Rego policy files */\n  dir: string;\n}\n\n/**\n * Local Rego lint via `opa check` and optional interactive formatting via `opa fmt`.\n *\n * @param this - CLI context\n * @param flags - Command flags\n */\nexport async function lint(this: LocalContext, { dir }: LintCommandFlags): Promise<void> {\n  doneInputValidation(this.process.exit);\n\n  assertOpaInstalled();\n  const resolvedDir = path.resolve(dir);\n\n  logger.info(colors.green(`Linting policy bundle in ${resolvedDir}...`));\n\n  const checkCode = await runOpa(['check', '--strict', resolvedDir]);\n  if (checkCode !== 0) {\n    this.process.exit(checkCode);\n  }\n\n  const { stdout } = await runOPACapture(['fmt', '--list', resolvedDir]);\n  const unformattedFiles = stdout\n    .trim()\n    .split('\\n')\n    .filter((file) => file.length > 0);\n\n  if (unformattedFiles.length > 0) {\n    logger.error(colors.red('Policy files are not formatted:'));\n    unformattedFiles.forEach((file) => {\n      logger.error(colors.red(`  - ${file}`));\n    });\n    logger.error('');\n    await runOpa(['fmt', '--diff', resolvedDir]);\n\n    if (!this.process.stdin.isTTY) {\n      logger.error(colors.red('Cannot format policy files in a non-interactive environment.'));\n      this.process.exit(1);\n    }\n\n    const shouldFormat = await inquirerConfirmBoolean({\n      message: 'Format the unformatted policy files listed above?',\n    });\n    if (!shouldFormat) {\n      this.process.exit(1);\n    }\n\n    const fmtWriteCode = await runOpa(['fmt', '-w', resolvedDir]);\n    if (fmtWriteCode !== 0) {\n      this.process.exit(fmtWriteCode);\n    }\n\n    logger.info(colors.green('Policy files formatted.'));\n    logger.info(colors.green('Policy lint passed.'));\n    return;\n  }\n\n  logger.info(colors.green('Policy lint passed.'));\n}\n"],"mappings":"sPAsBA,eAAsB,EAAyB,CAAE,OAAwC,CACvF,EAAoB,KAAK,QAAQ,KAAK,CAEtC,GAAoB,CACpB,IAAM,EAAc,EAAK,QAAQ,EAAI,CAErC,EAAO,KAAK,EAAO,MAAM,4BAA4B,EAAY,KAAK,CAAC,CAEvE,IAAM,EAAY,MAAM,EAAO,CAAC,QAAS,WAAY,EAAY,CAAC,CAC9D,IAAc,GAChB,KAAK,QAAQ,KAAK,EAAU,CAG9B,GAAM,CAAE,UAAW,MAAM,EAAc,CAAC,MAAO,SAAU,EAAY,CAAC,CAChE,EAAmB,EACtB,MAAM,CACN,MAAM;EAAK,CACX,OAAQ,GAAS,EAAK,OAAS,EAAE,CAEpC,GAAI,EAAiB,OAAS,EAAG,CAC/B,EAAO,MAAM,EAAO,IAAI,kCAAkC,CAAC,CAC3D,EAAiB,QAAS,GAAS,CACjC,EAAO,MAAM,EAAO,IAAI,OAAO,IAAO,CAAC,EACvC,CACF,EAAO,MAAM,GAAG,CAChB,MAAM,EAAO,CAAC,MAAO,SAAU,EAAY,CAAC,CAEvC,KAAK,QAAQ,MAAM,QACtB,EAAO,MAAM,EAAO,IAAI,+DAA+D,CAAC,CACxF,KAAK,QAAQ,KAAK,EAAE,EAMjB,MAHsB,EAAuB,CAChD,QAAS,oDACV,CAAC,EAEA,KAAK,QAAQ,KAAK,EAAE,CAGtB,IAAM,EAAe,MAAM,EAAO,CAAC,MAAO,KAAM,EAAY,CAAC,CACzD,IAAiB,GACnB,KAAK,QAAQ,KAAK,EAAa,CAGjC,EAAO,KAAK,EAAO,MAAM,0BAA0B,CAAC,CACpD,EAAO,KAAK,EAAO,MAAM,sBAAsB,CAAC,CAChD,OAGF,EAAO,KAAK,EAAO,MAAM,sBAAsB,CAAC"}