{"version":3,"file":"policy.mjs","sources":["../../src/plops/policy.ts"],"sourcesContent":["import type { ActionType, NodePlopAPI } from 'plop';\nimport tsUtils from '@strapi/typescript-utils';\nimport { join } from 'path';\nimport fs from 'fs';\n\nimport getDestinationPrompts from './prompts/get-destination-prompts';\nimport validateInput from './utils/validate-input';\nimport getFilePath from './utils/get-file-path';\nimport { appendToFile } from './utils/extend-plugin-index-files';\n\nexport default (plop: NodePlopAPI) => {\n  // Policy generator\n  plop.setGenerator('policy', {\n    description: 'Generate a policy for an API',\n    prompts: [\n      {\n        type: 'input',\n        name: 'id',\n        message: 'Policy name',\n        validate: (input) => validateInput(input),\n      },\n      ...getDestinationPrompts('policy', plop.getDestBasePath(), { rootFolder: true }),\n    ],\n    actions(answers) {\n      if (!answers) {\n        return [];\n      }\n\n      const currentDir = process.cwd();\n      const filePath = getFilePath(answers.destination);\n      let language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';\n\n      if (answers.plugin) {\n        // The tsconfig in plugins is located just outside the server src, not in the root of the plugin.\n        const pluginServerDir = join(\n          currentDir,\n          'src',\n          filePath.replace('{{ plugin }}', answers.plugin),\n          '../'\n        );\n        language = tsUtils.isUsingTypeScriptSync(pluginServerDir) ? 'ts' : 'js';\n      }\n\n      const baseActions: Array<ActionType> = [\n        {\n          type: 'add',\n          path: `${filePath}/policies/{{ id }}.${language}`,\n          templateFile: `templates/${language}/policy.${language}.hbs`,\n        },\n      ];\n\n      if (answers.plugin) {\n        const indexPath = join(plop.getDestBasePath(), `${filePath}/policies/index.${language}`);\n        const exists = fs.existsSync(indexPath);\n\n        if (!exists) {\n          // Create index file if it doesn't exist\n          baseActions.push({\n            type: 'add',\n            path: `${filePath}/policies/index.${language}`,\n            templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,\n            skipIfExists: true,\n          });\n        }\n\n        // Append the new policy to the index.ts file\n        baseActions.push({\n          type: 'modify',\n          path: `${filePath}/policies/index.${language}`,\n          transform(template: string) {\n            return appendToFile(template, { type: 'index', singularName: answers.id });\n          },\n        });\n      }\n\n      return baseActions;\n    },\n  });\n};\n"],"names":["plop","setGenerator","description","prompts","type","name","message","validate","input","validateInput","getDestinationPrompts","getDestBasePath","rootFolder","actions","answers","currentDir","process","cwd","filePath","getFilePath","destination","language","tsUtils","isUsingTypeScriptSync","plugin","pluginServerDir","join","replace","baseActions","path","templateFile","indexPath","exists","fs","existsSync","push","skipIfExists","transform","template","appendToFile","singularName","id"],"mappings":";;;;;;;;AAUA,qBAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAAA,CAAKC,YAAY,CAAC,QAAA,EAAU;QAC1BC,WAAAA,EAAa,8BAAA;QACbC,OAAAA,EAAS;AACP,YAAA;gBACEC,IAAAA,EAAM,OAAA;gBACNC,IAAAA,EAAM,IAAA;gBACNC,OAAAA,EAAS,aAAA;gBACTC,QAAAA,EAAU,CAACC,QAAUC,aAAAA,CAAcD,KAAAA;AACrC,aAAA;eACGE,qBAAAA,CAAsB,QAAA,EAAUV,IAAAA,CAAKW,eAAe,EAAA,EAAI;gBAAEC,UAAAA,EAAY;AAAK,aAAA;AAC/E,SAAA;AACDC,QAAAA,OAAAA,CAAAA,CAAQC,OAAO,EAAA;AACb,YAAA,IAAI,CAACA,OAAAA,EAAS;AACZ,gBAAA,OAAO,EAAE;AACX,YAAA;YAEA,MAAMC,UAAAA,GAAaC,QAAQC,GAAG,EAAA;YAC9B,MAAMC,QAAAA,GAAWC,WAAAA,CAAYL,OAAAA,CAAQM,WAAW,CAAA;AAChD,YAAA,IAAIC,QAAAA,GAAWC,OAAAA,CAAQC,qBAAqB,CAACR,cAAc,IAAA,GAAO,IAAA;YAElE,IAAID,OAAAA,CAAQU,MAAM,EAAE;;gBAElB,MAAMC,eAAAA,GAAkBC,IAAAA,CACtBX,UAAAA,EACA,KAAA,EACAG,QAAAA,CAASS,OAAO,CAAC,cAAA,EAAgBb,OAAAA,CAAQU,MAAM,CAAA,EAC/C,KAAA,CAAA;A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