Skip to content

Commit 6d8b3d7

Browse files
committed
feat(node-fs): writeFile under asyncQueue
1 parent 69da5b7 commit 6d8b3d7

File tree

6 files changed

+29
-24
lines changed

6 files changed

+29
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import {AsyncQueue} from '@alwatr/async-queue';
12
import {definePackage} from '@alwatr/logger';
23

34
import type {} from '@alwatr/nano-build';
45

56
export const logger = definePackage('@alwatr/node-fs', __package_version__);
7+
8+
export const asyncQueue = new AsyncQueue();

packages/node-fs/src/json.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {logger} from './logger';
1+
import {logger} from './common';
22

33
/**
44
* Parse json string.

packages/node-fs/src/make-file.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {open} from 'node:fs/promises';
22

3-
import {logger} from './logger.js';
3+
import {logger} from './common';
44

55
/**
66
* Make empty file.

packages/node-fs/src/read-file.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {readFile as readFile_} from 'node:fs/promises';
33

44
import {flatString} from '@alwatr/flat-string';
55

6+
import {logger} from './common';
67
import {parseJson} from './json';
7-
import {logger} from './logger';
88

99
import type {MaybePromise} from '@alwatr/type-helper';
1010

packages/node-fs/src/write-file.ts

+21-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {writeFileSync as writeFileSync_, existsSync, mkdirSync, renameSync} from
22
import {mkdir, rename, writeFile as writeFile_} from 'node:fs/promises';
33
import {dirname} from 'node:path';
44

5-
import {logger} from './logger';
5+
import {asyncQueue, logger} from './common';
66

77
/**
88
* Enhanced write file (Synchronous).
@@ -57,26 +57,28 @@ export function writeFileSync(path: string, content: string): void {
5757
* writeFile('./file.txt', 'Hello World!');
5858
* ```
5959
*/
60-
export async function writeFile(path: string, content: string): Promise<void> {
60+
export function writeFile(path: string, content: string): Promise<void> {
6161
logger.logMethodArgs?.('writeFile', '...' + path.slice(-32));
62-
try {
63-
logger.logOther?.('writeFile start', '...' + path.slice(-32));
64-
const pathExists = existsSync(path);
65-
if (!pathExists) {
66-
const dir = dirname(path);
67-
if (!existsSync(dir)) {
68-
await mkdir(dir, {recursive: true});
62+
return asyncQueue.push(path, async () => {
63+
try {
64+
logger.logOther?.('writeFile start', '...' + path.slice(-32));
65+
const pathExists = existsSync(path);
66+
if (!pathExists) {
67+
const dir = dirname(path);
68+
if (!existsSync(dir)) {
69+
await mkdir(dir, {recursive: true});
70+
}
71+
}
72+
await writeFile_(path + '.tmp', content, {encoding: 'utf-8', flag: 'w'});
73+
if (pathExists) {
74+
await rename(path, path + '.bak');
6975
}
76+
await rename(path + '.tmp', path);
77+
logger.logOther?.('writeFile success', '...' + path.slice(-32));
7078
}
71-
await writeFile_(path + '.tmp', content, {encoding: 'utf-8', flag: 'w'});
72-
if (pathExists) {
73-
await rename(path, path + '.bak');
79+
catch (err) {
80+
logger.error('writeFile', 'write_file_failed', {path}, err);
81+
throw new Error('write_file_failed', {cause: (err as Error).cause});
7482
}
75-
await rename(path + '.tmp', path);
76-
logger.logOther?.('writeFile success', '...' + path.slice(-32));
77-
}
78-
catch (err) {
79-
logger.error('writeFile', 'write_file_failed', {path}, err);
80-
throw new Error('write_file_failed', {cause: (err as Error).cause});
81-
}
83+
});
8284
}

packages/node-fs/src/write-json.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {flatString} from '@alwatr/flat-string';
22

3+
import {logger} from './common';
34
import {jsonStringify} from './json';
4-
import {logger} from './logger';
5-
import {writeFile, writeFileSync} from './write-file.js';
5+
import {writeFile, writeFileSync} from './write-file';
66

77
import type {MaybePromise} from '@alwatr/type-helper';
88

0 commit comments

Comments
 (0)