From 6175e935044b15ad068d1fc374eb084237865c6c Mon Sep 17 00:00:00 2001 From: Aiden-FE Date: Wed, 20 Mar 2024 19:13:30 +0800 Subject: [PATCH] fix: add sync function for create file and options must be provided create folder --- src/node-modules/create-file.ts | 33 +++++++++++++++++++++++++++++-- src/node-modules/create-folder.ts | 6 +++--- src/node-modules/index.ts | 5 +++-- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/node-modules/create-file.ts b/src/node-modules/create-file.ts index ff3b194..4fa3103 100644 --- a/src/node-modules/create-file.ts +++ b/src/node-modules/create-file.ts @@ -1,5 +1,5 @@ import { resolve } from 'path'; -import { writeFileSync } from 'fs'; +import { writeFile, writeFileSync } from 'fs'; /** * @description 创建文件 @@ -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 { + 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?: { diff --git a/src/node-modules/create-folder.ts b/src/node-modules/create-folder.ts index 45aaa0b..c8af216 100644 --- a/src/node-modules/create-folder.ts +++ b/src/node-modules/create-folder.ts @@ -10,7 +10,7 @@ import { stat, mkdirSync } from 'fs'; */ export default function createFolder( targetPath: string, - options: { + options?: { cwd?: string; }, ) { @@ -19,12 +19,12 @@ export default function createFolder( ...options, }; const target = resolve(cwd, targetPath); - return new Promise((res) => { + return new Promise((res) => { stat(target, (err, stats) => { if (err || !stats) { mkdirSync(target, { recursive: true }); } - res(true); + res(); }); }); } diff --git a/src/node-modules/index.ts b/src/node-modules/index.ts index b16df23..4eada0d 100644 --- a/src/node-modules/index.ts +++ b/src/node-modules/index.ts @@ -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 };