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
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
},
"type": "module",
"scripts": {
"cli:args": "node src/cli/args.js --some-arg value1 --other 1337 --arg2 42",
"cli:env": "npx cross-env SOME=any RSS_foo=bar RSS_bar=baz node src/cli/env.js",
"cp": "node src/cp/cp.js",
"fs:copy": "node src/fs/copy.js",
"fs:create": "node src/fs/create.js",
"fs:delete": "node src/fs/delete.js",
"fs:list": "node src/fs/list.js",
"fs:read": "node src/fs/read.js",
"fs:rename": "node src/fs/rename.js",
"hash": "node src/hash/calcHash.js",
"cli:args": "node src/cli/args.mjs --some-arg value1 --other 1337 --arg2 42",
"cli:env": "npx cross-env SOME=any RSS_foo=bar RSS_bar=baz node src/cli/env.mjs",
"cp": "node src/cp/cp.mjs",
"fs:copy": "node src/fs/copy.mjs",
"fs:create": "node src/fs/create.mjs",
"fs:delete": "node src/fs/delete.mjs",
"fs:list": "node src/fs/list.mjs",
"fs:read": "node src/fs/read.mjs",
"fs:rename": "node src/fs/rename.mjs",
"hash": "node src/hash/calcHash.mjs",
"modules": "node src/modules/esm.mjs",
"streams:read": "node src/streams/read.js",
"streams:transform": "node src/streams/transform.js",
"streams:write": "node src/streams/write.js",
"wt": "node src/wt/main.js",
"zip:compress": "node src/zip/compress.js",
"zip:decompress": "node src/zip/decompress.js"
"streams:read": "node src/streams/read.mjs",
"streams:transform": "node src/streams/transform.mjs",
"streams:write": "node src/streams/write.mjs",
"wt": "node src/wt/main.mjs",
"zip:compress": "node src/zip/compress.mjs",
"zip:decompress": "node src/zip/decompress.mjs"
},
"repository": {
"type": "git",
Expand Down
5 changes: 0 additions & 5 deletions src/cli/args.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/cli/args.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const FIRST_ARGUMENT_INDEX = 2;
const VARIABLE_PREFIX = '--';

const isVariable = (argument) => argument.startsWith(VARIABLE_PREFIX);

const getVariableValueFromArguments = (args, index) => {
if ((args.length <= index + 1) || isVariable(args[index + 1])) return '';
return args[index + 1];
}

const parseArgs = () => {
const args = process.argv.slice(FIRST_ARGUMENT_INDEX);
const result = [];

args.forEach((argument, index) => {
if (isVariable(argument)) result.push(`${argument} is ${getVariableValueFromArguments(args, index)}`);
});

console.log(result.join(', '));
};

parseArgs();
5 changes: 0 additions & 5 deletions src/cli/env.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/cli/env.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const VARIABLE_RSS_PREFIX = 'RSS_';
const RESULT_STRING_DELIMITER = '; ';

const isRSSEnvVariable = (envVariableName) => envVariableName.startsWith(VARIABLE_RSS_PREFIX);
const getEnvVariablePairString = (envVariableName, envVariablesObject) => `${envVariableName}=${envVariablesObject[envVariableName]}`;

const parseEnv = () => {
const envRSSVariablesPairsStrings = [];
const envVariablesObject = process.env;

for (let envVariableName in envVariablesObject) {
if (isRSSEnvVariable(envVariableName)) envRSSVariablesPairsStrings.push(getEnvVariablePairString(envVariableName, envVariablesObject));
}

console.log(envRSSVariablesPairsStrings.join(RESULT_STRING_DELIMITER));
};

parseEnv();
1 change: 1 addition & 0 deletions src/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FILES_DIR_NAME = 'files';
3 changes: 3 additions & 0 deletions src/common/helper.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { join } from 'path';

export const getPath = (dirname, subDirName, fileName = '') => join(dirname, subDirName, fileName);
6 changes: 0 additions & 6 deletions src/cp/cp.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/cp/cp.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { fork as childProcessFork } from 'child_process';

import { getPath } from '../common/helper.mjs';
import { FILES_DIR_NAME } from '../common/constants.mjs';

const SCRIPT_FILE_PATH = 'script.js';

const spawnChildProcess = (args) => {
const scriptPath = getPath(import.meta.dirname, FILES_DIR_NAME, SCRIPT_FILE_PATH);
childProcessFork(scriptPath, args, { stdio: [0, 1, 2, 'ipc'] });
};

spawnChildProcess(['first', 'the second', 3, [4, 4, 4, 4]]);
5 changes: 5 additions & 0 deletions src/fs/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FILES_DIR_NAME } from '../../common/constants.mjs';

export const ERROR_MESSAGE = 'FS operation failed';

export { FILES_DIR_NAME };
17 changes: 17 additions & 0 deletions src/fs/common/helpers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { access as fsPromisesAccess } from 'fs/promises';

import { getPath } from '../../common/helper.mjs';
import { FILES_DIR_NAME } from './constants.mjs';

export const getFilePath = (dirname, fileName) => getPath(dirname, FILES_DIR_NAME, fileName);

export const isFileExist = async (filePath) => {
try {
await fsPromisesAccess(filePath);
return true;
} catch {
return false;
}
}

export { getPath };
5 changes: 0 additions & 5 deletions src/fs/copy.js

This file was deleted.

28 changes: 28 additions & 0 deletions src/fs/copy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { cp as fsPromisesCopy } from 'fs/promises';

import { getPath } from './common/helpers.mjs';
import { ERROR_MESSAGE, FILES_DIR_NAME } from './common/constants.mjs';

const DESTINATION_FOLDER_NAME = 'files_copy';

const copy = async () => {
const currentDirectoryPath = import.meta.dirname;

const filesFolderPath = getPath(currentDirectoryPath, FILES_DIR_NAME);
const destinationFolderPath = getPath(currentDirectoryPath, DESTINATION_FOLDER_NAME);

try {
await fsPromisesCopy(
filesFolderPath,
destinationFolderPath,
{
errorOnExist: true,
force: false,
recursive: true
});
} catch {
throw new Error(ERROR_MESSAGE);
}
};

await copy();
5 changes: 0 additions & 5 deletions src/fs/create.js

This file was deleted.

19 changes: 19 additions & 0 deletions src/fs/create.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { writeFile as fsPromisesWriteFile } from 'fs/promises';

import { getFilePath } from './common/helpers.mjs';
import { ERROR_MESSAGE } from './common/constants.mjs';

const CONTENT_TO_WRITE = 'I am fresh and young';
const FILE_NAME = 'fresh.txt';

const create = async () => {
const outputFilePath = getFilePath(import.meta.dirname, FILE_NAME);

try {
await fsPromisesWriteFile(outputFilePath, CONTENT_TO_WRITE, { flag: 'wx' });
} catch {
throw new Error(ERROR_MESSAGE);
}
};

await create();
5 changes: 0 additions & 5 deletions src/fs/delete.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/fs/delete.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { rm as fsPromisesRemove } from 'fs/promises';

import { getFilePath } from './common/helpers.mjs';
import { ERROR_MESSAGE } from './common/constants.mjs';

const FILE_NAME = 'fileToRemove.txt';

const remove = async () => {
const filePath = getFilePath(import.meta.dirname, FILE_NAME);

try {
await fsPromisesRemove(filePath);
} catch {
throw new Error(ERROR_MESSAGE);
}
};

await remove();
5 changes: 0 additions & 5 deletions src/fs/list.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/fs/list.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { readdir as fsPromisesReadDir } from 'fs/promises';

import { getPath } from './common/helpers.mjs';
import { ERROR_MESSAGE, FILES_DIR_NAME } from './common/constants.mjs';

const list = async () => {
const filesPath = getPath(import.meta.dirname, FILES_DIR_NAME);

try {
const dirContent = await fsPromisesReadDir(filesPath, { recursive: true });
console.log(dirContent);
} catch {
throw new Error(ERROR_MESSAGE);
}
};

await list();
5 changes: 0 additions & 5 deletions src/fs/read.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/fs/read.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { readFile as fsPromisesReadFile } from 'fs/promises';

import { getFilePath } from './common/helpers.mjs';
import { ERROR_MESSAGE } from './common/constants.mjs';

const FILE_NAME = 'fileToRead.txt';
const ENCODING_UTF8 = 'utf-8';

const read = async () => {
const filePath = getFilePath(import.meta.dirname, FILE_NAME);

try {
const fileContent = await fsPromisesReadFile(filePath, ENCODING_UTF8);
console.log(fileContent);
} catch {
throw new Error(ERROR_MESSAGE);
}
};

await read();
5 changes: 0 additions & 5 deletions src/fs/rename.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/fs/rename.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { rename as fsPromisesRename } from 'fs/promises';

import { getFilePath, isFileExist } from './common/helpers.mjs';
import { ERROR_MESSAGE } from './common/constants.mjs';

const FILE_NAMES = {
WRONG: 'wrongFilename.txt',
PROPER: 'properFilename.md'
};

const rename = async () => {
const wrongFilePath = getFilePath(import.meta.dirname, FILE_NAMES.WRONG);
const properFilePath = getFilePath(import.meta.dirname, FILE_NAMES.PROPER);

try {
if (await isFileExist(properFilePath)) throw new Error();
await fsPromisesRename(wrongFilePath, properFilePath);
} catch {
throw new Error(ERROR_MESSAGE);
}
};

await rename();
5 changes: 0 additions & 5 deletions src/hash/calcHash.js

This file was deleted.

38 changes: 38 additions & 0 deletions src/hash/calcHash.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Writable } from 'stream';
import { pipeline as streamPromisesPipeline } from 'stream/promises';
import { open as fsPromisesOpen } from 'fs/promises';
import { createHash as cryptoCreateHash } from 'crypto';

import { getPath } from '../common/helper.mjs';
import { FILES_DIR_NAME } from '../common/constants.mjs';

const HASH_TYPE = 'sha256';
const INPUT_FILE_PATH = 'fileToCalculateHashFor.txt';

class ConsoleLoggerStream extends Writable {
constructor() {
super();
}

_write(chunk) {
console.log(chunk.toString('hex'));
}
}

const consoleLoggerStream = new ConsoleLoggerStream();

const calculateHash = async () => {
const inputFilePath = getPath(import.meta.dirname, FILES_DIR_NAME, INPUT_FILE_PATH);
const fd = await fsPromisesOpen(inputFilePath);
const inputStream = fd.createReadStream();

const calculateHashTransformStream = cryptoCreateHash(HASH_TYPE);

await streamPromisesPipeline(
inputStream,
calculateHashTransformStream,
consoleLoggerStream
);
};

calculateHash();
Loading