@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
1 lines • 5.05 kB
Source Map (JSON)
{"version":3,"file":"helpers.mjs","names":["existsSync"],"sources":["../src/helpers.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { parseTar, parseTarGzip } from \"nanotar\";\nimport { createWriteStream, mkdirSync, rmSync } from \"node:fs\";\nimport { mkdir, readFile, rm } from \"node:fs/promises\";\nimport { exists, existsSync } from \"./exists\";\n\n/**\n * Create a directory if it does not exist.\n *\n * @param path - The directory path to check\n * @returns An indicator specifying if the directory exists\n */\nexport function createDirectorySync(path: string) {\n if (existsSync(path)) {\n return;\n }\n\n return mkdirSync(path, { recursive: true });\n}\n\n/**\n * Create a directory if it does not exist.\n *\n * @param path - The directory path to check\n * @returns An indicator specifying if the directory exists\n */\nexport async function createDirectory(path: string) {\n if (await exists(path)) {\n return;\n }\n\n return mkdir(path, { recursive: true });\n}\n\n/**\n * Remove a directory if it exists.\n *\n * @param path - The directory path to check\n * @returns An indicator specifying if the directory exists\n */\nexport function removeDirectorySync(path: string) {\n if (!existsSync(path)) {\n return;\n }\n\n return rmSync(path, { recursive: true });\n}\n\n/**\n * Remove a directory if it exists.\n *\n * @param path - The directory path to check\n * @returns An indicator specifying if the directory exists\n */\nexport async function removeDirectory(path: string) {\n if (!existsSync(path)) {\n return;\n }\n\n return rm(path, { recursive: true });\n}\n\n/**\n * Extracts a file from a given tarball to the specified destination.\n *\n * @param tarballPath - The path to the tarball from where the file should be extracted.\n * @param file - The path to the file inside the tarball.\n * @param destinationFilePath - The destination file path.\n * @returns True if the file was extracted successfully, false otherwise.\n */\nexport async function extractFileFromTar(\n tarballPath: string,\n file: string,\n destinationFilePath: string\n) {\n const result = parseTar(await readFile(tarballPath));\n\n const entry = result.find(e => e.name === file);\n if (!entry?.data) {\n return;\n }\n\n if (!(await exists(destinationFilePath))) {\n await mkdir(destinationFilePath, { recursive: true });\n }\n\n const stream = createWriteStream(destinationFilePath);\n stream.write(entry.data);\n}\n\n/**\n * Extracts a file from a given TarGzip to the specified destination.\n *\n * @param tarballPath - The path to the tarball from where the file should be extracted.\n * @param file - The path to the file inside the tarball.\n * @param destinationFilePath - The destination file path.\n * @returns True if the file was extracted successfully, false otherwise.\n */\nexport async function extractFileFromTarGzip(\n tarballPath: string,\n file: string,\n destinationFilePath: string\n) {\n const result = await parseTarGzip(await readFile(tarballPath));\n\n const entry = result.find(e => e.name === file);\n if (!entry?.data) {\n return;\n }\n\n if (!(await exists(destinationFilePath))) {\n await mkdir(destinationFilePath, { recursive: true });\n }\n\n const stream = createWriteStream(destinationFilePath);\n stream.write(entry.data);\n}\n"],"mappings":";;;;;;;;;;;;AA6BA,SAAgB,oBAAoB,MAAc;AAChD,KAAIA,aAAW,KAAK,CAClB;AAGF,QAAO,UAAU,MAAM,EAAE,WAAW,MAAM,CAAC;;;;;;;;AAS7C,eAAsB,gBAAgB,MAAc;AAClD,KAAI,MAAM,OAAO,KAAK,CACpB;AAGF,QAAO,MAAM,MAAM,EAAE,WAAW,MAAM,CAAC;;;;;;;;AASzC,SAAgB,oBAAoB,MAAc;AAChD,KAAI,CAACA,aAAW,KAAK,CACnB;AAGF,QAAO,OAAO,MAAM,EAAE,WAAW,MAAM,CAAC;;;;;;;;AAS1C,eAAsB,gBAAgB,MAAc;AAClD,KAAI,CAACA,aAAW,KAAK,CACnB;AAGF,QAAO,GAAG,MAAM,EAAE,WAAW,MAAM,CAAC;;;;;;;;;;AAWtC,eAAsB,mBACpB,aACA,MACA,qBACA;CAGA,MAAM,QAFS,SAAS,MAAM,SAAS,YAAY,CAE/B,CAAC,MAAK,MAAK,EAAE,SAAS,KAAK;AAC/C,KAAI,CAAC,OAAO,KACV;AAGF,KAAI,CAAE,MAAM,OAAO,oBAAoB,CACrC,OAAM,MAAM,qBAAqB,EAAE,WAAW,MAAM,CAAC;AAIvD,CADe,kBAAkB,oBAC3B,CAAC,MAAM,MAAM,KAAK;;;;;;;;;;AAW1B,eAAsB,uBACpB,aACA,MACA,qBACA;CAGA,MAAM,SAAQ,MAFO,aAAa,MAAM,SAAS,YAAY,CAAC,EAEzC,MAAK,MAAK,EAAE,SAAS,KAAK;AAC/C,KAAI,CAAC,OAAO,KACV;AAGF,KAAI,CAAE,MAAM,OAAO,oBAAoB,CACrC,OAAM,MAAM,qBAAqB,EAAE,WAAW,MAAM,CAAC;AAIvD,CADe,kBAAkB,oBAC3B,CAAC,MAAM,MAAM,KAAK"}