Skip to content

Commit

Permalink
Add Switch.rename() function
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Sep 30, 2024
1 parent 12ca89e commit 6a3bff6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-points-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nx.js/runtime": patch
---

Add `Switch.rename()` function
1 change: 1 addition & 0 deletions packages/runtime/src/$.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export interface Init {
readFileSync(path: string): ArrayBuffer | null;
remove(path: string): Promise<void>;
removeSync(path: string): void;
rename(path: string, dest: string): Promise<void>;
renameSync(path: string, dest: string): void;
stat(path: string): Promise<Stats | null>;
statSync(path: string): Stats | null;
Expand Down
18 changes: 14 additions & 4 deletions packages/runtime/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ export function writeFileSync(path: PathLike, data: string | BufferSource) {
return $.writeFileSync(pathToString(path), ab);
}

/**
* Removes the file or directory recursively specified by `path`.
*
* @param path File path to remove.
*/
export function remove(path: PathLike) {
return $.remove(pathToString(path));
}

/**
* Synchronously removes the file or directory recursively specified by `path`.
*
Expand All @@ -96,12 +105,13 @@ export function removeSync(path: PathLike) {
}

/**
* Removes the file or directory recursively specified by `path`.
* Renames a file or directory.
*
* @param path File path to remove.
* @param path Source file path to rename.
* @param dest Destination file path to rename to.
*/
export function remove(path: PathLike) {
return $.remove(pathToString(path));
export function rename(path: PathLike, dest: PathLike) {
return $.rename(pathToString(path), pathToString(dest));
}

/**
Expand Down
58 changes: 51 additions & 7 deletions source/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ typedef struct {
const char *filename;
} nx_fs_remove_async_t;

typedef struct {
int err;
const char *src;
const char *dest;
} nx_fs_rename_async_t;

char *dirname(const char *path) {
if (path == NULL || *path == '\0') {
return strdup("."); // Current directory for empty paths
Expand Down Expand Up @@ -686,6 +692,43 @@ JSValue nx_remove_sync(JSContext *ctx, JSValueConst this_val, int argc,
return JS_UNDEFINED;
}

void nx_rename_do(nx_work_t *req) {
nx_fs_rename_async_t *data = (nx_fs_rename_async_t *)req->data;
if (rename(data->src, data->dest) != 0) {
data->err = errno;
}
}

JSValue nx_rename_cb(JSContext *ctx, nx_work_t *req) {
nx_fs_rename_async_t *data = (nx_fs_rename_async_t *)req->data;
JS_FreeCString(ctx, data->src);
JS_FreeCString(ctx, data->dest);

if (data->err) {
JSValue err = JS_NewError(ctx);
JS_DefinePropertyValueStr(ctx, err, "message",
JS_NewString(ctx, strerror(data->err)),
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
return JS_Throw(ctx, err);
}

return JS_NULL;
}

JSValue nx_rename(JSContext *ctx, JSValueConst this_val, int argc,
JSValueConst *argv) {
NX_INIT_WORK_T(nx_fs_rename_async_t);
data->src = JS_ToCString(ctx, argv[0]);
if (!data->src) {
return JS_EXCEPTION;
}
data->dest = JS_ToCString(ctx, argv[1]);
if (!data->dest) {
return JS_EXCEPTION;
}
return nx_queue_async(ctx, req, nx_rename_do, nx_rename_cb);
}

JSValue nx_rename_sync(JSContext *ctx, JSValueConst this_val, int argc,
JSValueConst *argv) {
const char *old_path = JS_ToCString(ctx, argv[0]);
Expand Down Expand Up @@ -739,18 +782,19 @@ JSValue nx_fs_create_big_file(JSContext *ctx, JSValueConst this_val, int argc,

static const JSCFunctionListEntry function_list[] = {
JS_CFUNC_DEF("fclose", 1, nx_fclose),
JS_CFUNC_DEF("fopen", 1, nx_fopen),
JS_CFUNC_DEF("fread", 1, nx_fread),
JS_CFUNC_DEF("fwrite", 1, nx_fwrite),
JS_CFUNC_DEF("fopen", 2, nx_fopen),
JS_CFUNC_DEF("fread", 2, nx_fread),
JS_CFUNC_DEF("fwrite", 2, nx_fwrite),
JS_CFUNC_DEF("fsCreateBigFile", 1, nx_fs_create_big_file),
JS_CFUNC_DEF("mkdirSync", 1, nx_mkdir_sync),
JS_CFUNC_DEF("mkdirSync", 2, nx_mkdir_sync),
JS_CFUNC_DEF("readDirSync", 1, nx_readdir_sync),
JS_CFUNC_DEF("readFile", 2, nx_read_file),
JS_CFUNC_DEF("readFile", 1, nx_read_file),
JS_CFUNC_DEF("readFileSync", 1, nx_read_file_sync),
JS_CFUNC_DEF("remove", 2, nx_remove),
JS_CFUNC_DEF("remove", 1, nx_remove),
JS_CFUNC_DEF("removeSync", 1, nx_remove_sync),
JS_CFUNC_DEF("rename", 2, nx_rename),
JS_CFUNC_DEF("renameSync", 2, nx_rename_sync),
JS_CFUNC_DEF("stat", 2, nx_stat),
JS_CFUNC_DEF("stat", 1, nx_stat),
JS_CFUNC_DEF("statSync", 1, nx_stat_sync),
JS_CFUNC_DEF("writeFileSync", 1, nx_write_file_sync),
};
Expand Down

0 comments on commit 6a3bff6

Please sign in to comment.