-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcreate.js
More file actions
26 lines (22 loc) · 695 Bytes
/
create.js
File metadata and controls
26 lines (22 loc) · 695 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
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const create = async () => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const dir = path.join(__dirname, 'files');
const filePath = path.join(dir, 'fresh.txt');
const content = 'I am fresh and young inside of the files folder';
try {
await fs.access(filePath);
throw new Error('FS operation failed');
} catch (err) {
if (err?.message === 'FS operation failed') throw err;
}
try {
await fs.writeFile(filePath, content, 'utf8');
} catch {
throw new Error('FS operation failed');
}
};
await create();