Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 01-read-file/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require("fs");
const path = require("node:path");

let pathTXT = path.join(__dirname, "text.txt");
let stream = new fs.ReadStream(pathTXT);

stream.on("data", (e) => {
console.log(e.toString());
});


2 changes: 1 addition & 1 deletion 01-read-file/text.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Нет хуже причины для выбора имени с, чем та, что имена a и b уже заняты.
Нет хуже причины для выбора имени с, чем та, что имена a и b уже заняты.
23 changes: 23 additions & 0 deletions 02-write-file/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require("fs");
const path = require("node:path");
let pathTXT = path.join(__dirname, "text.txt");

fs.appendFile(pathTXT, "", function (err) {
if (err) throw err;
console.log("Введите, пожалуйста, текст в консоль!");
let stdin = process.openStdin();
stdin.on("data", function (chunk) {
if (chunk.toString().trim() == "exit") {
console.log("Благодарю, что сделали проверку этого задания!");
process.exit(0);
}
fs.appendFile(pathTXT, chunk, function (err) {
if (err) throw err;
});
console.log("Got chunk: " + chunk);
process.on("SIGINT", function () {
console.log("Благодарю, что сделали проверку этого задания!");
process.exit(0);
});
});
});
37 changes: 37 additions & 0 deletions 03-files-in-folder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require("fs");
const path = require("node:path");

let dirPath = path.join(__dirname, "secret-folder");

function readDirectory(src) {
fs.readdir(src, (error, files) => {
if (error) {
console.error(error);
return;
}

files.forEach((file) => {
let dirPath = path.join(src, file);

fs.stat(dirPath, (error, stats) => {
if (error) {
console.error(error);
return;
}

if (stats.isFile()) {
const fileFormat = path.extname(dirPath);
const fileName = path.basename(dirPath, fileFormat);
const fileSize = stats.size;
const outputLine = `${fileName} - ${fileFormat.slice(
1
)} - ${fileSize}b`;

console.log(outputLine);
}
});
});
});
}

readDirectory(dirPath);
2 changes: 1 addition & 1 deletion 04-copy-directory/files/test-css.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* {
margin: unset;
}
}
21 changes: 21 additions & 0 deletions 04-copy-directory/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
const fs = require("fs");
const path = require("node:path");

let dirPathNew = path.join(__dirname, "files-copy");
let dirPathFile = path.join(__dirname, "files");

fs.mkdir(dirPathNew, { recursive: true }, function (err) {
if (err) throw err;
console.log("Папка успешно создана");
fs.readdir(dirPathFile, "utf8", function (err, data) {
for (const key in data) {
fs.copyFile(
dirPathFile + "\\" + data[key],
dirPathNew + "\\" + data[key],
(err) => {
if (err) throw err;
console.log("Файл успешно скопирован");
}
);
}
});
});
51 changes: 51 additions & 0 deletions 05-merge-styles/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require("fs");
const path = require("node:path");

const source = path.join(__dirname, 'styles');
const bundle = path.join(__dirname, 'project-dist', 'bundle.css');

function copyFiles(src, dist) {
fs.readdir(src, (error, files) => {
if (error) {
console.error(error);
return;
}

fs.writeFile(dist, '', (error) => {
if (error) {
console.error(error);
return;
}
});

files.forEach((file) => {
const fullPath = path.join(src, file);

fs.stat(fullPath, (error, stats) => {
if (error) {
console.error(error);
return;
}

if (stats.isFile() && path.extname(fullPath) === '.css') {
appendFile(fullPath, dist);
}
});
});
});
}

function appendFile(src, dist) {
const stream = fs.createReadStream(src, 'utf8');

stream.on('data', (chunk) => {
fs.appendFile(dist, chunk, (error) => {
if (error) {
console.error(error);
return;
}
});
});
}

copyFiles(source, bundle);
135 changes: 135 additions & 0 deletions 06-build-page/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
let path = require("path");
let fs = require("fs");

let pathCopy = path.join(__dirname, "project-dist");
let pathAssCopy = path.join(pathCopy, "assets");
let folderPath = path.join(__dirname, "components");
let pathCSS = path.join(__dirname, "styles");
let pathAss = path.join(__dirname, "assets");

function copyDir(dir, exit) {
fs.readdir(dir, { withFileTypes: true }, function (err, files) {
if (err) throw err;
files.forEach(function (file) {
if (!file.isFile()) {
fs.stat(path.join(exit, file.name), function (err) {
if (err) {
fs.mkdir(path.join(exit, file.name), function (err) {
if (err) {
return console.erroror(err);
}
});
copyDir(`${dir}\\${file.name}`, path.join(exit, file.name));
} else {
copyDir(`${dir}\\${file.name}`, path.join(exit, file.name));
}
});
} else {
fs.copyFile(
`${dir}\\${file.name}`,
`${exit}\\${file.name}`,
function (err) {
if (err) throw err;
}
);
}
});
});
}

fs.stat(pathCopy, function (err) {
if (err) {
fs.mkdir(pathCopy, function (err) {
if (err) {
return console.erroror(err);
}
});
copyHTML();
} else {
fs.readdir(pathCopy, function (err) {
if (err) console.log(err);
else {
copyHTML();
}
});
}
});

fs.stat(pathAssCopy, function (err) {
if (err) {
fs.mkdir(pathAssCopy, function (err) {
if (err) {
return console.erroror(err);
}
});
copyDir(pathAss, pathAssCopy);
} else {
copyDir(pathAss, pathAssCopy);
}
});

// копируем template.html в index.html
function copyHTML() {
fs.copyFile(
`${__dirname}\\template.html`,
`${pathCopy}\\index.html`,
function (err) {
if (err) throw err;
fs.readFile(`${pathCopy}\\index.html`, "utf8", function (err, data) {
if (err) throw err;
fs.readdir(folderPath, { withFileTypes: true }, function (err, files) {
if (err) throw err;

files.forEach(function (file) {
fs.readFile(
`${folderPath}\\${file.name}`,
"utf8",
function (err, dataFile) {
if (err) throw err;
let tagName = `{{${file.name.split(".")[0]}}}`;
data = data.replace(tagName, dataFile);
fs.writeFile(`${pathCopy}\\index.html`, data, function (err) {
if (err) console.log(err);
});
}
);
});
});
});
}
);
}

// css
fs.readdir(pathCSS, { withFileTypes: true }, async (err, all) => {
if (err) {
console.log(err);
} else {
all.forEach(function (one, ind) {
let cssPath = path.join(pathCSS, one.name);
if (one.isFile() && one.name.split(".")[1] === "css") {
fs.readFile(cssPath, "utf8", function (err, data) {
if (err) {
console.log(err);
} else if (ind === 0) {
fs.writeFile(
path.join(pathCopy, "style.css"),
data,
function (err) {
if (err) console.log(err);
}
);
} else {
fs.appendFile(
path.join(pathCopy, "style.css"),
data,
function (err) {
if (err) console.log(err);
}
);
}
});
}
});
}
});
Loading