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
10 changes: 10 additions & 0 deletions node_modules/.yarn-integrity

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

18 changes: 1 addition & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,7 @@
},
"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",
"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"
"start": "node src/index.mjs"
},
"repository": {
"type": "git",
Expand Down
5 changes: 0 additions & 5 deletions src/cli/args.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/cli/args.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const getValueByCLIArgs = (findName = '--') => {
const fullArg = process.argv
.slice(2)
.find(a => a.startsWith(`${findName}=`));

if (!fullArg) return undefined;

const idx = fullArg.indexOf('=');
return idx >= 0 ? fullArg.slice(idx + 1) : undefined;
};
5 changes: 0 additions & 5 deletions src/cli/env.js

This file was deleted.

69 changes: 69 additions & 0 deletions src/cli/path.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import fs from 'node:fs/promises'
import path from 'node:path'

import { DATA_TYPES } from '../constants/index.mjs'

const normalizeSeparators = (inputPath = '') => {
if (!inputPath) return inputPath;

if (process.platform === 'win32') {
return inputPath.replace(/\//g, path.win32.sep);
}

return inputPath.replace(/\\/g, path.posix.sep);
};


export const isPathExist = async (pathForCheck) => {
try {
const normalized = normalizeSeparators(pathForCheck);
await fs.stat(normalized);
} catch(err) {
return false;
}
return true;
}

export const getLastNameFromPath = (inputPath) => {
const normalized = normalizeSeparators(inputPath);
const arrOfArgs = normalized.split(path.sep);
return arrOfArgs[arrOfArgs.length - 1];
};


export const getNewPathFromInput = async (inputPath, pathToCurrentDir, isNewPath) => {
if (!inputPath) throw new Error('Path is required');

const normalizedTarget = normalizeSeparators(inputPath);
const normalizedBase = normalizeSeparators(pathToCurrentDir);

const newPath = path.isAbsolute(normalizedTarget)
? path.resolve(normalizedTarget)
: path.resolve(normalizedBase, normalizedTarget);

if (isNewPath) {
if (await isPathExist(newPath)) throw new Error(`${newPath} already exists.`);
} else {
await fs.stat(newPath);
}

return newPath;
};


export const getUpPath = (currentPath) => {
const normalized = normalizeSeparators(currentPath);
const { root } = path.parse(normalized);
if (normalized === root) return root;
return path.dirname(normalized);
};

export const getElTypeFromPath = async (pathToEl) => {
const normalized = normalizeSeparators(pathToEl);
const stat = await fs.lstat(normalized);
const isDir = stat.isDirectory();
if (isDir) return DATA_TYPES.directory;
const isFile = stat.isFile();
if (isFile) return DATA_TYPES.file;
return undefined;
};
37 changes: 37 additions & 0 deletions src/constants/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const FLAG_CONSTANTS = {
USERNAME_FLAG: '--username',
};

export const COMMAND_CONSTANTS = {
exit: '.exit',
up: 'up',
cd: 'cd',
ls: 'ls',
cat: 'cat',
add: 'add',
mkdir: 'mkdir',
rn: 'rn',
cp: 'cp',
mv: 'mv',
rm: 'rm',
os: 'os',
hash: 'hash',
compress: 'compress',
decompress: 'decompress',
};

export const OPERATING_SYSTEM_CONSTANTS = {
EOL: '--EOL',
cpus: '--cpus',
homedir: '--homedir',
username: '--username',
architecture: '--architecture',
};

export const OPERATION_FAILED_ERROR_TEXT_MESSAGE = 'Operation failed';
export const FS_OPERATION_FAILED_ERROR_TEXT_MESSAGE = 'FS operation failed';

export const DATA_TYPES = {
file: 'file',
directory: 'directory',
}
6 changes: 0 additions & 6 deletions src/cp/cp.js

This file was deleted.

19 changes: 0 additions & 19 deletions src/cp/files/script.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/copy.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/create.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/fs/create.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fs from 'node:fs/promises';

import { FS_OPERATION_FAILED_ERROR_TEXT_MESSAGE } from '../constants/index.mjs';
const createFile = async (pathToFile) => {
try {
await fs.access(pathToFile, fs.constants.F_OK);
}
catch (err) {
await fs.writeFile(pathToFile, '');
process.stdout.write('File created.\n');
return;
}
throw new Error(FS_OPERATION_FAILED_ERROR_TEXT_MESSAGE);
};

const createDirectory = async (pathToDirectory) => {
try {
await fs.access(pathToDirectory, fs.constants.F_OK);
}
catch (err) {
await fs.mkdir(pathToDirectory, { recursive: true });
process.stdout.write('Directory created.\n');
return;
}
throw new Error(FS_OPERATION_FAILED_ERROR_TEXT_MESSAGE);
}

export {
createFile,
createDirectory
}
5 changes: 0 additions & 5 deletions src/fs/delete.js

This file was deleted.

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

const deleteFile = async (pathToDeleteFile, skipMessage) => {
await unlink(pathToDeleteFile);
if (!skipMessage) process.stdout.write(`${pathToDeleteFile} file deleted.\n`)
};

export default deleteFile;
1 change: 0 additions & 1 deletion src/fs/files/dontLookAtMe.txt

This file was deleted.

7 changes: 0 additions & 7 deletions src/fs/files/fileToRead.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/fs/files/hello.txt

This file was deleted.

3 changes: 0 additions & 3 deletions src/fs/files/wrongFilename.txt

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/list.js

This file was deleted.

54 changes: 54 additions & 0 deletions src/fs/list.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from 'node:fs/promises';
import path from 'node:path';

import { getElTypeFromPath } from '../cli/path.mjs';
import { FS_OPERATION_FAILED_ERROR_TEXT_MESSAGE, DATA_TYPES } from '../constants/index.mjs';

export const getList = async (pathToDir) => {
try {
const files = await fs.readdir(pathToDir, {encoding: 'utf-8'});
return files;
}
catch(err) {
throw new Error(FS_OPERATION_FAILED_ERROR_TEXT_MESSAGE);
}
};

export const printTable = async (pathToCurrentDir) => {
const list = await getList(pathToCurrentDir);

if (list.length === 0) {
console.log('Directory is empty.');
return;
}

class Item {
constructor(name, type) {
this.Name = name
this.Type = type
}
}

const pairsList = await Promise.all(
list.map(async (el) => {
const pathToEl = pathToCurrentDir + path.sep + el;
const typeOfFile = await getElTypeFromPath(pathToEl);

return { name: el, type: typeOfFile };
})
);

const dirFirstSortedList = pairsList
.filter((pair) => pair.type)
.sort((a, b) => {
if (a.type === b.type) return a.name.localeCompare(b.name);
return a.type === DATA_TYPES.directory ? -1 : 1;
});

const table = dirFirstSortedList.reduce((acc, cur, idx) => {
acc[idx] = new Item(cur.name, cur.type);
return acc;
}, {});

console.table(table);
};
5 changes: 0 additions & 5 deletions src/fs/read.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/fs/rename.js

This file was deleted.

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

import { isPathExist ,getElTypeFromPath } from '../cli/path.mjs';
import { DATA_TYPES } from '../constants/index.mjs';

const rename = async (pathToRename, newPath) => {
if (await isPathExist(pathToRename)
&& !await isPathExist(newPath)
&& await getElTypeFromPath(pathToRename) === DATA_TYPES.file) {
await fs.rename(pathToRename, newPath);
process.stdout.write('Renamed.\n');
} else {
throw new Error ('Can not rename.');
}
};

export default rename;
5 changes: 0 additions & 5 deletions src/hash/calcHash.js

This file was deleted.

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

const calculateHash = async (pathToFile) => {
const hash = createHash('sha256');

const output = new Writable({
write(chunk, encoding, callback) {
hash.update(chunk);
callback();
}
});

await pipeline(
createReadStream(pathToFile),
output
);

process.stdout.write(hash.digest('hex') + '\n');
};

export default calculateHash;
Loading