diff --git a/01-read-file/index.js b/01-read-file/index.js index e69de29bb..001f6d272 100644 --- a/01-read-file/index.js +++ b/01-read-file/index.js @@ -0,0 +1,9 @@ +const fs = require('fs'); + +fs.readFile('./text.txt', 'utf8', (err, data) => { + if (err) { + console.error(err); + } else { + console.log(data); + } +}); diff --git a/02-write-file/index.js b/02-write-file/index.js index e69de29bb..0c7467103 100644 --- a/02-write-file/index.js +++ b/02-write-file/index.js @@ -0,0 +1,18 @@ +const fs = require('fs'); +const readline = require('readline'); + +console.log('Привет! Введи текст для записи в файл:'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +rl.on('line', (text) => { + fs.writeFile('text.txt', text, (err) => { + if (err) throw err; + + console.log(`Текст "${text}" успешно записан в файл text.txt`); + rl.close(); + }); +}); diff --git a/03-files-in-folder/index.js b/03-files-in-folder/index.js index e69de29bb..3b2b0462c 100644 --- a/03-files-in-folder/index.js +++ b/03-files-in-folder/index.js @@ -0,0 +1,13 @@ +const fs = require('fs'); + +const folderPath = './03-files-in-folder/secret-folder'; + +fs.readdir(folderPath, (err, files) => { + if (err) { + console.error('Error reading directory:', err); + return; + } + + console.log(`Files in directory ${folderPath}:`); + files.forEach(file => console.log(file)); +}); diff --git a/04-copy-directory/index.js b/04-copy-directory/index.js index 8b1378917..a2c9d1fac 100644 --- a/04-copy-directory/index.js +++ b/04-copy-directory/index.js @@ -1 +1,23 @@ +const fs = require('fs'); +function copyDir(sourceDir, targetDir) { + if (!fs.existsSync(sourceDir)) { + throw new Error(`Source directory "${sourceDir}" does not exist`); + } + if (!fs.existsSync(targetDir)) { + fs.mkdirSync(targetDir); + } + const files = fs.readdirSync(sourceDir); + files.forEach((file) => { + const sourcePath = `${sourceDir}/${file}`; + const targetPath = `${targetDir}/${file}`; + const stat = fs.statSync(sourcePath); + if (stat.isDirectory()) { + copyDir(sourcePath, targetPath); + } else { + fs.copyFileSync(sourcePath, targetPath); + } + }); +} + +copyDir('./files', './files-copy'); diff --git a/05-merge-styles/index.js b/05-merge-styles/index.js index e69de29bb..2bffc8f74 100644 --- a/05-merge-styles/index.js +++ b/05-merge-styles/index.js @@ -0,0 +1,21 @@ +const fs = require('fs'); +const concat = require('concat'); + +const stylesFolderPath = './styles'; +const outputFilePath = './project-dist/bundle.css'; + +fs.readdir(stylesFolderPath, (err, files) => { + if (err) { + throw err; + } + + const fileContents = files.map((fileName) => { + return fs.readFileSync(`${stylesFolderPath}/${fileName}`, 'utf-8'); + }); + + const concatenatedContents = concat(fileContents); + + fs.writeFileSync(outputFilePath, concatenatedContents, 'utf-8'); + + console.log('Styles merged successfully'); +});