1
import path from "path-browserify";
2
import type { PyodideInterface } from "pyodide";
4
export const globalHomeDir = "/home/pyodide";
5
export const getAppHomeDir = (appId: string): string =>
6
`${globalHomeDir}/${appId}`;
7
export const resolveAppHomeBasedPath = (
11
const normalized = path.normalize(filePath);
12
return path.resolve(getAppHomeDir(appId), filePath);
15
function ensureParent(pyodide: PyodideInterface, filePath: string): void {
16
const normalized = path.normalize(filePath);
18
const dirPath = path.dirname(normalized);
20
const dirNames = dirPath.split("/");
22
const chDirNames: string[] = [];
23
for (const dirName of dirNames) {
24
chDirNames.push(dirName);
25
const dirPath = chDirNames.join("/");
27
if (pyodide.FS.analyzePath(dirPath).exists) {
28
if (pyodide.FS.isDir(dirPath)) {
29
throw new Error(`"${dirPath}" already exists and is not a directory.`);
35
pyodide.FS.mkdir(dirPath);
37
console.error(`Failed to create a directory "${dirPath}"`);
43
export function writeFileWithParents(
44
pyodide: PyodideInterface,
46
data: string | ArrayBufferView,
47
opts?: Parameters<PyodideInterface["FS"]["writeFile"]>[2]
49
ensureParent(pyodide, filePath);
50
pyodide.FS.writeFile(filePath, data, opts);
53
export function renameWithParents(
54
pyodide: PyodideInterface,
58
ensureParent(pyodide, newPath);
59
pyodide.FS.rename(oldPath, newPath);