feat: Make the rename effect (#1669)

* feat: Make the rename effect

* chore: Change to dst and src

* chore: update the remove file to use dst src
This commit is contained in:
J M
2022-07-20 13:42:54 -06:00
committed by GitHub
parent 0f027fefb8
commit 5fa743755d
4 changed files with 204 additions and 28 deletions

View File

@@ -1,6 +1,10 @@
import Deno from "/deno_global.js";
import * as mainModule from "/embassy.js";
function requireParam(param) {
throw new Error(`Missing required parameter ${param}`);
}
/**
* This is using the simplified json pointer spec, using no escapes and arrays
* @param {object} obj
@@ -20,52 +24,83 @@ function jsonPointerValue(obj, pointer) {
function maybeDate(value) {
if (!value) return value;
return new Date(value)
return new Date(value);
}
const writeFile = ({ path, volumeId, toWrite }) => Deno.core.opAsync("write_file", volumeId, path, toWrite);
const writeFile = (
{
path = requireParam("path"),
volumeId = requireParam("volumeId"),
toWrite = requireParam("toWrite"),
} = requireParam("options"),
) => Deno.core.opAsync("write_file", volumeId, path, toWrite);
const readFile = ({ volumeId, path }) => Deno.core.opAsync("read_file", volumeId, path);
const metadata = async ({ volumeId, path }) => {
const data = await Deno.core.opAsync("metadata", volumeId, path)
const readFile = (
{ volumeId = requireParam("volumeId"), path = requireParam("path") } = requireParam("options"),
) => Deno.core.opAsync("read_file", volumeId, path);
const rename = (
{
srcVolume = requireParam("srcVolume"),
dstVolume = requireParam("dstVolume"),
srcPath = requireParam("srcPath"),
dstPath = requireParam("dstPath"),
} = requireParam("options"),
) => Deno.core.opAsync("rename", srcVolume, srcPath, dstVolume, dstPath);
const metadata = async (
{ volumeId = requireParam("volumeId"), path = requireParam("path") } = requireParam("options"),
) => {
const data = await Deno.core.opAsync("metadata", volumeId, path);
return {
...data,
modified: maybeDate(data.modified),
created: maybeDate(data.created),
accessed: maybeDate(data.accessed),
}
};
};
const removeFile = ({ volumeId, path }) => Deno.core.opAsync("remove_file", volumeId, path);
const removeFile = (
{ volumeId = requireParam("volumeId"), path = requireParam("path") } = requireParam("options"),
) => Deno.core.opAsync("remove_file", volumeId, path);
const isSandboxed = () => Deno.core.opSync("is_sandboxed");
const writeJsonFile = ({ volumeId, path, toWrite }) =>
const writeJsonFile = (
{
volumeId = requireParam("volumeId"),
path = requireParam("path"),
toWrite = requireParam("toWrite"),
} = requireParam("options"),
) =>
writeFile({
volumeId,
path,
toWrite: JSON.stringify(toWrite),
});
const readJsonFile = async ({ volumeId, path }) => JSON.parse(await readFile({ volumeId, path }));
const createDir = ({ volumeId, path }) => Deno.core.opAsync("create_dir", volumeId, path);
const removeDir = ({ volumeId, path }) => Deno.core.opAsync("remove_dir", volumeId, path);
const trace = (x) => Deno.core.opSync("log_trace", x);
const warn = (x) => Deno.core.opSync("log_warn", x);
const error = (x) => Deno.core.opSync("log_error", x);
const debug = (x) => Deno.core.opSync("log_debug", x);
const info = (x) => Deno.core.opSync("log_info", x);
const fetch = async (url, options = null) => {
const {body, ...response} = await Deno.core.opAsync("fetch", url, options);
const textValue = Promise.resolve(body)
const readJsonFile = async (
{ volumeId = requireParam("volumeId"), path = requireParam("path") } = requireParam("options"),
) => JSON.parse(await readFile({ volumeId, path }));
const createDir = (
{ volumeId = requireParam("volumeId"), path = requireParam("path") } = requireParam("options"),
) => Deno.core.opAsync("create_dir", volumeId, path);
const removeDir = (
{ volumeId = requireParam("volumeId"), path = requireParam("path") } = requireParam("options"),
) => Deno.core.opAsync("remove_dir", volumeId, path);
const trace = (whatToTrace = requireParam('whatToTrace')) => Deno.core.opSync("log_trace", whatToTrace);
const warn = (whatToTrace = requireParam('whatToTrace')) => Deno.core.opSync("log_warn", whatToTrace);
const error = (whatToTrace = requireParam('whatToTrace')) => Deno.core.opSync("log_error", whatToTrace);
const debug = (whatToTrace = requireParam('whatToTrace')) => Deno.core.opSync("log_debug", whatToTrace);
const info = (whatToTrace = requireParam('whatToTrace')) => Deno.core.opSync("log_info", whatToTrace);
const fetch = async (url = requireParam ('url'), options = null) => {
const { body, ...response } = await Deno.core.opAsync("fetch", url, options);
const textValue = Promise.resolve(body);
return {
...response,
text() {
return textValue
return textValue;
},
json() {
return textValue.then(x => JSON.parse(x))
}
}
return textValue.then((x) => JSON.parse(x));
},
};
};
const currentFunction = Deno.core.opSync("current_function");
const input = Deno.core.opSync("get_input");
const variable_args = Deno.core.opSync("get_variable_args");
@@ -85,14 +120,15 @@ const effects = {
removeFile,
createDir,
removeDir,
metadata
metadata,
rename,
};
const runFunction = jsonPointerValue(mainModule, currentFunction);
(async () => {
if (typeof runFunction !== "function") {
error(`Expecting ${ currentFunction } to be a function`);
throw new Error(`Expecting ${ currentFunction } to be a function`);
error(`Expecting ${currentFunction} to be a function`);
throw new Error(`Expecting ${currentFunction} to be a function`);
}
const answer = await runFunction(effects, input, ...variable_args);
setState(answer);