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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["asar"]
}
7 changes: 7 additions & 0 deletions node_modules/.package-lock.json

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

17 changes: 17 additions & 0 deletions package-lock.json

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

19 changes: 16 additions & 3 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
const PREFIX = '--'

const parseArgs = () => {
// Write your code here
};
const formatArgumentsArr = process.argv
.slice(2)
.reduce((acc, value, index, array) => {
if (value.startsWith(PREFIX)) {
const argument = `${value.replace(PREFIX, '')} is ${array[index + 1]}`
return [...acc, argument]
}
return acc
}, [])

const formatArgumentsStr = formatArgumentsArr.join(', ')
console.log(formatArgumentsStr)
}

parseArgs();
parseArgs()
22 changes: 19 additions & 3 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
const PREFIX = 'RSS_'

const parseEnv = () => {
// Write your code here
};
const envVariables = Object.entries(process.env).reduce(
(acc, value, index, array) => {
const [nameVar, valueVar] = value

if (nameVar.startsWith(PREFIX)) {
const variable = `${nameVar}=${valueVar}`
return [...acc, variable]
}
return acc
},
[]
)

const strFormatVariables = envVariables.join(', ')
console.log(strFormatVariables)
}

parseEnv();
parseEnv()
14 changes: 9 additions & 5 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const spawnChildProcess = async (args) => {
// Write your code here
};
import path from 'node:path'
import { fork } from 'node:child_process'

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
const pathFileFork = path.resolve(import.meta.dirname, 'files/script.js')

const spawnChildProcess = async args => {
fork(pathFileFork, args)
}

spawnChildProcess(['someArgument1', 'someArgument2'])
26 changes: 13 additions & 13 deletions src/cp/files/script.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { EOL } from 'node:os';
import { argv, stdout, stdin, exit } from 'node:process';
import { EOL } from 'node:os'
import { argv, stdout, stdin, exit } from 'node:process'

const args = argv.slice(2);
const args = argv.slice(2)

console.log(`Total number of arguments is ${args.length}`);
console.log(`Arguments: ${JSON.stringify(args)}${EOL}`);
console.log(`Total number of arguments is ${args.length}`)
console.log(`Arguments: ${JSON.stringify(args)}${EOL}`)

const echoInput = (chunk) => {
const chunkStringified = chunk.toString();
const echoInput = chunk => {
const chunkStringified = chunk.toString()

if (chunkStringified.includes('CLOSE')) {
exit(0);
}
if (chunkStringified.includes('CLOSE')) {
exit(0)
}

stdout.write(`Received from master process: ${chunk.toString()}${EOL}`);
};
stdout.write(`Received from master process: ${chunk.toString()}${EOL}`)
}

stdin.on('data', echoInput);
stdin.on('data', echoInput)
25 changes: 22 additions & 3 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { cp } from 'fs/promises'
import path from 'path'
import { isFileExists } from '../../utils.js'

const srcFolderPath = path.resolve(import.meta.dirname, 'files')
const destFolderPath = path.resolve(import.meta.dirname, 'files_copy')
const isSrcFolderPathExists = await isFileExists(srcFolderPath)

const copy = async () => {
// Write your code here
};
if (!isSrcFolderPathExists) {
throw new Error('FS operation failed')
} else {
await cp(srcFolderPath, destFolderPath, {
recursive: true,
errorOnExist: true,
force: false,
}).catch(error => {
console.log(error)
throw new Error('FS operation failed')
})
}
}

await copy();
await copy()
16 changes: 13 additions & 3 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { writeFile } from 'fs/promises'
import path from 'path'

const fileUrl = path.resolve(import.meta.dirname, 'files/fresh.txt')

const create = async () => {
// Write your code here
};
try {
await writeFile(fileUrl, 'I am fresh and young', { flag: 'wx' })
} catch (err) {
console.log(err)
throw new Error('FS operation failed')
}
}

await create();
await create()
16 changes: 13 additions & 3 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { rm } from 'fs/promises'
import path from 'path'

const filePath = path.resolve(import.meta.dirname, 'files/fileToRemove.txt')

const remove = async () => {
// Write your code here
};
try {
await rm(filePath)
} catch (error) {
console.log(error)
throw new Error('FS operation failed')
}
}

await remove();
await remove()
1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
How dare you!
1 change: 1 addition & 0 deletions src/fs/files/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
File renamed without changes.
17 changes: 14 additions & 3 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { readdir } from 'fs/promises'
import path from 'path'

const folderPath = path.resolve(import.meta.dirname, 'files')

const list = async () => {
// Write your code here
};
try {
const listFiles = await readdir(folderPath)
console.log(listFiles)
} catch (error) {
console.log(error)
throw new Error('FS operation failed')
}
}

await list();
await list()
17 changes: 14 additions & 3 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { promises as fs } from 'fs'
import path from 'path'

const filePath = path.resolve(import.meta.dirname, 'files/fileToRead.txt')

const read = async () => {
// Write your code here
};
try {
const fileContent = await fs.readFile(filePath, 'utf-8')
console.log(fileContent)
} catch (error) {
console.log(error)
throw new Error('FS operation failed')
}
}

await read();
await read()
19 changes: 16 additions & 3 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { rename } from 'fs/promises'
import path from 'path'

import { isFileExists } from '../../utils.js'

const oldFileName = path.resolve(import.meta.dirname, 'wrongFilename.txt')
const newFileName = path.resolve(import.meta.dirname, 'properFilename.md')
const isNewFileNameExists = await isFileExists(newFileName)

const rename = async () => {
// Write your code here
};
if (isNewFileNameExists) {
throw new Error('FS operation failed')
} else {
await rename(oldFileName, newFileName)
}
}

await rename();
await rename()
17 changes: 14 additions & 3 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { readFile } from 'fs/promises'
import { createHash } from 'crypto'
import path from 'path'

const filePath = path.resolve(
import.meta.dirname,
'files/fileToCalculateHashFor.txt'
)

const calculateHash = async () => {
// Write your code here
};
const textFromFile = await readFile(filePath)
const hash = createHash('sha256').update(textFromFile)
console.log(hash.digest('hex'))
}

await calculateHash();
await calculateHash()
34 changes: 0 additions & 34 deletions src/modules/cjsToEsm.cjs

This file was deleted.

36 changes: 36 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'path'
import { release, version } from 'node:os'
import { createServer as createServerHttp } from 'node:http'
import { fileURLToPath } from 'node:url'

import './files/c.cjs'

const random = Math.random()

const unknownObject = await (random > 0.5
? import('./files/a.json', { with: { type: 'json' } })
: import('./files/b.json', { with: { type: 'json' } }))

console.log(`Release ${release()}`)
console.log(`Version ${version()}`)
console.log(`Path segment separator is "${path.sep}"`)

const fileName = fileURLToPath(import.meta.url)
const dirName = path.dirname(fileName)
console.log(`Path to current file is ${fileName}`)
console.log(`Path to current directory is ${dirName}`)

const myServer = createServerHttp((_, res) => {
res.end('Request accepted')
})

const PORT = 3000

console.log(unknownObject)

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`)
console.log('To terminate it, use Ctrl+C combination')
})

export { unknownObject, myServer }
12 changes: 12 additions & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
rfgdfgdfgfd
gdf
gfd
gfd
g
fdg
fd
g
fdg



12 changes: 9 additions & 3 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createReadStream } from 'fs'
import path from 'path'

const filePath = path.resolve(import.meta.dirname, 'files/fileToRead.txt')

const read = async () => {
// Write your code here
};
const readStream = createReadStream(filePath, 'utf8')
readStream.pipe(process.stdout)
}

await read();
await read()
Loading