{"version":3,"file":"service.mjs","sources":["../../src/plops/service.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 getFilePath from './utils/get-file-path';\nimport { appendToFile } from './utils/extend-plugin-index-files';\n\nexport default (plop: NodePlopAPI) => {\n  // Service generator\n  plop.setGenerator('service', {\n    description: 'Generate a service for an API',\n    prompts: [\n      {\n        type: 'input',\n        name: 'id',\n        message: 'Service name',\n      },\n      ...getDestinationPrompts('service', plop.getDestBasePath()),\n    ],\n    actions(answers) {\n      if (!answers) {\n        return [];\n      }\n\n      const filePath = getFilePath(answers?.destination);\n      const currentDir = process.cwd();\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}/services/{{ id }}.${language}`,\n          templateFile: `templates/${language}/service.${language}.hbs`,\n        },\n      ];\n\n      if (answers.plugin) {\n        const indexPath = join(plop.getDestBasePath(), `${filePath}/services/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}/services/index.${language}`,\n            templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,\n            skipIfExists: true,\n          });\n        }\n\n        // Append the new service to the index.ts file\n        baseActions.push({\n          type: 'modify',\n          path: `${filePath}/services/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","getDestinationPrompts","getDestBasePath","actions","answers","filePath","getFilePath","destination","currentDir","process","cwd","language","tsUtils","isUsingTypeScriptSync","plugin","pluginServerDir","join","replace","baseActions","path","templateFile","indexPath","exists","fs","existsSync","push","skipIfExists","transform","template","appendToFile","singularName","id"],"mappings":";;;;;;;AASA,sBAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAAA,CAAKC,YAAY,CAAC,SAAA,EAAW;QAC3BC,WAAAA,EAAa,+BAAA;QACbC,OAAAA,EAAS;AACP,YAAA;gBACEC,IAAAA,EAAM,OAAA;gBACNC,IAAAA,EAAM,IAAA;gBACNC,OAAAA,EAAS;AACX,aAAA;eACGC,qBAAAA,CAAsB,SAAA,EAAWP,KAAKQ,eAAe,EAAA;AACzD,SAAA;AACDC,QAAAA,OAAAA,CAAAA,CAAQC,OAAO,EAAA;AACb,YAAA,IAAI,CAACA,OAAAA,EAAS;AACZ,gBAAA,OAAO,EAAE;AACX,YAAA;YAEA,MAAMC,QAAAA,GAAWC,YAAYF,OAAAA,EAASG,WAAAA,CAAAA;YACtC,MAAMC,UAAAA,GAAaC,QAAQC,GAAG,EAAA;AAC9B,YAAA,IAAIC,QAAAA,GAAWC,OAAAA,CAAQC,qBAAqB,CAACL,cAAc,IAAA,GAAO,IAAA;YAElE,IAAIJ,OAAAA,CAAQU,MAAM,EAAE;;gBAElB,MAAMC,eAAAA,GAAkBC,IAAAA,CACtBR,UAAAA,EACA,KAAA,EACAH,QAAAA,CAASY,OAAO,CAAC,cAAA,EAAgBb,OAAAA,CAAQU,MAAM,CAAA,EAC/C,KAAA,CAAA;AAEFH,gBAAAA,QAAAA,GAAWC,OAAAA,CAAQC,qBAAqB,CAACE,eAAAA,CAAAA,GAAmB,IAAA,GAAO,IAAA;AACrE,YAAA;AAEA,YAAA,MAAMG,WAAAA,GAAiC;AACrC,gBAAA;oBACEpB,IAAAA,EAAM,KAAA;AACNqB,oBAAAA,IAA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