-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathrename.js
More file actions
32 lines (27 loc) · 762 Bytes
/
rename.js
File metadata and controls
32 lines (27 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { access } from "fs/promises";
import { constants } from "fs/promises";
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rename = async () => {
try {
const isTargetExist = await access(
path.resolve(__dirname, "files/properFilename.md"),
constants.F_OK
)
.then(() => true)
.catch(() => false);
if (isTargetExist) {
throw new Error();
}
await fs.rename(
path.resolve(__dirname, "files/wrongFilename.txt"),
path.resolve(__dirname, "files/properFilename.md")
);
} catch {
throw new Error("FS operation failed");
}
};
await rename();