Files
pyscript/pyscriptjs/directoryManifest.mjs
Hood Chatham 854e9d1378 Refactor pyexec (#1318)
This is some refactoring I did on the way towards resolving pyscript#1313.
I added a new _run_pyscript Python function which executes the code
inside a context manager that sets the display target. We can then
return a JS object wrapper directly from Python.

I moved the "installation" of the pyscript module to loadInterpreter,
and pyimport pyscript_py there and give it a type. This avoids a bunch
of creating and deleting of proxies for pyscript_py and allows us to
give it a type once and for all.

I also did some minor logic cleanup in a few places.
2023-03-30 04:34:24 +02:00

45 lines
1.5 KiB
JavaScript

// This logic split out because it is shared by:
// 1. esbuild.mjs
// 2. Jest setup.ts
import { join } from 'path';
import { opendir, readFile } from 'fs/promises';
/**
* List out everything in a directory, but skip __pycache__ directory. Used to
* list out the directory paths and the [file path, file contents] pairs in the
* Python package. All paths are relative to the directory we are listing. The
* directories are sorted topologically so that a parent directory always
* appears before its children.
*
* This is consumed in main.ts which calls mkdir for each directory and then
* writeFile to create each file.
*
* @param {string} dir The path to the directory we want to list out
* @returns {dirs: string[], files: [string, string][]}
*/
export async function directoryManifest(dir) {
const result = { dirs: [], files: [] };
await _directoryManifestHelper(dir, '.', result);
return result;
}
/**
* Recursive helper function for directoryManifest
*/
async function _directoryManifestHelper(root, dir, result) {
const dirObj = await opendir(join(root, dir));
for await (const d of dirObj) {
const entry = join(dir, d.name);
if (d.isDirectory()) {
if (d.name === '__pycache__') {
continue;
}
result.dirs.push(entry);
await _directoryManifestHelper(root, entry, result);
} else if (d.isFile()) {
result.files.push([entry, await readFile(join(root, entry), { encoding: 'utf-8' })]);
}
}
}