diff --git a/.eslintrc.js b/.eslintrc.js index 664f4417f..04c1c8b70 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,7 +1,7 @@ module.exports = { 'env': { 'commonjs': true, - 'es2022': true, + 'es2021': true, 'node': true }, 'extends': 'eslint:recommended', diff --git a/01-read-file/index.js b/01-read-file/index.js index e69de29bb..56542a3c3 100644 --- a/01-read-file/index.js +++ b/01-read-file/index.js @@ -0,0 +1,17 @@ +const fs = require('fs'); +const path = require('path'); + +const readFile = () => { + const filePath = path.join(__dirname, './text.txt'); + const fileStream = fs.createReadStream(filePath, { encoding: 'utf8' }); + + logChunks(fileStream); +}; + +async function logChunks(readable) { + for await (const chunk of readable) { + console.log(chunk); + } +} + +readFile(); \ No newline at end of file diff --git a/02-write-file/index.js b/02-write-file/index.js index e69de29bb..681216818 100644 --- a/02-write-file/index.js +++ b/02-write-file/index.js @@ -0,0 +1,25 @@ +const fs = require('fs'); +const path = require('path'); + +const writeFile = () => { + const filePath = path.join(__dirname, './text.txt'); + + const file = fs.createWriteStream(filePath, { flags: 'a' }); + + process.stdin.on('data', (data) => { + if (data.toString().trim() === 'exit') { + console.log('Bye...'); + process.exit(); + } + file.write(data); + }); + + process.on('SIGINT', () => { + console.log('\n'); + console.log('Bye...'); + process.exit(); + }); +}; + +writeFile(); + \ No newline at end of file