Skip to content

Commit

Permalink
feat(node): add node adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Aug 29, 2024
1 parent a0a8b5d commit b3bf0e0
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 6 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ It also requires the following permission.
- `--allow-read`(path to binary cache, target directories and files)
- `--allow-write`(path to binary cache, target files)

#### Node

// TODO

#### Bun

// TODO
Expand Down
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"exports": {
".": "./src/mod.ts",
"./deno": "./src/deno/mod.ts"
"./deno": "./src/deno/mod.ts",
"./node": "./src/node/mod.ts"
},
"exclude": ["CHANGELOG.md"],
"version": "0.1.0-placeholder"
Expand Down
90 changes: 89 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions src/node/adaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { BucketFileSystem } from "@miyauci/fs/node";
import { FileDialog } from "@miyauci/rfd/node";
import { parse } from "node:path";
import { writeFileSync } from "node:fs";
import type {
Adaptor,
FileLocation,
OpenDirectoryPicker,
OpenFileDialog,
OpenFileDialogOptions,
OpenSaveFilePickerOptions,
Options,
} from "../implementation_defined.ts";

export class NodeAdaptor implements Adaptor {
openFileDialog: OpenFileDialog = openFileDialog;
openDirectoryDialog: OpenDirectoryPicker = openDirectoryDialog;

locateEntry = BucketFileSystem.prototype.locateEntry;
openSaveFileDialog = openSaveFileDialog;
}

export function openFileDialog(
options: OpenFileDialogOptions,
): FileLocation[] | null {
const fileDialog = new FileDialog();

if (options.startingDirectory) {
fileDialog.setDirectory(options.startingDirectory);
}

for (const [description, exts] of options.acceptsOptions) {
const extsWithoutDot = exts.map((ext) =>
ext.startsWith(".") ? ext.slice(1) : ext
);

fileDialog.addFilter(description, extsWithoutDot);
}

if (options?.multiple) {
const paths = fileDialog.pickFiles();

if (!paths) return null;

return paths.map(toLoc);
}

const fullPath = fileDialog.pickFile();

if (!fullPath) return null;

return [toLoc(fullPath)];
}

export function openSaveFileDialog(
options: OpenSaveFilePickerOptions,
): FileLocation | null {
const fileDialog = new FileDialog();

if (options.startingDirectory) {
fileDialog.setDirectory(options.startingDirectory);
}

if (options?.suggestedName) {
fileDialog.setFileName(options.suggestedName);
}

for (const [description, exts] of options.acceptsOptions) {
const extsWithoutDot = exts.map((ext) =>
ext.startsWith(".") ? ext.slice(1) : ext
);

fileDialog.addFilter(description, extsWithoutDot);
}

const path = fileDialog.saveFile();

if (!path) return null;

writeFileSync(path, new Uint8Array());

return toLoc(path);
}

function toLoc(path: string): FileLocation {
const { base: name, dir: root } = parse(path);

return { name, root };
}

export function openDirectoryDialog(
options: Options,
): FileLocation | null {
const fileDialog = new FileDialog();

if (options.startingDirectory) {
fileDialog.setDirectory(options.startingDirectory);
}

const fullPath = fileDialog.pickDirectory();

if (!fullPath) return null;

return toLoc(fullPath);
}
1 change: 1 addition & 0 deletions src/node/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { NodeAdaptor } from "./adaptor.ts";

0 comments on commit b3bf0e0

Please sign in to comment.