[next] FS create folder before writing in it (#1582)

This commit is contained in:
Andrea Giammarchi
2023-07-17 13:46:17 +02:00
committed by GitHub
parent 7e0aceced1
commit 2555833831
8 changed files with 19 additions and 47 deletions

View File

@@ -1,4 +1,4 @@
import { clean, writeFile as writeFileUtil } from "./_utils.js";
import { clean } from "./_utils.js";
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
@@ -20,7 +20,4 @@ export const runEvent = async (interpreter, code, event) => {
for (const key of keys) [context, target] = [target, target[key]];
await target.call(context, event);
};
export const writeFile = ({ FS }, path, buffer) =>
writeFileUtil(FS, path, buffer);
/* c8 ignore stop */

View File

@@ -30,21 +30,21 @@ export const stdio = (init) => {
},
};
};
/* c8 ignore stop */
// This should be the only helper needed for all Emscripten based FS exports
export const writeFile = (FS, path, buffer) => {
const { parentPath, name } = FS.analyzePath(path, true);
FS.mkdirTree(parentPath);
return FS.writeFile([parentPath, name].join("/"), new Uint8Array(buffer), {
export const writeFile = ({ FS, PATH, PATH_FS }, path, buffer) => {
const absPath = PATH_FS.resolve(path);
FS.mkdirTree(PATH.dirname(absPath));
return FS.writeFile(absPath, new Uint8Array(buffer), {
canOwn: true,
});
};
/* c8 ignore stop */
// This is instead a fallback for Lua or others
export const writeFileShim = (FS, path, buffer) => {
path = resolve(FS, path);
mkdirTree(FS, dirname(path));
path = resolve(FS, path);
return FS.writeFile(path, new Uint8Array(buffer), { canOwn: true });
};
@@ -57,6 +57,7 @@ const dirname = (path) => {
const mkdirTree = (FS, path) => {
const current = [];
for (const branch of path.split("/")) {
if (branch === ".") continue;
current.push(branch);
if (branch) FS.mkdir(current.join("/"));
}

View File

@@ -1,11 +1,5 @@
import { fetchPaths, stdio } from "./_utils.js";
import {
registerJSModule,
run,
runAsync,
runEvent,
writeFile,
} from "./_python.js";
import { fetchPaths, stdio, writeFile } from "./_utils.js";
import { registerJSModule, run, runAsync, runEvent } from "./_python.js";
const type = "micropython";
@@ -26,6 +20,7 @@ export default {
run,
runAsync,
runEvent,
writeFile,
writeFile: ({ FS, _module: { PATH, PATH_FS } }, path, buffer) =>
writeFile({ FS, PATH, PATH_FS }, path, buffer),
};
/* c8 ignore stop */

View File

@@ -1,11 +1,5 @@
import { fetchPaths, stdio } from "./_utils.js";
import {
registerJSModule,
run,
runAsync,
runEvent,
writeFile,
} from "./_python.js";
import { fetchPaths, stdio, writeFile } from "./_utils.js";
import { registerJSModule, run, runAsync, runEvent } from "./_python.js";
const type = "pyodide";
@@ -34,6 +28,7 @@ export default {
run,
runAsync,
runEvent,
writeFile,
writeFile: ({ FS, PATH, _module: { PATH_FS } }, path, buffer) =>
writeFile({ FS, PATH, PATH_FS }, path, buffer),
};
/* c8 ignore stop */

View File

@@ -66,6 +66,6 @@
"coincident": "^0.11.1"
},
"worker": {
"blob": "sha256-idLCB+fF+/sTx1vHssJPaTaZgW6neAWX5SJ6leLl1kA="
"blob": "sha256-lKnkzAqePCcARjTnFS6sRscDVvubVW8t5ptuccVkREc="
}
}

File diff suppressed because one or more lines are too long

View File

@@ -12,16 +12,6 @@ const FS = {
},
};
// REQUIRE INTEGRATION TESTS
writeFileShim(FS, "./test/abc.js", []);
assert(JSON.stringify(FS.mkdir_args), `["${__dirname}/test"]`);
assert(
JSON.stringify(FS.writeFile_args),
`["${__dirname}/test/abc.js",{},{"canOwn":true}]`,
);
writeFileShim(FS, "/./../abc.js", []);
assert(JSON.stringify(FS.mkdir_args), `["${__dirname}"]`);
assert(
JSON.stringify(FS.writeFile_args),
`["${__dirname}/abc.js",{},{"canOwn":true}]`,
);

View File

@@ -37,13 +37,9 @@ export const loadPyodide = () => ({
FS: {
mkdirTree() {},
writeFile() {},
analyzePath: (path) => ({
parentPath: dirname(path),
name: basename(path),
}),
},
_module: {
PATH: { dirname },
_module: {
PATH_FS: {
resolve: (path) => path,
},