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
17 changes: 14 additions & 3 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const parseArgs = () => {
// Write your code here
};
let res = ""
for (let i = 2; i < process.argv.length; i++) {
const el = process.argv[i]
if (!el.startsWith("--") || i == process.argv.length - 1) continue

parseArgs();
const elNext = process.argv[i + 1]
if (elNext.startsWith("--")) continue

res += `${el.slice(2)} is ${elNext}, `
i++
}
console.log(res.slice(0, -2))
}

parseArgs()
10 changes: 7 additions & 3 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const parseEnv = () => {
// Write your code here
};
let res = ""
for (let v in process.env) {
if (v.startsWith("RSS_")) res += `${v}=${process.env[v]}; `
}
console.log(res.slice(0, -2))
}

parseEnv();
parseEnv()
29 changes: 25 additions & 4 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
const spawnChildProcess = async (args) => {
// Write your code here
};
import { spawn } from "child_process"
import path from "path"
import { fileURLToPath } from "url"

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const spawnChildProcess = async args => {
const script = path.join(__dirname, "files", "script.js")
const child = spawn("node", [script, ...args])

process.stdin.pipe(child.stdin)

child.on("exit", function (code, signal) {
console.log(`child process exited with code ${code} and signal ${signal}`)
})

child.stdout.on("data", data => {
console.log(`child stdout:\n${data}`)
})

child.stderr.on("data", data => {
console.error(`child stderr:\n${data}`)
})
}

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["a1", "someArgument2", 56])
21 changes: 18 additions & 3 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from "fs/promises"
import path from "path"
import { fileURLToPath } from "url"
import { isPathExist } from "../utils/utils.js"

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const copy = async () => {
// Write your code here
};
const src = path.join(__dirname, "files")
const dest = path.join(__dirname, "files_copy")

try {
if (!(await isPathExist(src)) || (await isPathExist(dest))) throw Error
await fs.cp(src, dest, { recursive: true, errorOnExist: true, force: false })
} catch {
throw new Error("FS operation failed")
}
}

await copy();
await copy()
22 changes: 19 additions & 3 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { fileURLToPath } from "url"
import path from "path"
import fs from "fs/promises"

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const create = async () => {
// Write your code here
};
const folderPath = path.join(__dirname, "files")
const filePath = path.join(folderPath, "fresh.txt")
const fileContents = "I am fresh and young"

try {
await fs.mkdir(folderPath, { recursive: true })
await fs.writeFile(filePath, fileContents, { flag: "ax" })
console.log(`File created: ${filePath}`)
} catch {
throw new Error("FS operation failed")
}
}

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

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const remove = async () => {
// Write your code here
};
const file = path.join(__dirname, "files", "fileToRemove.txt")
try {
await fs.rm(file)
} catch {
throw new Error("FS operation failed")
}
}

await remove();
await remove()
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 fs from "fs/promises"
import path from "path"
import { fileURLToPath } from "url"
import { isPathExist } from "../utils/utils.js"

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const list = async () => {
// Write your code here
};
const src = path.join(__dirname, "files")
if (!(await isPathExist(src))) throw new Error("FS operation failed")

const files = [...(await fs.readdir(src, { withFileTypes: true }))].filter(v => v.isFile()).map(v => v.name)
console.log(files)
}

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 fs from "fs/promises"
import path from "path"
import { fileURLToPath } from "url"
import { isPathExist } from "../utils/utils.js"

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const read = async () => {
// Write your code here
};
const src = path.join(__dirname, "files", "fileToRead.txt")
if (!(await isPathExist(src))) throw new Error("FS operation failed")

const contents = await fs.readFile(src, "utf8")
console.log(contents)
}

await read();
await read()
21 changes: 18 additions & 3 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from "fs/promises"
import path from "path"
import { fileURLToPath } from "url"
import { isPathExist } from "../utils/utils.js"

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const rename = async () => {
// Write your code here
};
const src = path.join(__dirname, "files", "wrongFilename.txt")
const dest = path.join(__dirname, "files", "properFilename.md")

try {
if (!(await isPathExist(src)) || (await isPathExist(dest))) throw Error
await fs.rename(src, dest)
} catch {
throw new Error("FS operation failed")
}
}

await rename();
await rename()
21 changes: 18 additions & 3 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import crypto from "crypto"
import { createReadStream } from "fs"
import path from "path"
import { fileURLToPath } from "url"

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const calculateHash = async () => {
// Write your code here
};
const src = path.join(__dirname, "files", "fileToCalculateHashFor.txt")
const hash = crypto.createHash("sha256")

const input = createReadStream(src)
input.on("readable", () => {
const data = input.read()
if (data) hash.update(data)
else console.log(hash.digest("hex"))
})
}

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

This file was deleted.

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

await import("./files/c.js")

const random = Math.random()
let unknownObject
if (random > 0.5) {
unknownObject = (await import("./files/a.json", { assert: { type: "json" } })).default
} else {
unknownObject = (await import("./files/b.json", { assert: { type: "json" } })).default
}

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

const __filename = path.dirname(import.meta.url)
const __dirname = fileURLToPath(__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 }
13 changes: 10 additions & 3 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from "fs"
import path from "path"
import { fileURLToPath } from "url"

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const read = async () => {
// Write your code here
};
const src = path.join(__dirname, "files", "fileToRead.txt")
fs.createReadStream(src, "utf-8").pipe(process.stdout)
}

await read();
await read()
15 changes: 12 additions & 3 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { stdin, stdout } from "process"
import { Transform } from "stream"

const transform = async () => {
// Write your code here
};
const revers = new Transform({
transform(chunk, _encoding, callback) {
callback(null, chunk.toString().slice(0, -2).split("").reverse().join("") + "\n")
},
})

stdin.pipe(revers).pipe(stdout)
}

await transform();
await transform()
13 changes: 10 additions & 3 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from "fs"
import path from "path"
import { fileURLToPath } from "url"

const __dirname = fileURLToPath(path.dirname(import.meta.url))

const write = async () => {
// Write your code here
};
const src = path.join(__dirname, "files", "fileToWrite.txt")
process.stdin.pipe(fs.createWriteStream(src, "utf-8"))
}

await write();
await write()
10 changes: 10 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fs from "fs/promises"

export const isPathExist = async path => {
try {
await fs.access(path)
return true
} catch {
return false
}
}
Loading