Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit 82f1ea1

Browse files
niccordEomm
authored andcommitted
Add appendToFile (#4)
Feature: appendToFile: Issue #1
1 parent f397fe1 commit 82f1ea1

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ fue.readDirectoryFiles('path/')
2929

3030
```
3131

32-
3332
## API
3433

3534

3635
* [file-utils-easy](#module_file-utils-easy)
3736
* [~writeToFile(fileContent, filePath)](#module_file-utils-easy..writeToFile) ⇒ <code>Promise.&lt;string&gt;</code>
3837
* [~writeToFileStream(fileStream, filePath)](#module_file-utils-easy..writeToFileStream) ⇒ <code>Promise</code>
38+
* [~appendToFile(fileContent, filePath)](#module_file-utils-easy..appendToFile) ⇒ <code>Promise.&lt;string&gt;</code>
3939
* [~readFileStats(filePath)](#module_file-utils-easy..readFileStats) ⇒ <code>Promise.&lt;fs.Stats&gt;</code>
4040
* [~readDirectoryFiles(directory)](#module_file-utils-easy..readDirectoryFiles) ⇒ <code>Promise.&lt;array&gt;</code>
4141
* [~readFile(filePath, [encoding])](#module_file-utils-easy..readFile) ⇒ <code>Promise.&lt;string&gt;</code>
@@ -73,6 +73,19 @@ Write a stream to a file
7373
| fileStream | <code>stream</code> | the stream payload |
7474
| filePath | <code>string</code> | path and filename: where store the file |
7575

76+
<a name="module_file-utils-easy..appendToFile"></a>
77+
78+
### file-utils-easy~appendToFile(fileContent, filePath) ⇒ <code>Promise.&lt;string&gt;</code>
79+
Append a string to a file
80+
81+
**Kind**: inner method of [<code>file-utils-easy</code>](#module_file-utils-easy)
82+
**Returns**: <code>Promise.&lt;string&gt;</code> - resolve with the filePath received in input
83+
84+
| Param | Type | Description |
85+
| --- | --- | --- |
86+
| fileContent | <code>string</code> | the payload of the file |
87+
| filePath | <code>string</code> | path and filename: where store the file |
88+
7689
<a name="module_file-utils-easy..readFileStats"></a>
7790

7891
### file-utils-easy~readFileStats(filePath) ⇒ <code>Promise.&lt;fs.Stats&gt;</code>
@@ -203,7 +216,6 @@ Check if a file exists
203216
| filePath | <code>string</code> | path and filename: the file to control |
204217

205218

206-
207219
## Test
208220

209221
For run the tests simply execute:

index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ function writeToFile(fileContent, filePath) {
2828
});
2929
}
3030

31+
/**
32+
* Append a string to a file, if the file doesn't exist it is created
33+
* @param {string} fileContent the payload of the file
34+
* @param {string} filePath path and filename: where store the file
35+
* @returns {Promise<string>} resolve with the filePath received in input
36+
*/
37+
function appendToFile(fileContent, filePath) {
38+
return new Promise((resolve, reject) => {
39+
fs.appendFile(filePath, fileContent, 'utf8', (err) => {
40+
if (err) {
41+
reject(err);
42+
} else {
43+
resolve(filePath);
44+
}
45+
});
46+
});
47+
}
48+
3149

3250
/**
3351
* Write a stream to a file
@@ -239,6 +257,7 @@ function existFile(filePath, mode = fs.constants.W_OK) {
239257

240258
module.exports = Object.freeze({
241259
writeToFile,
260+
appendToFile,
242261
writeToFileStream,
243262
readDirectoryFiles,
244263
readFile,

test/assets/append.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world - this is some added text

test/file-utils-easy.test.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ describe('write tests', () => {
3535
});
3636
});
3737

38+
describe('append tests', () => {
39+
it('create with append relative', async () => {
40+
const fileContent = 'Hello world';
41+
const filePath = 'test/assets/append.txt';
42+
try {
43+
await fue.deleteFile(filePath);
44+
} catch (err) { }
45+
return expect(fue.appendToFile(fileContent, filePath)).resolves.toEqual(filePath);
46+
});
47+
48+
it('append relative', async () => {
49+
const fileContent = 'Hello world';
50+
const filePath = 'test/assets/append.txt';
51+
const addingContent = ' - this is some added text';
52+
try {
53+
await fue.deleteFile(filePath);
54+
} catch (err) { }
55+
const res = await fue.writeToFile(fileContent, filePath).then(fp => fue.appendToFile(addingContent, fp));
56+
expect(res).toEqual(filePath);
57+
return expect(fue.readFile(filePath)).resolves.toEqual(fileContent + addingContent);
58+
});
59+
60+
it('append in not existing path', () => {
61+
const fileContent = 'Hello world';
62+
const filePath = 'output/not-exist.txt';
63+
return expect(fue.appendToFile(fileContent, filePath))
64+
.rejects.toBeDefined();
65+
});
66+
});
3867

3968
describe('alter tests', () => {
4069
it('rename file to existing path', () => {
@@ -117,12 +146,11 @@ describe('delete tests', () => {
117146
});
118147
});
119148

120-
121149
describe('read tests', () => {
122150
it('file list', () => {
123151
const path = 'test/assets/';
124152
return expect(fue.readDirectoryFiles(path))
125-
.resolves.toHaveLength(3);
153+
.resolves.toHaveLength(4);
126154
});
127155

128156
it('file list not existing path', () => {

0 commit comments

Comments
 (0)