Skip to content

Commit

Permalink
feat: use import.meta.dirname, released in node v20.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-koran committed Oct 13, 2024
1 parent 073f9b8 commit fb883a9
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 89 deletions.
7 changes: 1 addition & 6 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { fork } from 'node:child_process';
import { dirname } from 'node:path';
import { stdin, stdout } from 'node:process';
import { fileURLToPath } from 'node:url';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const spawnChildProcess = async (args) => {
const controller = new AbortController();

const childProcess = fork(`${currentFolderPath}/files/script.js`, [...args], { silent: true });
const childProcess = fork(`${import.meta.dirname}/files/script.js`, [...args], { silent: true });

stdin.pipe(childProcess.stdin);
childProcess.stdout.pipe(stdout);
Expand Down
7 changes: 1 addition & 6 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { cp } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const copy = async () => {
try {
await cp(`${currentFolderPath}/files`, `${currentFolderPath}/files_copy`, {
await cp(`${import.meta.dirname}/files`, `${import.meta.dirname}/files_copy`, {
errorOnExist: true,
force: false,
recursive: true,
Expand Down
7 changes: 1 addition & 6 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { open } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const fileContent = 'I am fresh and young';

const create = async () => {
let file;

try {
file = await open(`${currentFolderPath}/files/fresh.txt`, 'wx+');
file = await open(`${import.meta.dirname}/files/fresh.txt`, 'wx+');

await file.write(fileContent);

Expand Down
7 changes: 1 addition & 6 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { rm } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const remove = async () => {
try {
await rm(`${currentFolderPath}/files/fileToRemove.txt`);
await rm(`${import.meta.dirname}/files/fileToRemove.txt`);
} catch (error) {
if (error?.code === 'ENOENT') {
throw new Error('FS operation failed');
Expand Down
7 changes: 1 addition & 6 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { readdir } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const list = async () => {
try {
const list = await readdir(`${currentFolderPath}/files`);
const list = await readdir(`${import.meta.dirname}/files`);
console.debug(list);
} catch (error) {
if (error?.code === 'ENOENT') {
Expand Down
7 changes: 1 addition & 6 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { readFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const read = async () => {
try {
const fileContent = await readFile(`${currentFolderPath}/files/fileToRead.txt`, {
const fileContent = await readFile(`${import.meta.dirname}/files/fileToRead.txt`, {
encoding: 'utf8',
});

Expand Down
11 changes: 3 additions & 8 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { rename as fsRename, access, constants } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const rename = async () => {
try {
const properFileExists = await access(
`${currentFolderPath}/files/properFilename.md`,
`${import.meta.dirname}/files/properFilename.md`,
constants.R_OK | constants.W_OK,
);

Expand All @@ -23,8 +18,8 @@ const rename = async () => {

try {
await fsRename(
`${currentFolderPath}/files/wrongFilename.txt`,
`${currentFolderPath}/files/properFilename.md`,
`${import.meta.dirname}/files/wrongFilename.txt`,
`${import.meta.dirname}/files/properFilename.md`,
);
} catch (error) {
if (error?.code === 'ENOENT') {
Expand Down
7 changes: 1 addition & 6 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { createReadStream } from 'node:fs';
import { dirname } from 'node:path';
import { stdout } from 'node:process';
import { fileURLToPath } from 'node:url';

const calculateHash = async () => {
const { createHash } = await import('node:crypto');

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const hash = createHash('sha256');

const input = createReadStream(`${currentFolderPath}/files/fileToCalculateHashFor.txt`);
const input = createReadStream(`${import.meta.dirname}/files/fileToCalculateHashFor.txt`);

input.pipe(hash).setEncoding('hex').pipe(stdout);
};
Expand Down
10 changes: 3 additions & 7 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createServer as createServerHttp } from 'node:http';
import { release, version } from 'node:os';
import { dirname, sep } from 'node:path';
import { fileURLToPath } from 'node:url';
import { sep } from 'node:path';

await import('./files/c.js');

Expand All @@ -19,11 +18,8 @@ console.debug(`Release ${release()}`);
console.debug(`Version ${version()}`);
console.debug(`Path segment separator is "${sep}"`);

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

console.debug(`Path to current file is ${filePath}`);
console.debug(`Path to current directory is ${currentFolderPath}`);
console.debug(`Path to current file is ${import.meta.filename}`);
console.debug(`Path to current directory is ${import.meta.dirname}`);

const myServer = createServerHttp((_, res) => {
res.end('Request accepted');
Expand Down
7 changes: 1 addition & 6 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { createReadStream } from 'node:fs';
import { dirname } from 'node:path';
import { stdout } from 'node:process';
import { fileURLToPath } from 'node:url';

const read = async () => {
const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const input = createReadStream(`${currentFolderPath}/files/fileToRead.txt`);
const input = createReadStream(`${import.meta.dirname}/files/fileToRead.txt`);

input.pipe(stdout);
};
Expand Down
7 changes: 1 addition & 6 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { createWriteStream } from 'node:fs';
import { dirname } from 'node:path';
import { stdin } from 'node:process';
import { fileURLToPath } from 'node:url';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const write = async () => {
const writeableStream = createWriteStream(`${currentFolderPath}/files/fileToWrite.txt`);
const writeableStream = createWriteStream(`${import.meta.dirname}/files/fileToWrite.txt`);

stdin.pipe(writeableStream);
};
Expand Down
7 changes: 1 addition & 6 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { availableParallelism } from 'node:os';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Worker } from 'node:worker_threads';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

let startNCount = 10;

const performCalculations = async () => {
Expand All @@ -14,7 +9,7 @@ const performCalculations = async () => {

for (let i = 0; i < cpuCoresCount; i += 1) {
const workerPromise = new Promise((resolve, reject) => {
const newWorker = new Worker(`${currentFolderPath}/worker.js`, {
const newWorker = new Worker(`${import.meta.dirname}/worker.js`, {
workerData: { n: startNCount + i },
});

Expand Down
9 changes: 2 additions & 7 deletions src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { createReadStream, createWriteStream } from 'node:fs';
import { dirname } from 'node:path';
import { pipeline } from 'node:stream/promises';
import { fileURLToPath } from 'node:url';
import { createGzip } from 'node:zlib';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const compress = async () => {
try {
const gzip = createGzip();

const source = createReadStream(`${currentFolderPath}/files/fileToCompress.txt`);
const destination = createWriteStream(`${currentFolderPath}/files/archive.gz`);
const source = createReadStream(`${import.meta.dirname}/files/fileToCompress.txt`);
const destination = createWriteStream(`${import.meta.dirname}/files/archive.gz`);

await pipeline(source, gzip, destination);
} catch (error) {
Expand Down
9 changes: 2 additions & 7 deletions src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { createReadStream, createWriteStream } from 'node:fs';
import { dirname } from 'node:path';
import { pipeline } from 'node:stream/promises';
import { fileURLToPath } from 'node:url';
import { createUnzip } from 'node:zlib';

const filePath = fileURLToPath(import.meta.url);
const currentFolderPath = dirname(filePath);

const decompress = async () => {
try {
const unGzip = createUnzip();

const source = createReadStream(`${currentFolderPath}/files/archive.gz`);
const destination = createWriteStream(`${currentFolderPath}/files/fileToCompress.txt`);
const source = createReadStream(`${import.meta.dirname}/files/archive.gz`);
const destination = createWriteStream(`${import.meta.dirname}/files/fileToCompress.txt`);

await pipeline(source, unGzip, destination);
} catch (error) {
Expand Down

0 comments on commit fb883a9

Please sign in to comment.