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
106 changes: 106 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Packsge
package-lock.json
30 changes: 27 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,41 @@
"description": "This repository is the part of nodejs-assignments https://github.com/AlreadyBored/nodejs-assignments",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"fs:create": "node ./src/fs/create.js",
"fs:copy": "node ./src/fs/copy.js",
"fs:rename": "node ./src/fs/rename.js",
"fs:delete": "node ./src/fs/delete.js",
"fs:list": "node ./src/fs/list.js",
"fs:read": "node ./src/fs/read.js",
"cli:env": "set RSS_name1=value1& set RSS_name2=value2& node ./src/cli/env.js",
"cli:args": "node ./src/cli/args.js --propName value --prop2Name value2",
"modules:cjsToEsm": "node ./src/modules/cjsToEsm.mjs",
"hash:calcHash": "node ./src/hash/calcHash.js",
"streams:read": "node ./src/streams/read.js",
"streams:write": "node ./src/streams/write.js",
"streams:transform": "node ./src/streams/transform.js",
"zip:compress": "node ./src/zip/compress.js",
"zip:decompress": "node ./src/zip/decompress.js",
"wt:main": "node ./src/wt/main.js",
"cp:cp": "node ./src/cp/cp.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/AlreadyBored/node-nodejs-basics.git"
},
"keywords": ["nodejs", "assignments", "alreadybored"],
"keywords": [
"nodejs",
"assignments",
"alreadybored"
],
"author": "alreadybored",
"license": "ISC",
"bugs": {
"url": "https://github.com/AlreadyBored/node-nodejs-basics/issues"
},
"homepage": "https://github.com/AlreadyBored/node-nodejs-basics#readme"
"homepage": "https://github.com/AlreadyBored/node-nodejs-basics#readme",
"devDependencies": {
"@types/node": "^16.11.38"
}
}
17 changes: 15 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import { argv } from "process";

export const parseArgs = () => {
// Write your code here
};
const arrayKey = []
argv.slice(2).forEach((value, i, arr) => {
if (value.indexOf('--') == 0) {
arrayKey.push(`${value.slice(2)} is ${arr[i + 1]}`);
}
});
console.log(arrayKey.join(', '));
};

//npm run cli:args
//or
//node ./src/cli/args.js --propName value --prop2Name value2
parseArgs();
17 changes: 15 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import { env } from "process";

export const parseEnv = () => {
// Write your code here
};
const arrayKey = [];
Object.entries(env).forEach((property) => {
if (property[0].indexOf('RSS_') == 0) {
arrayKey.push(`${property[0]}=${property[1]}`);
};
});
console.log(arrayKey.join('; '));
};

//npm run cli:env
//or
//RSS_name1=value1 RSS_name2=value2 node ./src/cli/env.js
parseEnv();
25 changes: 23 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import { spawn } from "child_process";
import { join } from "path";
import { fileURLToPath, URL } from "url";

const __dirname = fileURLToPath(new URL('.', import.meta.url));

export const spawnChildProcess = async (args) => {
// Write your code here
};
const child = spawn('node', ['script.js', ...args], {
cwd: join(__dirname, 'files'),
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
});
child.stdout.on('data', (chunk) => {
process.stdout.write(chunk.toString());
});
process.stdin.on('data', (chunk) => {
child.stdin.write(chunk.toString());
})
child.on('close', () => {
process.exit();
})
};

//npm run cp:cp
spawnChildProcess([1, 2, 3, 4, 5]);
38 changes: 36 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
import { copyFile, mkdir, readdir } from "fs/promises";
import { join } from "path";
import { fileURLToPath, URL } from "url";

export const copy = async () => {
// Write your code here
};
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const pathToSourseDir = join(__dirname, 'files');
const pathToDestinationDir = join(__dirname, 'files_copy');

const copyElements = async (pathToSourseDir, pathToDestinationDir) => {
await mkdir(pathToDestinationDir).catch(() => {
throw new Error('FS operation failed');
});
const elements = await readdir(pathToSourseDir, {withFileTypes: true}).then((dirents) => {
const elements = {files: [], directory: []};
dirents.forEach((dirent) => dirent.isFile() ? elements.files.push(dirent) : elements.directory.push(dirent));
return elements;
}).catch(() => {
throw new Error('FS operation failed');
});
for (const file of elements.files) {
const pathToSourseFile = join(pathToSourseDir, file.name);
const pathToDestinationFile = join(pathToDestinationDir, file.name);
await copyFile(pathToSourseFile, pathToDestinationFile);
};
for (const directory of elements.directory) {
const pathToSourseDirectory = join(pathToSourseDir, directory.name);
const pathToDestinationDirectory = join(pathToDestinationDir, directory.name);
await copyElements(pathToSourseDirectory, pathToDestinationDirectory);
}
}

await copyElements(pathToSourseDir, pathToDestinationDir);
};

//npm run fs:copy
copy();
21 changes: 19 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import { writeFile } from "fs/promises";
import { join } from "path";
import { fileURLToPath, URL } from "url";

export const create = async () => {
// Write your code here
};
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const pathToFile = join(__dirname, 'files', 'fresh.txt');
const contentFile = 'I am fresh and young';

await writeFile(pathToFile, contentFile, {
flag: 'wx'
}).then(() => {
console.log('file created successfully');
}).catch(() => {
throw new Error('FS operation failed');
});
};

//npm run fs:create
create();
15 changes: 13 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { rm } from "fs/promises";
import { join } from "path";
import { fileURLToPath, URL } from "url";

export const remove = async () => {
// Write your code here
};
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const pathToFile = join(__dirname, 'files', 'fileToRemove.txt');
await rm(pathToFile).catch(() => {
throw new Error('FS operation failed');
})
};

//npm run fs:delete
remove();
19 changes: 17 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import { readdir } from "fs/promises";
import { join } from "path";
import { fileURLToPath, URL } from "url";

export const list = async () => {
// Write your code here
};
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const pathToDir = join(__dirname, 'files');
await readdir(pathToDir, {withFileTypes: true}).then((dirents) => {
dirents.forEach((dirent) => {
if (dirent.isFile()) console.log(dirent.name);
})
}).catch(() => {
throw new Error('FS operation failed');
});
};

//npm run fs:list
list();
17 changes: 15 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import { readFile } from "fs/promises";
import { join } from "path";
import { fileURLToPath, URL } from "url";

export const read = async () => {
// Write your code here
};
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const pathToFile = join(__dirname, 'files', 'fileToRead.txt');
await readFile(pathToFile, 'utf-8').then((content) => {
console.log(content);
}).catch(() => {
throw new Error('FS operation failed');
})
};

//npm run fs:read
read();
16 changes: 14 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import * as fsp from "fs/promises";
import { join } from "path";
import { fileURLToPath, URL } from "url";

export const rename = async () => {
// Write your code here
};
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const pathToOldFile = join(__dirname, 'files', 'wrongFilename.txt');
const pathToNewFile = join(__dirname, 'files', 'wrongFilename.md');
await fsp.rename(pathToOldFile, pathToNewFile).catch(() => {
throw new Error('FS operation failed');
});
};

//npm run fs:rename
rename();
Loading