feat: chrome native compatible fs api

This commit is contained in:
Tienson Qin
2020-11-23 17:10:29 +08:00
parent 3a81b5e856
commit 50a7a7c585
7 changed files with 152 additions and 82 deletions

View File

@@ -74,13 +74,13 @@ export var getSelectionText = function() {
// Modified from https://github.com/GoogleChromeLabs/browser-nativefs
// because shadow-cljs doesn't handle this babel transform
const getFiles = async function (dirHandle, recursive) {
const getFiles = async function (dirHandle, recursive, cb, path = dirHandle.name) {
const dirs = [];
const files = [];
const path = dirHandle.name;
for await (const entry of dirHandle.values()) {
const nestedPath = `${path}/${entry.name}`;
if (entry.kind === 'file') {
cb(nestedPath, entry);
files.push(
entry.getFile().then((file) => {
Object.defineProperty(file, 'webkitRelativePath', {
@@ -98,17 +98,18 @@ const getFiles = async function (dirHandle, recursive) {
)
);
} else if (entry.kind === 'directory' && recursive) {
dirs.push(getFiles(entry, recursive, nestedPath));
cb(nestedPath, entry);
dirs.push(getFiles(entry, recursive, cb, nestedPath));
}
}
return [(await Promise.all(dirs)), (await Promise.all(files))];
};
export var openDirectory = async function (options = {}) {
export var openDirectory = async function (options = {}, cb) {
options.recursive = options.recursive || false;
const handle = await window.showDirectoryPicker({ mode: 'readwrite' });
return [handle, getFiles(handle, options.recursive)];
return [handle, getFiles(handle, options.recursive, cb)];
};
export var writeFile = async function (fileHandle, contents) {