Skip to content

Commit

Permalink
feat: finish 2nd task
Browse files Browse the repository at this point in the history
  • Loading branch information
elrouss committed May 8, 2023
1 parent 3f54dfa commit 23450b4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 02-write-file/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const path = require('path');

const { stdin, exit } = process;
const stream = fs.createWriteStream(path.join(__dirname, 'text.txt'));

stdin.write(
`\nHi! You can write here text and it will be added to a new file.
To exit the terminal just type 'exit' or press 'Ctrl + C'\n\n`
);

stdin.on('data', (chunk) => {
String(chunk).slice(0, 4) === 'exit' ? exit() : stream.write(chunk);
});

process.on('error', ({ message }) => console.log(`\nThere was an error:\n ${message}\n`));
process.on('SIGINT', () => exit());
process.on('exit', () => stdin.write('\nThe process is finished. Goodbye!\n'));

0 comments on commit 23450b4

Please sign in to comment.