{"version":3,"file":"fs.mjs","sources":["../../src/services/fs.ts"],"sourcesContent":["import path from 'path';\nimport fse from 'fs-extra';\n\nimport type { Strapi } from '@strapi/types';\n\ninterface StrapiFS {\n  writeAppFile(optPath: string | string[], data: string): Promise<void>;\n  writePluginFile(plugin: string, optPath: string | string[], data: string): Promise<void>;\n  removeAppFile(optPath: string | string[]): Promise<void>;\n  appendFile(optPath: string | string[], data: string): void;\n}\n\n/**\n * create strapi fs layer\n */\nexport default (strapi: Strapi) => {\n  function normalizePath(optPath: string | string[]) {\n    const filePath = Array.isArray(optPath) ? optPath.join('/') : optPath;\n\n    const normalizedPath = path.normalize(filePath).replace(/^\\/?(\\.\\/|\\.\\.\\/)+/, '');\n\n    return path.resolve(strapi.dirs.app.root, normalizedPath);\n  }\n\n  const strapiFS: StrapiFS = {\n    /**\n     * Writes a file in a strapi app\n     * @param {Array|string} optPath - file path\n     * @param {string} data - content\n     */\n    writeAppFile(optPath, data) {\n      const writePath = normalizePath(optPath);\n      return fse.ensureFile(writePath).then(() => fse.writeFile(writePath, data));\n    },\n\n    /**\n     * Writes a file in a plugin extensions folder\n     * @param {string} plugin - plugin name\n     * @param {Array|string} optPath - path to file\n     * @param {string} data - content\n     */\n    writePluginFile(plugin, optPath, data) {\n      const newPath = ['extensions', plugin].concat(optPath).join('/');\n      return strapiFS.writeAppFile(newPath, data);\n    },\n\n    /**\n     * Removes a file in strapi app\n     */\n    removeAppFile(optPath) {\n      const removePath = normalizePath(optPath);\n      return fse.remove(removePath);\n    },\n\n    /**\n     * Appends a file in strapi app\n     */\n    appendFile(optPath, data) {\n      const writePath = normalizePath(optPath);\n      return fse.appendFileSync(writePath, data);\n    },\n  };\n\n  return strapiFS;\n};\n"],"names":[],"mappings":";;AAeA,MAAe,iBAAA,CAAC,WAAmB;AACjC,WAAS,cAAc,SAA4B;AAC3C,UAAA,WAAW,MAAM,QAAQ,OAAO,IAAI,QAAQ,KAAK,GAAG,IAAI;AAE9D,UAAM,iBAAiB,KAAK,UAAU,QAAQ,EAAE,QAAQ,sBAAsB,EAAE;AAEhF,WAAO,KAAK,QAAQ,OAAO,KAAK,IAAI,MAAM,cAAc;AAAA,EAC1D;AAEA,QAAM,WAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMzB,aAAa,SAAS,MAAM;AACpB,YAAA,YAAY,cAAc,OAAO;AAChC,aAAA,IAAI,WAAW,SAAS,EAAE,KAAK,MAAM,IAAI,UAAU,WAAW,IAAI,CAAC;AAAA,IAC5E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQA,gBAAgB,QAAQ,SAAS,MAAM;AAC/B,YAAA,UAAU,CAAC,cAAc,MAAM,EAAE,OAAO,OAAO,EAAE,KAAK,GAAG;AACxD,aAAA,SAAS,aAAa,SAAS,IAAI;AAAA,IAC5C;AAAA;AAAA;AAAA;AAAA,IAKA,cAAc,SAAS;AACf,YAAA,aAAa,cAAc,OAAO;AACjC,aAAA,IAAI,OAAO,UAAU;AAAA,IAC9B;AAAA;AAAA;AAAA;AAAA,IAKA,WAAW,SAAS,MAAM;AAClB,YAAA,YAAY,cAAc,OAAO;AAChC,aAAA,IAAI,eAAe,WAAW,IAAI;AAAA,IAC3C;AAAA,EAAA;AAGK,SAAA;AACT;"}