-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75aaf69
commit f7cb50c
Showing
6 changed files
with
91 additions
and
33 deletions.
There are no files selected for viewing
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,22 +1,51 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import fs from 'node:fs/promises' | ||
import path from 'node:path' | ||
|
||
export const loadFile = <T>(path: string, throwError = true): T | undefined => { | ||
if (fs.existsSync(path)) { | ||
return require(path) as T | ||
/** | ||
* Load file | ||
* @param path | ||
* @param throwError | ||
* @returns | ||
*/ | ||
export const loadFile = async <T>( | ||
path: string, | ||
throwError = true | ||
): Promise<T | undefined> => { | ||
// Get path stat | ||
const stat = await fs.stat(path) | ||
|
||
// Import and return if the file exist | ||
if (stat.isFile()) { | ||
return (await import(path)).default | ||
} | ||
|
||
// Handle error | ||
if (throwError) { | ||
new Error(`${path} does not exist.`) | ||
throw new Error(`${path} does not exist.`) | ||
} | ||
} | ||
|
||
export const exportFile = (filePath: string, content: string): void => { | ||
/** | ||
* Export file | ||
* @param filePath | ||
* @param content | ||
* @returns | ||
*/ | ||
export const exportFile = async ( | ||
filePath: string, | ||
content: string | ||
): Promise<any> => { | ||
// Target folder | ||
const folder = path.dirname(filePath) | ||
if (!fs.existsSync(folder)) { | ||
fs.mkdirSync(folder) | ||
|
||
// Get file stat | ||
const stat = await fs.stat(folder) | ||
|
||
// Create folder if folder not exist | ||
if (!stat.isDirectory()) { | ||
await fs.mkdir(folder) | ||
} | ||
|
||
fs.writeFileSync(filePath, content) | ||
return fs.writeFile(filePath, content) | ||
} |
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