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
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "es2015",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
}
}
6 changes: 5 additions & 1 deletion hw1/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": "es2015"
"extends": "es2015",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
}
}
3 changes: 3 additions & 0 deletions hw3/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "es2015"
}
44 changes: 44 additions & 0 deletions hw3/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Usage examples

### 1. Reverse string from process.stdin to process.stdout
`npm start -- -a reverse`

`npm start -- --action=reverse`



### 2. Convert string to uppercase from process.stdin to process.stdout

`npm start -- -a transform`

`npm start -- --action=transform`



### 3. Read file and print to console

`npm start -- -a outputFile -f users.txt`

`npm start -- --action=outputFile --file=users.txt`



### 4. Read .csv file, convert to JSON and print to console

`npm start -- -a convertFromFile -f users.csv`

`npm start -- --action=convertFromFile --file=users.csv`


### 5. Read .csv file, convert to JSON and write to .json file with the same name

`npm start -- -a convertToFile -f users.csv`

`npm start -- --action=convertToFile --file=users.csv`


### 6. Bundle all css files from given folder and add extra css styles for own file

`npm start -- -a cssBundler -p ./assets/css`

`npm start -- --action=cssBundler --path=./assets/css`
84 changes: 84 additions & 0 deletions hw3/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions hw3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "hw3",
"version": "1.0.0",
"description": "",
"main": "utils/streams.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node utils/streams.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/okachynskyy/nodejs-homeworks.git"
},
"keywords": [
"nodejs"
],
"author": "Oleksiy Kachynskyy <oleksiy.kachynskyy@gmail.com>",
"license": "ISC",
"bugs": {
"url": "https://github.com/okachynskyy/nodejs-homeworks/issues"
},
"homepage": "https://github.com/okachynskyy/nodejs-homeworks#readme",
"dependencies": {
"commander": "^2.15.1",
"csvjson": "^5.0.0",
"through2": "^2.0.3"
}
}
3 changes: 3 additions & 0 deletions hw3/utils/assets/css/1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: red;
}
3 changes: 3 additions & 0 deletions hw3/utils/assets/css/2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.header {
background-color: gray;
}
15 changes: 15 additions & 0 deletions hw3/utils/lib/convert-from-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require('fs');
const through = require('through2');
const csvJson = require('csvjson');

module.exports = (filePath, stdOut) => {
const parseCsv = function (buffer, encoding, next) {
const parsedCsv = csvJson.toObject(buffer.toString());
this.push(JSON.stringify(parsedCsv));
next();
}

fs.createReadStream(filePath)
.pipe(through(parseCsv))
.pipe(stdOut);
}
20 changes: 20 additions & 0 deletions hw3/utils/lib/convert-to-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const path = require('path');
const through = require('through2');
const csvJson = require('csvjson');
const pUtils = require('./path-utils');

module.exports = (filePath) => {
const parseCsv = function (buffer, encoding, next) {
const parsedCsv = csvJson.toObject(buffer.toString());
this.push(JSON.stringify(parsedCsv));
next();
}

const outputFilePath = pUtils.replaceExt(filePath, '.json');
fs.createReadStream(filePath)
.pipe(through(parseCsv))
.pipe(fs.createWriteStream(outputFilePath));

console.info(`Done! Data saved to: \n${outputFilePath}`);
}
18 changes: 18 additions & 0 deletions hw3/utils/lib/css-bundler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const path = require('path');

module.exports = (folderPath) => {
const files = fs.readdirSync(folderPath);
const outputFilePath = path.resolve(folderPath, `bundle.css`);
const outStream = fs.createWriteStream(outputFilePath);

files.forEach(file => {
const filePath = path.resolve(folderPath, file);
fs.createReadStream(filePath).pipe(outStream);
});

const extraContent = path.resolve(__dirname, './css-data/nodejs18-hw3-css.css');
fs.createReadStream(extraContent).pipe(outStream);

console.info(`Done! Styles saved to: \n${outputFilePath}`);
}
15 changes: 15 additions & 0 deletions hw3/utils/lib/css-data/nodejs18-hw3-css.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.ngmp18 {
background-color: #fff;
overflow: hidden;
width: 100%;
height: 100%;
position: relative;
}

.ngmp18__hw3 {
color: #333;
}

.ngmp18__hw3--t7 {
font-weight: bold;
}
6 changes: 6 additions & 0 deletions hw3/utils/lib/output-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const fs = require('fs');
const path = require('path');

module.exports = (filePath, stdOut) => {
fs.createReadStream(filePath).pipe(stdOut);
}
47 changes: 47 additions & 0 deletions hw3/utils/lib/path-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const fs = require('fs');
const path = require('path');

const resolveFilePath = (dir, fileName) => {
if (!fileName) {
throw new Error('Please, provide a file name!');
}
const filePath = path.resolve(__dirname, `../${fileName}`);

if (!fs.existsSync(filePath)) {
throw new Error(`File not exist: ${filePath}`);
}

return filePath;
};

const resolveFolderPath = (dir, relativeFolderPath) => {
if (!relativeFolderPath) {
throw new Error('Please, provide a folder path!');
}
const folderPath = path.resolve(__dirname, `../${relativeFolderPath}`);

if (!fs.existsSync(folderPath)) {
throw new Error(`Folder not exist: ${folderPath}`);
}

return folderPath;
};

const replaceExt = (npath, ext) => {
if (typeof npath !== 'string') {
return npath;
}

if (npath.length === 0) {
return npath;
}

var nFileName = path.basename(npath, path.extname(npath)) + ext;
return path.join(path.dirname(npath), nFileName);
}

module.exports = {
resolveFilePath,
resolveFolderPath,
replaceExt
}
16 changes: 16 additions & 0 deletions hw3/utils/lib/reverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const through = require('through2');

module.exports = (stdIn, stdOut) => {
const transform = function (buffer, encoding, next) {
const input = buffer.toString();

const strArray = input.split('');
strArray.unshift(strArray.pop())
const output = strArray.reverse().join('');

this.push(output);
next();
};

stdIn.pipe(through(transform)).pipe(stdOut);
}
10 changes: 10 additions & 0 deletions hw3/utils/lib/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const through = require('through2');

module.exports = (stdin, stdout) => {
const transform = function (buffer, encoding, next) {
this.push(buffer.toString().toUpperCase());
next();
}

stdin.pipe(through(transform)).pipe(stdout);
}
44 changes: 44 additions & 0 deletions hw3/utils/streams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const program = require('commander');
const pUtils = require('./lib/path-utils');

program
.version('0.1.0')
.option('-a, --action [value]', 'Provide necessary action name')
.option('-f, --file [value]', 'File name')
.option('-p, --path [value]', 'Folder path')
.parse(process.argv);

try {
let filePath;
switch (program.action) {
case 'reverse':
require('./lib/reverse')(process.stdin, process.stdout);
break;
case 'transform':
require('./lib/transform')(process.stdin, process.stdout);
break;
case 'outputFile':
filePath = pUtils.resolveFilePath(__dirname, program.file);
require('./lib/output-file')(filePath, process.stdout);
break;
case 'convertFromFile':
filePath = pUtils.resolveFilePath(__dirname, program.file);
require('./lib/convert-from-file')(filePath, process.stdout);
break;
case 'convertToFile':
filePath = pUtils.resolveFilePath(__dirname, program.file);
require('./lib/convert-to-file')(filePath);
break;
case 'cssBundler':
const folderPath = pUtils.resolveFolderPath(__dirname, program.path);
require('./lib/css-bundler')(folderPath);
break;
default:
console.error('Wrong input parameters!')
program.help();
break;
}
} catch (error) {
console.error('Something went wrong!')
console.error(`Error: ${error.message || error}`);
}
3 changes: 3 additions & 0 deletions hw3/utils/users.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user_id,first_name,last_name,timestamp
4a5a0930-faac-11e7-8c3f-9a214cf093ae,John,Snow,2017-01-23 03:00:00+03
510c3e42-faac-11e7-8c3f-9a214cf093ae,Sam,Smith,2017-01-23 03:00:00+04
3 changes: 3 additions & 0 deletions hw3/utils/users.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Bob
Jack
Bill
3 changes: 3 additions & 0 deletions hw4/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
Loading