Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the initial file setup for @stlite/desktop on Windows #466

Merged
merged 5 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update walkRead() to return objects with POSIX paths even on Windows
  • Loading branch information
whitphx committed Jan 20, 2023
commit 62428a6d6ce9d9a77af4eedec856db878b9c7d72
13 changes: 0 additions & 13 deletions packages/desktop/electron/__tests__/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,4 @@ describe("walkRead", () => {
"bar/bar.txt": Buffer.from("consectetur adipiscing elit\n"),
});
});

it("walks in the specified directory, reads the all child file contents, and returns the pairs of the absolute path and the file content if relative=false", async () => {
const targetDirPath = path.resolve(__dirname, "./foo/");
const result = await walkRead(targetDirPath, false);
expect(result).toEqual({
[path.resolve(targetDirPath, "./foo.txt")]: Buffer.from(
"Lorem ipsum dolor sit amet\n"
),
[path.resolve(targetDirPath, "./bar/bar.txt")]: Buffer.from(
"consectetur adipiscing elit\n"
),
});
});
});
25 changes: 16 additions & 9 deletions packages/desktop/electron/file.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as fsPromises from "fs/promises";
import * as path from "path";

export async function walkRead(
dirPath: string,
relative = true
/**
* Returns an object whose keys are absolute paths of the files and values are the file contents.
* Note that the paths are OS-specific, "/" for POSIX and "\" for Windows,
* so this function is only expected to be called from `walkRead()` that converts the paths to be POSIX.
*/
async function walkReadAbsPath(
dirPath: string
): Promise<Record<string, Buffer>> {
const fileContents: Record<string, Buffer> = {};
const childNames = await fsPromises.readdir(dirPath);
Expand All @@ -12,7 +16,7 @@ export async function walkRead(
const childPath = path.join(dirPath, childName);
const stat = await fsPromises.stat(childPath);
if (stat.isDirectory()) {
const childFileContents = await walkRead(childPath, false);
const childFileContents = await walkReadAbsPath(childPath);
Object.assign(fileContents, childFileContents);
} else {
const fileBin = await fsPromises.readFile(childPath);
Expand All @@ -21,16 +25,19 @@ export async function walkRead(
})
);

// TODO: All paths must be converted to be POSIX
return fileContents;
}

if (!relative) {
return fileContents;
}
export async function walkRead(
dirPath: string
): Promise<Record<string, Buffer>> {
const fileContents = await walkReadAbsPath(dirPath);

const relPathFileContents: Record<string, Buffer> = {};
Object.keys(fileContents).forEach((absPath) => {
const relPath = path.relative(dirPath, absPath);
relPathFileContents[relPath] = fileContents[absPath];
const posixRelPath = relPath.split(path.sep).join(path.posix.sep); // Convert the path separators on Windows to POSIX ones.
relPathFileContents[posixRelPath] = fileContents[absPath];
});
return relPathFileContents;
}