@stryke/fs 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/files/write-file.cjs +23 -15
- package/dist/files/write-file.d.ts +12 -4
- package/dist/files/write-file.mjs +3 -3
- package/package.json +1 -1
|
@@ -6,26 +6,34 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.writeFileSync = exports.writeFile = void 0;
|
|
7
7
|
exports.writeJsonFile = writeJsonFile;
|
|
8
8
|
exports.writeJsonFileSync = writeJsonFileSync;
|
|
9
|
+
var _stormJson = require("@stryke/json/storm-json");
|
|
10
|
+
var _exists = require("@stryke/path/utilities/exists");
|
|
11
|
+
var _filePathFns = require("@stryke/path/utilities/file-path-fns");
|
|
12
|
+
var _normalizePath = require("@stryke/path/utilities/normalize-path");
|
|
9
13
|
var _nodeFs = require("node:fs");
|
|
10
14
|
var _promises = require("node:fs/promises");
|
|
11
|
-
var
|
|
12
|
-
const writeFileSync = (
|
|
13
|
-
if (!
|
|
14
|
-
(0,
|
|
15
|
+
var _helpers = require("./helpers.cjs");
|
|
16
|
+
const writeFileSync = (r, o, t) => {
|
|
17
|
+
if (!r) throw new Error("No file path provided to write data");
|
|
18
|
+
const e = (0, _filePathFns.findFilePath)((0, _normalizePath.normalizePath)(r));
|
|
19
|
+
if (!(0, _exists.existsSync)(e)) if (t?.createDirectory !== !1) (0, _helpers.createDirectorySync)(e);else throw new Error(`Directory ${e} does not exist`);
|
|
20
|
+
(0, _nodeFs.writeFileSync)(r, o || "", t);
|
|
15
21
|
},
|
|
16
|
-
writeFile = async (
|
|
17
|
-
if (!
|
|
18
|
-
|
|
22
|
+
writeFile = async (r, o, t) => {
|
|
23
|
+
if (!r) throw new Error("No file path provided to read data");
|
|
24
|
+
const e = (0, _filePathFns.findFilePath)((0, _normalizePath.normalizePath)(r));
|
|
25
|
+
if (!(0, _exists.existsSync)(e)) if (t?.createDirectory !== !1) await (0, _helpers.createDirectory)(e);else throw new Error(`Directory ${e} does not exist`);
|
|
26
|
+
return (0, _promises.writeFile)(r, o || "", t);
|
|
19
27
|
};
|
|
20
28
|
exports.writeFile = writeFile;
|
|
21
29
|
exports.writeFileSync = writeFileSync;
|
|
22
|
-
function writeJsonFileSync(
|
|
23
|
-
const
|
|
24
|
-
return writeFileSync(
|
|
25
|
-
` :
|
|
30
|
+
function writeJsonFileSync(r, o, t) {
|
|
31
|
+
const e = _stormJson.StormJSON.stringify(o, t);
|
|
32
|
+
return writeFileSync(r, t?.appendNewLine ? `${e}
|
|
33
|
+
` : e);
|
|
26
34
|
}
|
|
27
|
-
async function writeJsonFile(
|
|
28
|
-
const
|
|
29
|
-
return writeFile(
|
|
30
|
-
` :
|
|
35
|
+
async function writeJsonFile(r, o, t) {
|
|
36
|
+
const e = _stormJson.StormJSON.stringify(o);
|
|
37
|
+
return writeFile(r, t?.appendNewLine ? `${e}
|
|
38
|
+
` : e);
|
|
31
39
|
}
|
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import type { JsonSerializeOptions } from "@stryke/json/types";
|
|
2
2
|
import type { Abortable } from "node:events";
|
|
3
|
-
import type { Mode, ObjectEncodingOptions, OpenMode
|
|
3
|
+
import type { WriteFileOptions as FSWriteFileOptions, Mode, ObjectEncodingOptions, OpenMode } from "node:fs";
|
|
4
4
|
import type { Encoding } from "./constants";
|
|
5
|
+
export interface WriteFileOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Whether to create the directory if it does not exist
|
|
8
|
+
*
|
|
9
|
+
* @defaultValue true
|
|
10
|
+
*/
|
|
11
|
+
createDirectory?: boolean;
|
|
12
|
+
}
|
|
5
13
|
/**
|
|
6
14
|
* Write the given content to the given file path
|
|
7
15
|
*
|
|
8
16
|
* @param filePath - The file path to write to
|
|
9
17
|
* @param content - The content to write to the file
|
|
10
18
|
*/
|
|
11
|
-
export declare const writeFileSync: (filePath: string, content?: any, options?: WriteFileOptions) => void;
|
|
19
|
+
export declare const writeFileSync: (filePath: string, content?: any, options?: WriteFileOptions & FSWriteFileOptions) => void;
|
|
12
20
|
/**
|
|
13
21
|
* Read the given content to the given file path
|
|
14
22
|
*
|
|
@@ -16,11 +24,11 @@ export declare const writeFileSync: (filePath: string, content?: any, options?:
|
|
|
16
24
|
* @param content - The content to write to the file
|
|
17
25
|
* @returns The content of the file
|
|
18
26
|
*/
|
|
19
|
-
export declare const writeFile: (filePath: string, content?: any, options?: (ObjectEncodingOptions & {
|
|
27
|
+
export declare const writeFile: (filePath: string, content?: any, options?: WriteFileOptions & ((ObjectEncodingOptions & {
|
|
20
28
|
mode?: Mode | undefined;
|
|
21
29
|
flag?: OpenMode | undefined;
|
|
22
30
|
flush?: boolean | undefined;
|
|
23
|
-
} & Abortable) | Encoding
|
|
31
|
+
} & Abortable) | Encoding)) => Promise<void>;
|
|
24
32
|
export interface JsonWriteOptions extends JsonSerializeOptions {
|
|
25
33
|
/**
|
|
26
34
|
* whether to append new line at the end of JSON file
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{writeFileSync as
|
|
2
|
-
`:
|
|
3
|
-
`:
|
|
1
|
+
import{StormJSON as i}from"@stryke/json/storm-json";import{existsSync as n}from"@stryke/path/utilities/exists";import{findFilePath as s}from"@stryke/path/utilities/file-path-fns";import{normalizePath as p}from"@stryke/path/utilities/normalize-path";import{writeFileSync as c}from"node:fs";import{writeFile as d}from"node:fs/promises";import{createDirectory as a,createDirectorySync as f}from"./helpers";export const writeFileSync=(r,o,t)=>{if(!r)throw new Error("No file path provided to write data");const e=s(p(r));if(!n(e))if(t?.createDirectory!==!1)f(e);else throw new Error(`Directory ${e} does not exist`);c(r,o||"",t)},writeFile=async(r,o,t)=>{if(!r)throw new Error("No file path provided to read data");const e=s(p(r));if(!n(e))if(t?.createDirectory!==!1)await a(e);else throw new Error(`Directory ${e} does not exist`);return d(r,o||"",t)};export function writeJsonFileSync(r,o,t){const e=i.stringify(o,t);return writeFileSync(r,t?.appendNewLine?`${e}
|
|
2
|
+
`:e)}export async function writeJsonFile(r,o,t){const e=i.stringify(o);return writeFile(r,t?.appendNewLine?`${e}
|
|
3
|
+
`:e)}
|