UNPKG

@stryke/fs

Version:

A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.

1 lines 3.96 kB
{"version":3,"file":"write-file.mjs","names":["existsSync","writeFileFs"],"sources":["../src/write-file.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 { correctPath } from \"@stryke/path/correct-path\";\nimport { findFilePath } from \"@stryke/path/file-path-fns\";\nimport type { Abortable } from \"node:events\";\nimport type {\n WriteFileOptions as FSWriteFileOptions,\n Mode,\n ObjectEncodingOptions,\n OpenMode\n} from \"node:fs\";\nimport { writeFileSync as writeFileSyncFs } from \"node:fs\";\nimport { writeFile as writeFileFs } from \"node:fs/promises\";\nimport type { Encoding } from \"./constants\";\nimport { existsSync } from \"./exists\";\nimport { createDirectory, createDirectorySync } from \"./helpers\";\n\nexport interface WriteFileOptions {\n /**\n * Whether to create the directory if it does not exist\n *\n * @defaultValue true\n */\n createDirectory?: boolean;\n}\n\n/**\n * Write the given content to the given file path\n *\n * @param filePath - The file path to write to\n * @param content - The content to write to the file\n */\nexport const writeFileSync = (\n filePath: string,\n content = \"\",\n options: WriteFileOptions & FSWriteFileOptions = {}\n): void => {\n if (!filePath) {\n throw new Error(\"No file path provided to write data\");\n }\n\n const directory = findFilePath(correctPath(filePath));\n if (!existsSync(directory)) {\n if (options.createDirectory !== false) {\n createDirectorySync(directory);\n } else {\n throw new Error(`Directory ${directory} does not exist`);\n }\n }\n\n writeFileSyncFs(filePath, content || \"\", options);\n};\n\n/**\n * Read the given content to the given file path\n *\n * @param filePath - The file path to read to\n * @param content - The content to write to the file\n * @returns The content of the file\n */\nexport const writeFile = async (\n filePath: string,\n content = \"\",\n options: WriteFileOptions &\n (\n | (ObjectEncodingOptions & {\n mode?: Mode | undefined;\n flag?: OpenMode | undefined;\n flush?: boolean | undefined;\n } & Abortable)\n | Encoding\n ) = {}\n): Promise<void> => {\n if (!filePath) {\n throw new Error(\"No file path provided to read data\");\n }\n\n const directory = findFilePath(correctPath(filePath));\n if (!existsSync(directory)) {\n if (options.createDirectory !== false) {\n await createDirectory(directory);\n } else {\n throw new Error(`Directory ${directory} does not exist`);\n }\n }\n\n return writeFileFs(filePath, content || \"\", options);\n};\n"],"mappings":";;;;;;;;;;;;;;AAgDA,MAAa,iBACX,UACA,UAAU,IACV,UAAiD,EAAE,KAC1C;AACT,KAAI,CAAC,SACH,OAAM,IAAI,MAAM,sCAAsC;CAGxD,MAAM,YAAY,aAAa,YAAY,SAAS,CAAC;AACrD,KAAI,CAACA,aAAW,UAAU,CACxB,KAAI,QAAQ,oBAAoB,MAC9B,qBAAoB,UAAU;KAE9B,OAAM,IAAI,MAAM,aAAa,UAAU,iBAAiB;AAI5D,iBAAgB,UAAU,WAAW,IAAI,QAAQ;;;;;;;;;AAUnD,MAAa,YAAY,OACvB,UACA,UAAU,IACV,UAQM,EAAE,KACU;AAClB,KAAI,CAAC,SACH,OAAM,IAAI,MAAM,qCAAqC;CAGvD,MAAM,YAAY,aAAa,YAAY,SAAS,CAAC;AACrD,KAAI,CAACA,aAAW,UAAU,CACxB,KAAI,QAAQ,oBAAoB,MAC9B,OAAM,gBAAgB,UAAU;KAEhC,OAAM,IAAI,MAAM,aAAa,UAAU,iBAAiB;AAI5D,QAAOC,YAAY,UAAU,WAAW,IAAI,QAAQ"}