Skip to content

Commit

Permalink
feat(node-fs): writeFile under asyncQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD committed Jan 8, 2024
1 parent 69da5b7 commit 6d8b3d7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {AsyncQueue} from '@alwatr/async-queue';
import {definePackage} from '@alwatr/logger';

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

export const logger = definePackage('@alwatr/node-fs', __package_version__);

export const asyncQueue = new AsyncQueue();
2 changes: 1 addition & 1 deletion packages/node-fs/src/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {logger} from './logger';
import {logger} from './common';

/**
* Parse json string.
Expand Down
2 changes: 1 addition & 1 deletion packages/node-fs/src/make-file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {open} from 'node:fs/promises';

import {logger} from './logger.js';
import {logger} from './common';

/**
* Make empty file.
Expand Down
2 changes: 1 addition & 1 deletion packages/node-fs/src/read-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {readFile as readFile_} from 'node:fs/promises';

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

import {logger} from './common';
import {parseJson} from './json';
import {logger} from './logger';

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

Expand Down
40 changes: 21 additions & 19 deletions packages/node-fs/src/write-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {writeFileSync as writeFileSync_, existsSync, mkdirSync, renameSync} from
import {mkdir, rename, writeFile as writeFile_} from 'node:fs/promises';
import {dirname} from 'node:path';

import {logger} from './logger';
import {asyncQueue, logger} from './common';

/**
* Enhanced write file (Synchronous).
Expand Down Expand Up @@ -57,26 +57,28 @@ export function writeFileSync(path: string, content: string): void {
* writeFile('./file.txt', 'Hello World!');
* ```
*/
export async function writeFile(path: string, content: string): Promise<void> {
export function writeFile(path: string, content: string): Promise<void> {
logger.logMethodArgs?.('writeFile', '...' + path.slice(-32));
try {
logger.logOther?.('writeFile start', '...' + path.slice(-32));
const pathExists = existsSync(path);
if (!pathExists) {
const dir = dirname(path);
if (!existsSync(dir)) {
await mkdir(dir, {recursive: true});
return asyncQueue.push(path, async () => {
try {
logger.logOther?.('writeFile start', '...' + path.slice(-32));
const pathExists = existsSync(path);
if (!pathExists) {
const dir = dirname(path);
if (!existsSync(dir)) {
await mkdir(dir, {recursive: true});
}
}
await writeFile_(path + '.tmp', content, {encoding: 'utf-8', flag: 'w'});
if (pathExists) {
await rename(path, path + '.bak');
}
await rename(path + '.tmp', path);
logger.logOther?.('writeFile success', '...' + path.slice(-32));
}
await writeFile_(path + '.tmp', content, {encoding: 'utf-8', flag: 'w'});
if (pathExists) {
await rename(path, path + '.bak');
catch (err) {
logger.error('writeFile', 'write_file_failed', {path}, err);
throw new Error('write_file_failed', {cause: (err as Error).cause});
}
await rename(path + '.tmp', path);
logger.logOther?.('writeFile success', '...' + path.slice(-32));
}
catch (err) {
logger.error('writeFile', 'write_file_failed', {path}, err);
throw new Error('write_file_failed', {cause: (err as Error).cause});
}
});
}
4 changes: 2 additions & 2 deletions packages/node-fs/src/write-json.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {flatString} from '@alwatr/flat-string';

import {logger} from './common';
import {jsonStringify} from './json';
import {logger} from './logger';
import {writeFile, writeFileSync} from './write-file.js';
import {writeFile, writeFileSync} from './write-file';

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

Expand Down

0 comments on commit 6d8b3d7

Please sign in to comment.