Skip to content

Commit

Permalink
Fix: add recursive option to directory APIs (#1141)
Browse files Browse the repository at this point in the history
* Add recursive option

* Fix ESLint

* Fix all other possible code style issues

* Add .changes file
  • Loading branch information
ravenclaw900 authored Jan 12, 2021
1 parent 0753877 commit 2fd1067
Show file tree
Hide file tree
Showing 14 changed files with 3,226 additions and 821 deletions.
5 changes: 5 additions & 0 deletions .changes/bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri.js": patch
---

Fixed a TypeScript issue where it didn't allow you to put the "recursive" option in the directory functions.
17 changes: 14 additions & 3 deletions cli/tauri.js/api-src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export interface FsOptions {
dir?: BaseDirectory
}

export interface FsDirOptions {
dir?: BaseDirectory
recursive?: boolean
}

export interface FsTextFileOption {
path: string
contents: string
Expand Down Expand Up @@ -184,7 +189,7 @@ async function writeBinaryFile(
*/
async function readDir(
dir: string,
options: FsOptions = {}
options: FsDirOptions = {}
): Promise<FileEntry[]> {
return await promisified({
cmd: 'readDir',
Expand All @@ -204,7 +209,10 @@ async function readDir(
* @param [options.dir] base directory
* @return
*/
async function createDir(dir: string, options: FsOptions = {}): Promise<void> {
async function createDir(
dir: string,
options: FsDirOptions = {}
): Promise<void> {
return await promisified({
cmd: 'createDir',
path: dir,
Expand All @@ -222,7 +230,10 @@ async function createDir(dir: string, options: FsOptions = {}): Promise<void> {
* @param [options.dir] base directory
* @return
*/
async function removeDir(dir: string, options: FsOptions = {}): Promise<void> {
async function removeDir(
dir: string,
options: FsDirOptions = {}
): Promise<void> {
return await promisified({
cmd: 'removeDir',
path: dir,
Expand Down
9 changes: 5 additions & 4 deletions tauri/examples/communication/dist/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
document.getElementById('cli-matches').addEventListener('click', function () {
window.__TAURI__.cli.getMatches()
document.getElementById("cli-matches").addEventListener("click", function () {
window.__TAURI__.cli
.getMatches()
.then(registerResponse)
.catch(registerResponse)
})
.catch(registerResponse);
});
41 changes: 22 additions & 19 deletions tauri/examples/communication/dist/communication.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
document.getElementById('log').addEventListener('click', function () {
document.getElementById("log").addEventListener("click", function () {
window.__TAURI__.tauri.invoke({
cmd: 'logOperation',
event: 'tauri-click',
payload: 'this payload is optional because we used Option in Rust'
})
})
cmd: "logOperation",
event: "tauri-click",
payload: "this payload is optional because we used Option in Rust",
});
});

document.getElementById('request').addEventListener('click', function () {
window.__TAURI__.tauri.promisified({
cmd: 'performRequest',
endpoint: 'dummy endpoint arg',
body: {
id: 5,
name: 'test'
}
}).then(registerResponse).catch(registerResponse)
})
document.getElementById("request").addEventListener("click", function () {
window.__TAURI__.tauri
.promisified({
cmd: "performRequest",
endpoint: "dummy endpoint arg",
body: {
id: 5,
name: "test",
},
})
.then(registerResponse)
.catch(registerResponse);
});

document.getElementById('event').addEventListener('click', function () {
window.__TAURI__.event.emit('js-event', 'this is the payload string')
})
document.getElementById("event").addEventListener("click", function () {
window.__TAURI__.event.emit("js-event", "this is the payload string");
});
81 changes: 45 additions & 36 deletions tauri/examples/communication/dist/dialog.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
var defaultPathInput = document.getElementById('dialog-default-path')
var filterInput = document.getElementById('dialog-filter')
var multipleInput = document.getElementById('dialog-multiple')
var directoryInput = document.getElementById('dialog-directory')
var defaultPathInput = document.getElementById("dialog-default-path");
var filterInput = document.getElementById("dialog-filter");
var multipleInput = document.getElementById("dialog-multiple");
var directoryInput = document.getElementById("dialog-directory");

document.getElementById('open-dialog').addEventListener('click', function () {
window.__TAURI__.dialog.open({
defaultPath: defaultPathInput.value || null,
filter: filterInput.value || null,
multiple: multipleInput.checked,
directory: directoryInput.checked
}).then(function (res) {
console.log(res)
var pathToRead = res
var isFile = pathToRead.match(/\S+\.\S+$/g)
window.__TAURI__.fs.readBinaryFile(pathToRead).then(function (response) {
if (isFile) {
if (pathToRead.includes('.png') || pathToRead.includes('.jpg')) {
arrayBufferToBase64(new Uint8Array(response), function (base64) {
var src = 'data:image/png;base64,' + base64
registerResponse('<img src="' + src + '"></img>')
})
} else {
registerResponse(res)
}
} else {
registerResponse(res)
}
}).catch(registerResponse(res))
}).catch(registerResponse)
})
document.getElementById("open-dialog").addEventListener("click", function () {
window.__TAURI__.dialog
.open({
defaultPath: defaultPathInput.value || null,
filter: filterInput.value || null,
multiple: multipleInput.checked,
directory: directoryInput.checked,
})
.then(function (res) {
console.log(res);
var pathToRead = res;
var isFile = pathToRead.match(/\S+\.\S+$/g);
window.__TAURI__.fs
.readBinaryFile(pathToRead)
.then(function (response) {
if (isFile) {
if (pathToRead.includes(".png") || pathToRead.includes(".jpg")) {
arrayBufferToBase64(new Uint8Array(response), function (base64) {
var src = "data:image/png;base64," + base64;
registerResponse('<img src="' + src + '"></img>');
});
} else {
registerResponse(res);
}
} else {
registerResponse(res);
}
})
.catch(registerResponse(res));
})
.catch(registerResponse);
});

document.getElementById('save-dialog').addEventListener('click', function () {
window.__TAURI__.dialog.save({
defaultPath: defaultPathInput.value || null,
filter: filterInput.value || null
}).then(registerResponse).catch(registerResponse)
})
document.getElementById("save-dialog").addEventListener("click", function () {
window.__TAURI__.dialog
.save({
defaultPath: defaultPathInput.value || null,
filter: filterInput.value || null,
})
.then(registerResponse)
.catch(registerResponse);
});
91 changes: 50 additions & 41 deletions tauri/examples/communication/dist/fs.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,66 @@
var dirSelect = document.getElementById('dir')
var dirSelect = document.getElementById("dir");

function getDir() {
return dirSelect.value ? parseInt(dir.value) : null
return dirSelect.value ? parseInt(dir.value) : null;
}

function arrayBufferToBase64(buffer, callback) {
var blob = new Blob([buffer], {
type: 'application/octet-binary'
})
var reader = new FileReader()
type: "application/octet-binary",
});
var reader = new FileReader();
reader.onload = function (evt) {
var dataurl = evt.target.result
callback(dataurl.substr(dataurl.indexOf(',') + 1))
}
reader.readAsDataURL(blob)
var dataurl = evt.target.result;
callback(dataurl.substr(dataurl.indexOf(",") + 1));
};
reader.readAsDataURL(blob);
}

var pathInput = document.getElementById('path-to-read')
var pathInput = document.getElementById("path-to-read");

addClickEnterHandler(
document.getElementById('read'),
pathInput,
function () {
var pathToRead = pathInput.value
var isFile = pathToRead.match(/\S+\.\S+$/g)
var opts = {
dir: getDir()
}
var promise = isFile ? window.__TAURI__.fs.readBinaryFile(pathToRead, opts) : window.__TAURI__.fs.readDir(pathToRead, opts)
promise.then(function (response) {
addClickEnterHandler(document.getElementById("read"), pathInput, function () {
var pathToRead = pathInput.value;
var isFile = pathToRead.match(/\S+\.\S+$/g);
var opts = {
dir: getDir(),
};
var promise = isFile
? window.__TAURI__.fs.readBinaryFile(pathToRead, opts)
: window.__TAURI__.fs.readDir(pathToRead, opts);
promise
.then(function (response) {
if (isFile) {
if (pathToRead.includes('.png') || pathToRead.includes('.jpg')) {
if (pathToRead.includes(".png") || pathToRead.includes(".jpg")) {
arrayBufferToBase64(new Uint8Array(response), function (base64) {
var src = 'data:image/png;base64,' + base64
registerResponse('<img src="' + src + '"></img>')
})
var src = "data:image/png;base64," + base64;
registerResponse('<img src="' + src + '"></img>');
});
} else {
var value = String.fromCharCode.apply(null, response)
registerResponse('<textarea id="file-response" style="height: 400px"></textarea><button id="file-save">Save</button>')
var fileInput = document.getElementById('file-response')
fileInput.value = value
document.getElementById('file-save').addEventListener('click', function () {
window.__TAURI__.fs.writeFile({
file: pathToRead,
contents: fileInput.value
}, {
dir: getDir()
}).catch(registerResponse)
})
var value = String.fromCharCode.apply(null, response);
registerResponse(
'<textarea id="file-response" style="height: 400px"></textarea><button id="file-save">Save</button>'
);
var fileInput = document.getElementById("file-response");
fileInput.value = value;
document
.getElementById("file-save")
.addEventListener("click", function () {
window.__TAURI__.fs
.writeFile(
{
file: pathToRead,
contents: fileInput.value,
},
{
dir: getDir(),
}
)
.catch(registerResponse);
});
}
} else {
registerResponse(response)
registerResponse(response);
}
}).catch(registerResponse)
}
)
})
.catch(registerResponse);
});
38 changes: 22 additions & 16 deletions tauri/examples/communication/dist/http.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
const methodSelect = document.getElementById('request-method')
const requestUrlInput = document.getElementById('request-url')
const requestBodyInput = document.getElementById('request-body')
const methodSelect = document.getElementById("request-method");
const requestUrlInput = document.getElementById("request-url");
const requestBodyInput = document.getElementById("request-body");

document.getElementById('make-request').addEventListener('click', function () {
const method = methodSelect.value || 'GET'
const url = requestUrlInput.value || ''
document.getElementById("make-request").addEventListener("click", function () {
const method = methodSelect.value || "GET";
const url = requestUrlInput.value || "";

const options = {
url: url,
method: method
}
method: method,
};

let body = requestBodyInput.value || ''
if ((body.startsWith('{') && body.endsWith('}')) || (body.startsWith('[') && body.endsWith(']'))) {
body = JSON.parse(body)
} else if (body.startsWith('/') || body.match(/\S:\//g)) {
options.bodyAsFile = true
let body = requestBodyInput.value || "";
if (
(body.startsWith("{") && body.endsWith("}")) ||
(body.startsWith("[") && body.endsWith("]"))
) {
body = JSON.parse(body);
} else if (body.startsWith("/") || body.match(/\S:\//g)) {
options.bodyAsFile = true;
}
options.body = body
options.body = body;

window.__TAURI__.http.request(options).then(registerResponse).catch(registerResponse)
})
window.__TAURI__.http
.request(options)
.then(registerResponse)
.catch(registerResponse);
});
Loading

0 comments on commit 2fd1067

Please sign in to comment.