mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
[next] FS create folder before writing in it (#1582)
This commit is contained in:
committed by
GitHub
parent
7e0aceced1
commit
2555833831
@@ -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 */
|
||||
|
||||
@@ -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("/"));
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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
@@ -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}]`,
|
||||
);
|
||||
|
||||
@@ -37,13 +37,9 @@ export const loadPyodide = () => ({
|
||||
FS: {
|
||||
mkdirTree() {},
|
||||
writeFile() {},
|
||||
analyzePath: (path) => ({
|
||||
parentPath: dirname(path),
|
||||
name: basename(path),
|
||||
}),
|
||||
},
|
||||
PATH: { dirname },
|
||||
_module: {
|
||||
PATH: { dirname },
|
||||
PATH_FS: {
|
||||
resolve: (path) => path,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user