8000 fs, refactor: add writeFile() method options. · fibjs/fibjs@7af243e · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 7af243e

Browse files
committed
fs, refactor: add writeFile() method options.
1 parent ff40f87 commit 7af243e

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

fibjs/include/ifs/fs.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class fs_base : public object_base {
7575
static result_t write(FileHandle_base* fd, exlib::string string, int32_t position, exlib::string encoding, int32_t& retVal, AsyncEvent* ac);
7676
static result_t writeTextFile(exlib::string fname, exlib::string txt, AsyncEvent* ac);
7777
static result_t writeFile(exlib::string fname, Buffer_base* data, AsyncEvent* ac);
78+
static result_t writeFile(exlib::string fname, exlib::string data, exlib::string opt, AsyncEvent* ac);
79+
static result_t writeFile(exlib::string fname, exlib::string data, v8::Local<v8::Object> options, AsyncEvent* ac);
7880
static result_t appendFile(exlib::string fname, Buffer_base* data, AsyncEvent* ac);
7981
static result_t setZipFS(exlib::string fname, Buffer_base* data);
8082
static result_t clearZipFS(exlib::string fname);
@@ -178,6 +180,8 @@ class fs_base : public object_base {
178180
ASYNC_STATICVALUE5(fs_base, write, FileHandle_base*, exlib::string, int32_t, exlib::string, int32_t);
179181
ASYNC_STATIC2(fs_base, writeTextFile, exlib::string, exlib::string);
180182
ASYNC_STATIC2(fs_base, writeFile, exlib::string, Buffer_base*);
183+
ASYNC_STATIC3(fs_base, writeFile, exlib::string, exlib::string, exlib::string);
184+
ASYNC_STATIC3(fs_base, writeFile, exlib::string, exlib::string, v8::Local<v8::Object>);
181185
ASYNC_STATIC2(fs_base, appendFile, exlib::string, Buffer_base*);
182186
};
183187
}
@@ -954,6 +958,28 @@ inline void fs_base::s_static_writeFile(const v8::FunctionCallbackInfo<v8::Value
954958
else
955959
hr = ac_writeFile(v0, v1);
956960

961+
ASYNC_METHOD_OVER(3, 2);
962+
963+
ARG(exlib::string, 0);
964+
ARG(exlib::string, 1);
965+
OPT_ARG(exlib::string, 2, "utf8");
966+
967+
if (!cb.IsEmpty())
968+
hr = acb_writeFile(v0, v1, v2, cb, args);
969+
else
970+
hr = ac_writeFile(v0, v1, v2);
971+
972+
ASYNC_METHOD_OVER(3, 3);
973+
974+
ARG(exlib::string, 0);
975+
ARG(exlib::string, 1);
976+
ARG(v8::Local<v8::Object>, 2);
977+
978+
if (!cb.IsEmpty())
979+
hr = acb_writeFile(v0, v1, v2, cb, args);
980+
else
981+
hr = ac_writeFile(v0, v1, v2);
982+
957983
METHOD_VOID();
958984
}
959985

fibjs/src/fs/fs.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "File.h"
1919
#include "AsyncUV.h"
2020
#include "utils.h"
21+
#include "encoding.h"
2122

2223
namespace fibjs {
2324

@@ -247,6 +248,38 @@ result_t fs_base::writeFile(exlib::string fname, Buffer_base* data, AsyncEvent*
247248
return hr;
248249
}
249250

251+
result_t fs_base::writeFile(exlib::string fname, exlib::string data, exlib::string opt, AsyncEvent* ac)
252+
{
253+
if (ac->isSync())
254+
return CHECK_ERROR(CALL_E_NOSYNC);
255+
256+
result_t hr = commonEncode(opt, data, data);
257+
if (hr < 0)
258+
return hr;
259+
260+
return writeTextFile(fname, data, ac);
261+
}
262+
263+
result_t fs_base::writeFile(exlib::string fname, exlib::string data, v8::Local<v8::Object> options, AsyncEvent* ac)
264+
{
265+
if (ac->isSync()) {
266+
Isolate* isolate = Isolate::current(options);
267+
result_t hr;
268+
269+
ac->m_ctx.resize(1);
270+
271+
exlib::string encoding = "utf8";
272+
hr = GetConfigValue(isolate, options, "encoding", encoding, true);
273+
if (hr < 0 && hr != CALL_E_PARAMNOTOPTIONAL)
274+
return hr;
275+
ac->m_ctx[0] = encoding;
276+
277+
return CHECK_ERROR(CALL_E_NOSYNC);
278+
}
279+
280+
return writeFile(fname, data, ac->m_ctx[0].string(), ac);
281+
}
282+
250283
result_t fs_base::appendFile(exlib::string fname, Buffer_base* data, AsyncEvent* ac)
251284
{
252285
if (ac->isSync())

idl/zh-cn/fs.idl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,28 @@ module fs
357357
*/
358358
static writeFile(String fname, Buffer data) async;
359359

360+
/*! @brief 创建文件,并写入内容
361+
@param fname 指定文件名
362+
@param data 指定要写入的数据
363+
@param opt 指定写入选项
364+
*/
365+
static writeFile(String fname, String data, String opt = "utf8") async;
366+
367+
/*! @brief 创建文件,并写入内容
368+
369+
options 支持的选项如下:
370+
```JavaScript
371+
{
372+
"encoding": "utf8" // specify the encoding, default is utf8.
373+
}
374+
```
375+
376+
@param fname 指定文件名
377+
@param data 指定要写入的数据
378+
@param options 指定写入选项
379+
*/
380+
static writeFile(String fname, String data, Object options) async;
381+
360382
/*! @brief 创建二进制文件,并写入内容
361383
@param fname 指定文件名
362384
@param data 指定要写入的二进制数据

npm/types/dts/module/fs.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,36 @@ declare module 'fs' {
529529

530530
function writeFile(fname: string, data: Class_Buffer, callback: (err: Error | undefined | null)=>any): void;
531531

532+
/**
533+
* @description 创建文件,并写入内容
534+
* @param fname 指定文件名
535+
* @param data 指定要写入的数据
536+
* @param opt 指定写入选项
537+
*
538+
*/
539+
function writeFile(fname: string, data: string, opt?: string): void;
540+
541+
function writeFile(fname: string, data: string, opt?: string, callback?: (err: Error | undefined | null)=>any): void;
542+
543+
/**
544+
* @description 创建文件,并写入内容
545+
*
546+
* options 支持的选项如下:
547+
* ```JavaScript
548+
* {
549+
* "encoding": "utf8" // specify the encoding, default is utf8.
550+
* }
551+
* ```
552+
*
553+
* @param fname 指定文件名
554+
* @param data 指定要写入的数据
555+
* @param options 指定写入选项
556+
*
557+
*/
558+
function writeFile(fname: string, data: string, options: FIBJS.GeneralObject): void;
559+
560+
function writeFile(fname: string, data: string, options: FIBJS.GeneralObject, callback: (err: Error | undefined | null)=>any): void;
561+
532562
/**
533563
* @description 创建二进制文件,并写入内容
534564
* @param fname 指定文件名

test/fs_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,15 @@ describe('fs', () => {
883883

884884
fs.writeFile(fn, 'data to be written');
885885
assert.equal(fs.readFile(fn), 'data to be written');
886+
887+
fs.writeFile(fn, 'data to be written', "utf8");
888+
assert.equal(fs.readFile(fn), 'data to be written');
889+
890+
fs.writeFile(fn, 'data to be written', {
891+
encoding: "utf8"
892+
});
893+
assert.equal(fs.readFile(fn), 'data to be written');
894+
886895
fs.appendFile(fn, 'data to be appended');
887896
assert.equal(fs.readFile(fn), 'data to be writtendata to be appended');
888897

0 commit comments

Comments
 (0)
0