-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use fs.writeFile instead of fs-extra
- Loading branch information
Showing
9 changed files
with
1,231 additions
and
1,327 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { fs } from 'memfs'; | ||
const { vol } = require('memfs'); | ||
|
||
module.exports = vol; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { fs } from 'memfs'; | ||
const { vol } = require('memfs'); | ||
|
||
export default fs.promises; | ||
module.exports = vol.promises; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
import { createReadStream } from 'fs'; | ||
import { writeFile, stat } from 'fs/promises'; | ||
import { parser } from 'stream-json'; | ||
import { chain } from 'stream-chain'; | ||
import Asm from 'stream-json/Assembler'; | ||
|
||
export const readJSONStream = <T = unknown>(filepath: string): Promise<T> => { | ||
const pipeline = chain([createReadStream(filepath), parser()]); | ||
export const readJSONStream = async <T = unknown>(filepath: string): Promise<T> => { | ||
// Check if the file exists and throw error before creating a stream | ||
await stat(filepath); | ||
|
||
const readStream = createReadStream(filepath); | ||
const pipeline = chain([readStream, parser()]); | ||
const asm = Asm.connectTo(pipeline); | ||
|
||
return new Promise((fulfill) => { | ||
asm.on('done', (data) => fulfill(data.current)); | ||
return new Promise((resolve, reject) => { | ||
asm.on('done', (data) => { | ||
if (data.current) { | ||
resolve(data.current); | ||
} else { | ||
reject(new Error('Invalid JSON file')); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
export async function writeJSON(filepath: string, data: Record<string, unknown>): Promise<void> { | ||
return writeFile(filepath, JSON.stringify(data)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters