Skip to content

Commit

Permalink
fix: add sync function for create file and options must be provided c…
Browse files Browse the repository at this point in the history
…reate folder
  • Loading branch information
Aiden-FE committed Mar 20, 2024
1 parent 15e814e commit 6175e93
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
33 changes: 31 additions & 2 deletions src/node-modules/create-file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path';
import { writeFileSync } from 'fs';
import { writeFile, writeFileSync } from 'fs';

/**
* @description 创建文件
Expand All @@ -10,7 +10,36 @@ import { writeFileSync } from 'fs';
* @param options.encoding 编码格式, 默认utf8
* @category Files
*/
export default function createFile(
export function createFile(
filePath: string,
fileData: string | NodeJS.ArrayBufferView,
options?: {
cwd?: string;
encoding?: BufferEncoding;
},
): Promise<void> {
return new Promise((res, rej) => {
const target = resolve(options?.cwd || process.cwd(), filePath);
writeFile(target, fileData, { encoding: options?.encoding || 'utf-8' }, (err) => {
if (err) {
rej(err);
return;
}
res();
});
});
}

/**
* @description 创建文件
* @param filePath 文件路径
* @param fileData 文件数据
* @param options 配置项
* @param options.cwd 执行路径, 默认process.cwd()
* @param options.encoding 编码格式, 默认utf8
* @category Files
*/
export function createFileSync(
filePath: string,
fileData: string | NodeJS.ArrayBufferView,
options?: {
Expand Down
6 changes: 3 additions & 3 deletions src/node-modules/create-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { stat, mkdirSync } from 'fs';
*/
export default function createFolder(
targetPath: string,
options: {
options?: {
cwd?: string;
},
) {
Expand All @@ -19,12 +19,12 @@ export default function createFolder(
...options,
};
const target = resolve(cwd, targetPath);
return new Promise<true>((res) => {
return new Promise<void>((res) => {
stat(target, (err, stats) => {
if (err || !stats) {
mkdirSync(target, { recursive: true });
}
res(true);
res();
});
});
}
5 changes: 3 additions & 2 deletions src/node-modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import createFolder from './create-folder';
import createFile from './create-file';
import isCommandExists from './is-command-exists';
import isFileOrFolderExists from './is-file-or-folder-exists';
import requireModule from './require-module';
import scanDependencyManager from './scan-dependency-manager';

export { createFolder, createFile, isCommandExists, isFileOrFolderExists, requireModule, scanDependencyManager };
export * from './create-file';

export { createFolder, isCommandExists, isFileOrFolderExists, requireModule, scanDependencyManager };

0 comments on commit 6175e93

Please sign in to comment.