Skip to content

Commit

Permalink
Merge pull request #52 from Psychopoulet/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Psychopoulet committed Mar 17, 2021
2 parents 6060f31 + 5460e25 commit 7cafee1
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 70 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2019, Sébastien Vidal
Copyright (c) 2021, Sébastien Vidal

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
29 changes: 23 additions & 6 deletions README.md
Expand Up @@ -40,6 +40,23 @@ type tError = ReferenceError|TypeError|Error|null;
type tCallback = (err: tError, data: any) => void;
```

```typescript
interface iReadJSONOptions {
"encoding"?: string;
"flag"?: string;
"signal"?: AbortSignal;
}

interface iWriteJSONOptions extends iReadJSONOptions {
"mode"?: number;
"replacer"?: (k, v) => any;
"space"?: string|number|null;
}

type tReadJSONOptions = string|iReadJSONOptions|null;
type tWriteJSONOptions = string|iWriteJSONOptions|null;
```

### -- Extended --

> in "separator" parameter, you can use "{{filename}}" pattern, it will be replaced by the file's basename (ex : separator = "\r\n\r\n--- {{filename}} ---\r\n\r\n")
Expand All @@ -66,14 +83,14 @@ type tCallback = (err: tError, data: any) => void;
* ``` extractFilesProm(dir: string): Promise<Array<string>> ```

[writeJSONFile] : stringify JSON and writeFile
* ``` writeJSONFile(file: string, data: any, callback: (err: tError) => void, replacer?: (k, v) => any, space?: string|number|null): void ```
* ``` writeJSONFileSync(file: string, data: any, replacer?: (k, v) => any, space?: string|number|null): void ```
* ``` writeJSONFileProm(file: string, data: any, replacer?: (k, v) => any, space?: string|number|null): Promise<void> ```
* ``` writeJSONFile(file: string, data: any, options: tCallback | tWriteJSONOptions, callback?: tCallback): void ```
* ``` writeJSONFileSync(file: string, data: any, options?: tReadJSONOptions): void ```
* ``` writeJSONFileProm(file: string, data: any, options?: tReadJSONOptions): Promise<void> ```

[readJSONFile] : readFile and parse JSON
* ``` readJSONFile(file: string, opts: tCallback | object | string | null, callback?: tCallback): void ```
* ``` readJSONFileSync(file: string, opts?: object | string): any ```
* ``` readJSONFileProm(file: string, opts?: object | string) : Promise<any> ```
* ``` readJSONFile(file: string, opts: tCallback | tReadJSONOptions, callback?: tCallback): void ```
* ``` readJSONFileSync(file: string, opts?: tReadJSONOptions): any ```
* ``` readJSONFileProm(file: string, opts?: tReadJSONOptions) : Promise<any> ```


[mkdirp] : recursively create a directory
Expand Down
13 changes: 7 additions & 6 deletions lib/extends/_readJSONFile.js
Expand Up @@ -15,11 +15,11 @@
/**
* Async readJSONFile
* @param {string} file : file to check
* @param {function|null|object} options : operation's result
* @param {function|null|object} opts : operation's result or options
* @param {function} cb : operation's result
* @returns {void}
*/
function _readJSONFile (file, options, cb) {
function _readJSONFile (file, opts, cb) {

if ("undefined" === typeof file) {
throw new ReferenceError("missing \"file\" argument");
Expand All @@ -31,16 +31,17 @@
throw new Error("\"file\" argument is empty");
}

else if ("undefined" === typeof options) {
else if ("undefined" === typeof opts) {
throw new ReferenceError("missing \"callback\" argument");
}
else if ("undefined" === typeof cb && "function" !== typeof options) {
else if ("undefined" === typeof cb && "function" !== typeof opts) {
throw new TypeError("\"callback\" argument is not a function");
}

else {

const callback = "function" === typeof cb ? cb : options;
const callback = "function" === typeof cb ? cb : opts;
const options = "function" === typeof cb ? opts : null;

if ("function" !== typeof callback) {
throw new TypeError("\"callback\" argument is not a function");
Expand All @@ -53,7 +54,7 @@
Promise.reject(new Error("The file does not exist")) :
new Promise((resolve, reject) => {

readFile(file, "function" === typeof cb ? options : null, (err, content) => {
readFile(file, options, (err, content) => {
return err ? reject(err) : resolve(content);
});

Expand Down
68 changes: 41 additions & 27 deletions lib/extends/_writeJSONFile.js
Expand Up @@ -19,13 +19,12 @@
/**
* Async writeJSONFile
* @param {string} file : file to check
* @param {function} data : data to write
* @param {function} callback : operation's result
* @param {function|null} replacer : JSON.stringify argument
* @param {string|number|null} space : JSON.stringify argument
* @param {any} data : data to write
* @param {function|null|object} opts : operation's result or options
* @param {function} cb : operation's result
* @returns {void}
*/
function _writeJSONFile (file, data, callback, replacer, space) {
function _writeJSONFile (file, data, opts, cb) {

if ("undefined" === typeof file) {
throw new ReferenceError("missing \"file\" argument");
Expand All @@ -40,38 +39,51 @@
else if ("undefined" === typeof data) {
throw new ReferenceError("missing \"data\" argument");
}
else if ("undefined" === typeof callback) {
throw new ReferenceError("missing \"callback\" argument");
}
else if ("function" !== typeof callback) {

else if ("undefined" === typeof opts) {
throw new ReferenceError("missing \"callback\" argument");
}
else if ("undefined" === typeof cb && "function" !== typeof opts) {
throw new TypeError("\"callback\" argument is not a function");
}

else {

isFileProm(file).then((exists) => {
const callback = "function" === typeof cb ? cb : opts;
const options = "function" === typeof cb ? opts : null;

return !exists ? Promise.resolve() : new Promise((resolve, reject) => {
if ("function" !== typeof callback) {
throw new TypeError("\"callback\" argument is not a function");
}
else {

isFileProm(file).then((exists) => {

return !exists ? Promise.resolve() : new Promise((resolve, reject) => {

unlink(file, (err) => {
return err ? reject(err) : resolve();
});

unlink(file, (err) => {
return err ? reject(err) : resolve();
});

});
}).then(() => {

}).then(() => {
return new Promise((resolve, reject) => {

return new Promise((resolve, reject) => {
writeFile(file, JSON.stringify(data,
options && options.replacer ? options.replacer : null, options && options.space ? options.space : null
), options, (err) => {
return err ? reject(err) : resolve();
});

writeFile(file, JSON.stringify(data, replacer, space), (err) => {
return err ? reject(err) : resolve();
});

});
}).then(() => {
callback(null);
}).catch(callback);

}).then(() => {
callback(null);
}).catch(callback);
}

}

Expand All @@ -87,21 +99,21 @@ module.exports = {

// promise version

"writeJSONFileProm": (file, data, replacer = null, space = null) => {
"writeJSONFileProm": (file, data, options = null) => {

return new Promise((resolve, reject) => {

_writeJSONFile(file, data, (err) => {
_writeJSONFile(file, data, options, (err) => {
return err ? reject(err) : resolve();
}, replacer, space);
});

});

},

// sync version

"writeJSONFileSync": (file, data, replacer = null, space = null) => {
"writeJSONFileSync": (file, data, options = null) => {

if ("undefined" === typeof file) {
throw new ReferenceError("missing \"file\" argument");
Expand All @@ -123,7 +135,9 @@ module.exports = {
unlinkSync(file);
}

writeFileSync(file, JSON.stringify(data, replacer, space));
writeFileSync(file, JSON.stringify(
data, options && options.replacer ? options.replacer : null, options && options.space ? options.space : null
), options);

}

Expand Down
40 changes: 31 additions & 9 deletions lib/index.d.ts
Expand Up @@ -2,10 +2,32 @@

declare module "node-promfs" {

import { Transform } from "stream";
// types & interfaces

type tError = ReferenceError|TypeError|Error|null;
type tCallback = (err: tError, data: any) => void;
// natives
import { Transform } from "stream";

// locals

type tError = ReferenceError|TypeError|Error|null;
type tCallback = (err: tError, data: any) => void;

// json

interface iReadJSONOptions {
"encoding"?: string;
"flag"?: string;
"signal"?: AbortSignal;
}

interface iWriteJSONOptions extends iReadJSONOptions {
"mode"?: number;
"replacer"?: (k, v) => any;
"space"?: string|number|null;
}

type tReadJSONOptions = string|iReadJSONOptions|null;
type tWriteJSONOptions = string|iWriteJSONOptions|null;

class PromFS extends require("fs") {

Expand All @@ -29,13 +51,13 @@ declare module "node-promfs" {
public static readFileProm(file: string, options) : Promise<string>;
public static writeFileProm(file: string, data: string, options) : Promise<void>;

public static readJSONFile(file: string, opts: tCallback | object | string | null, callback?: tCallback): void;
public static readJSONFileSync(file: string, opts?: object | string): any;
public static readJSONFileProm(file: string, opts?: object | string) : Promise<any>;
public static readJSONFile(file: string, opts: tCallback | tReadJSONOptions, callback?: tCallback): void;
public static readJSONFileSync(file: string, opts?: tReadJSONOptions): any;
public static readJSONFileProm(file: string, opts?: tReadJSONOptions) : Promise<any>;

public static writeJSONFile(file: string, data: any, callback: (err: tError) => void, replacer?: (k, v) => any, space?: string|number|null) : void;
public static writeJSONFileSync(file: string, data: any, replacer?: (k, v) => any, space?: string|number|null): void;
public static writeJSONFileProm(file: string, data: any, replacer?: (k, v) => any, space?: string|number|null) : Promise<void>;
public static writeJSONFile(file: string, data: any, options: tCallback | tWriteJSONOptions, callback?: tCallback) : void;
public static writeJSONFileSync(file: string, data: any, options?: tReadJSONOptions): void;
public static writeJSONFileProm(file: string, data: any, options?: tReadJSONOptions) : Promise<void>;

public static readJSONFile(file: string, callback: (err: tError, data: any) => void): void;
public static readJSONFileSync(file: string): any;
Expand Down
16 changes: 8 additions & 8 deletions lib/main.js
Expand Up @@ -145,7 +145,7 @@

};

fs.appendFileProm = (file, data, options) => {
fs.appendFileProm = (file, data, options = null) => {

if ("undefined" === typeof file) {
return Promise.reject(new ReferenceError("missing \"file\" argument"));
Expand All @@ -165,7 +165,7 @@

return "" === _file ? Promise.reject(new Error("\"file\" argument is empty")) : new Promise((resolve, reject) => {

fs.appendFile(_file, data, options ? options : null, (err) => {
fs.appendFile(_file, data, options, (err) => {
return err ? reject(err) : resolve();
});

Expand Down Expand Up @@ -270,7 +270,7 @@

};

fs.readFileProm = (file, options) => {
fs.readFileProm = (file, options = null) => {

if ("undefined" === typeof file) {
return Promise.reject(new ReferenceError("missing \"file\" argument"));
Expand All @@ -282,7 +282,7 @@

return new Promise((resolve, reject) => {

fs.readFile(file, options ? options : null, (err, result) => {
fs.readFile(file, options, (err, result) => {
return err ? reject(err) : resolve(result);
});

Expand All @@ -292,7 +292,7 @@

};

fs.realpathProm = (path, options) => {
fs.realpathProm = (path, options = null) => {

if ("undefined" === typeof path) {
return Promise.reject(new ReferenceError("missing \"path\" argument"));
Expand All @@ -306,7 +306,7 @@

return "" === _path ? Promise.reject(new Error("\"path\" argument is empty")) : new Promise((resolve, reject) => {

fs.realpath(_path, "undefined" !== typeof options ? options : null, (err, result) => {
fs.realpath(_path, options, (err, result) => {
return err ? reject(err) : resolve(result);
});

Expand Down Expand Up @@ -411,7 +411,7 @@

};

fs.writeFileProm = (file, data, options) => {
fs.writeFileProm = (file, data, options = null) => {

if ("undefined" === typeof file) {
return Promise.reject(new ReferenceError("missing \"file\" argument"));
Expand All @@ -429,7 +429,7 @@

return new Promise((resolve, reject) => {

fs.writeFile(file, data, options ? options : null, (err, result) => {
fs.writeFile(file, data, options, (err, result) => {
return err ? reject(err) : resolve(result);
});

Expand Down
12 changes: 6 additions & 6 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "node-promfs",
"version": "3.6.6",
"version": "3.7.0",
"description": "'fs' object extensions & promisifications",
"main": "lib/main.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -54,17 +54,17 @@
},
"dependencies": {},
"devDependencies": {
"@types/node": "14.14.31",
"typescript": "4.2.2",
"@types/node": "14.14.35",
"typescript": "4.2.3",
"check-version-modules": "1.3.0",
"coveralls": "3.1.0",
"eslint": "7.21.0",
"eslint": "7.22.0",
"husky": "5.1.3",
"mocha": "8.3.0",
"mocha": "8.3.2",
"nyc": "15.1.0"
},
"homepage": "https://github.com/Psychopoulet/node-promfs#readme",
"engines": {
"node": ">=6.0.0"
"node": ">=10.0.0"
}
}

0 comments on commit 7cafee1

Please sign in to comment.