Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
feat: Nested source directories (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarlow12 authored Nov 5, 2020
1 parent d48de94 commit a603a27
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
63 changes: 37 additions & 26 deletions src/electron/providers/storage/localFileSystem.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import fs from "fs";
import path, { relative } from "path";
import path, { relative, sep } from "path";
import shortid from "shortid";
import LocalFileSystem from "./localFileSystem";
import mockFs from "mock-fs";
import { AssetService } from "../../../services/assetService";
import registerMixins from "../../../registerMixins";

jest.mock("electron", () => ({
dialog: {
showOpenDialog: jest.fn(),
},
}));
import { dialog } from "electron";
import { AssetService } from "../../../services/assetService";

registerMixins();

describe("LocalFileSystem Storage Provider", () => {
let localFileSystem: LocalFileSystem = null;
const sourcePath = path.join("path", "to", "my", "source");
const sourceFilePaths = [
path.join(sourcePath, "file1.jpg"),
path.join(sourcePath, "file2.jpg"),
path.join(sourcePath, "subDir1", "file3.jpg"),
path.join(sourcePath, "subDir1", "file4.jpg"),
path.join(sourcePath, "subDir2", "file5.jpg"),
path.join(sourcePath, "subDir2", "file6.jpg"),
path.join(sourcePath, "subDir2", "subSubDir2", "file7.jpg"),
path.join(sourcePath, "subDir2", "subSubDir2", "file8.jpg"),
];

beforeEach(() => {
localFileSystem = new LocalFileSystem(null);
Expand All @@ -28,7 +41,18 @@ describe("LocalFileSystem Storage Provider", () => {
source: {
"file1.jpg": "contents",
"file2.jpg": "contents",
"file3.jpg": "contents",
"subDir1": {
"file3.jpg": "contents",
"file4.jpg": "contents",
},
"subDir2": {
"file5.jpg": "contents",
"file6.jpg": "contents",
"subSubDir2": {
"file7.jpg": "contents",
"file8.jpg": "contents",
},
},
},
},
},
Expand Down Expand Up @@ -113,34 +137,21 @@ describe("LocalFileSystem Storage Provider", () => {
await expect(localFileSystem.deleteFile("/path/to/fake/file.txt")).resolves.not.toBeNull();
});

it("getAssets uses an absolute path when relative not specified", async () => {
it("getAssets gets all files recursively using path relative to the source", async () => {
AssetService.createAssetFromFilePath = jest.fn(() => []);
await localFileSystem.getAssets(sourcePath);
await localFileSystem.getAssets(sourcePath, true);
const calls: any[] = (AssetService.createAssetFromFilePath as any).mock.calls;
expect(calls).toHaveLength(3);
calls.forEach((call, index) => {
const absolutePath = path.join(sourcePath, `file${index + 1}.jpg`);
expect(call).toEqual([
absolutePath,
undefined,
absolutePath,
]);
});
expect(calls).toHaveLength(8);
expect(calls).toEqual(sourceFilePaths.map((path) => [
path, undefined, path.replace(`${sourcePath}${sep}`, ""),
]));
});

it("getAssets uses a path relative to the source connection when specified", async () => {
it("getAssets gets all files recursively using absolute path", async () => {
AssetService.createAssetFromFilePath = jest.fn(() => []);
await localFileSystem.getAssets(sourcePath, true);
await localFileSystem.getAssets(sourcePath, false);
const calls: any[] = (AssetService.createAssetFromFilePath as any).mock.calls;
expect(calls).toHaveLength(3);
calls.forEach((call, index) => {
const relativePath = `file${index + 1}.jpg`;
const absolutePath = path.join(sourcePath, relativePath);
expect(call).toEqual([
absolutePath,
undefined,
relativePath,
]);
});
expect(calls).toHaveLength(8);
expect(calls).toEqual(sourceFilePaths.map((path) => [path, undefined, path]));
});
});
16 changes: 12 additions & 4 deletions src/electron/providers/storage/localFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,16 @@ export default class LocalFileSystem implements IStorageProvider {
});
}

public listFiles(folderPath: string): Promise<string[]> {
return this.listItems(path.normalize(folderPath), (stats) => !stats.isDirectory());
public async listFiles(folderPath: string): Promise<string[]> {
const normalizedPath = path.normalize(folderPath);
console.log(`Listing files from ${normalizedPath}`);
const files = await this.listItems(normalizedPath, (stats) => !stats.isDirectory());
const directories = await this.listItems(normalizedPath, (stats) => stats.isDirectory());
await directories.forEachAsync(async (directory) => {

This comment has been minimized.

Copy link
@MathiasPolfliet

MathiasPolfliet Jan 26, 2021

@tbarlow12
I fixed it for me with some stackoverflow and first time coding in typescript:
if (typeof directories !== 'undefined' && directories.length > 0) { ... }

const directoryFiles = await this.listFiles(directory);
directoryFiles.forEach((file) => files.push(file));
});
return files;
}

public listContainers(folderPath: string): Promise<string[]> {
Expand Down Expand Up @@ -138,8 +146,8 @@ export default class LocalFileSystem implements IStorageProvider {
}

public async getAssets(sourceConnectionFolderPath?: string, relativePath: boolean = false): Promise<IAsset[]> {
return (await this.listFiles(path.normalize(sourceConnectionFolderPath)))
.map((filePath) => AssetService.createAssetFromFilePath(
const files = await this.listFiles(path.normalize(sourceConnectionFolderPath));
return files.map((filePath) => AssetService.createAssetFromFilePath(
filePath,
undefined,
relativePath ? path.relative(sourceConnectionFolderPath, filePath) : filePath))
Expand Down

0 comments on commit a603a27

Please sign in to comment.