From b9efc3b3163aa92ba8207eccb7b7c84b421a50a3 Mon Sep 17 00:00:00 2001 From: stefano Date: Tue, 6 Aug 2024 22:32:03 +0100 Subject: [PATCH] Fix data creation + add test --- examples/single/bos.config.json | 3 - .../single/test/nestedTest/nestedTest.json | 3 - examples/single/test/test.json | 3 - examples/single/test1/test1.json | 3 - lib/deploy.ts | 8 +-- lib/utils/fs.ts | 24 ++++--- tests/unit/devNoMock.ts | 65 ++++++++++++++++++- 7 files changed, 82 insertions(+), 27 deletions(-) delete mode 100644 examples/single/test/nestedTest/nestedTest.json delete mode 100644 examples/single/test/test.json delete mode 100644 examples/single/test1/test1.json diff --git a/examples/single/bos.config.json b/examples/single/bos.config.json index 6704118..273c159 100644 --- a/examples/single/bos.config.json +++ b/examples/single/bos.config.json @@ -8,8 +8,5 @@ "aliases": ["./aliases.testnet.json"], "index": "quickstart.testnet/widget/home" } - }, - "data": { - "include": ["test", "test1"] } } diff --git a/examples/single/test/nestedTest/nestedTest.json b/examples/single/test/nestedTest/nestedTest.json deleted file mode 100644 index 519728a..0000000 --- a/examples/single/test/nestedTest/nestedTest.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "data": "this is a nested folder" -} \ No newline at end of file diff --git a/examples/single/test/test.json b/examples/single/test/test.json deleted file mode 100644 index e225b92..0000000 --- a/examples/single/test/test.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "data": "this is test" -} \ No newline at end of file diff --git a/examples/single/test1/test1.json b/examples/single/test1/test1.json deleted file mode 100644 index 816865d..0000000 --- a/examples/single/test1/test1.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "data": "this is test1" -} \ No newline at end of file diff --git a/lib/deploy.ts b/lib/deploy.ts index dbdd34a..21d30d1 100644 --- a/lib/deploy.ts +++ b/lib/deploy.ts @@ -206,11 +206,9 @@ export async function deployAppDataFolders( const result = {}; for (const folder of config.data.include) { - try { - await processDirectory(folder, result); - } catch (error) { - console.error(`Error processing folder ${folder}:`, error); - } + const folderName = path.basename(folder); + result[folderName] = {}; + await processDirectory(folder, '', result[folderName]); } const args = { diff --git a/lib/utils/fs.ts b/lib/utils/fs.ts index 399a511..461af08 100644 --- a/lib/utils/fs.ts +++ b/lib/utils/fs.ts @@ -17,21 +17,27 @@ async function loopThroughFiles(pwd: string, callback: (file: string) => Promise } } -async function processDirectory(dir, result) { - const files = await readdirSync(dir, { withFileTypes: true }); +async function processDirectory(baseDir, currentDir, result) { + const files = await readdirSync(path.join(baseDir, currentDir), { withFileTypes: true }); for (const file of files) { - const filePath = path.join(dir, file.name); + const relativePath = path.join(currentDir, file.name); + const fullPath = path.join(baseDir, relativePath); + if (file.isDirectory()) { - await processDirectory(filePath, result); + await processDirectory(baseDir, relativePath, result); } else if (path.extname(file.name).toLowerCase() === '.json') { try { - const fileContent = await readFileSync(filePath, 'utf8'); + const fileContent = await readFileSync(fullPath, 'utf8'); const jsonContent = JSON.parse(fileContent); - const relativePath = path.relative(process.cwd(), filePath); - const key = path.dirname(relativePath) + '/' + path.basename(file.name, '.json'); - result[key] = jsonContent; + let key; + if (currentDir === '') { + key = path.basename(file.name, '.json'); + } else { + key = path.join(currentDir, path.basename(file.name, '.json')).replace(/[\\/]/g, '.'); + } + result[key] = { "": JSON.stringify(jsonContent) }; } catch (error) { - console.error(`Error processing file ${filePath}:`, error); + console.error(`Error processing file ${fullPath}:`, error); } } } diff --git a/tests/unit/devNoMock.ts b/tests/unit/devNoMock.ts index 5613260..ddb0322 100644 --- a/tests/unit/devNoMock.ts +++ b/tests/unit/devNoMock.ts @@ -1,6 +1,7 @@ import { startFileWatcher } from "@/lib/watcher"; import path from "path"; import fs from "fs"; +import { processDirectory } from "@/lib/utils/fs"; describe("File Watcher Tests", () => { let watcher; @@ -37,4 +38,66 @@ describe("File Watcher Tests", () => { fs.rmSync(tempDirPath, { recursive: true, force: true }); } }); -}); \ No newline at end of file +}); + +describe('Folder structure processing', () => { + const tempDirPath = path.join(__dirname, 'temp_test_dir'); + const testFolderPath = path.join(tempDirPath, 'test'); + const nestedTestFolderPath = path.join(testFolderPath, 'nestedTest'); + const test1FolderPath = path.join(tempDirPath, 'test1'); + + beforeAll(() => { + fs.mkdirSync(tempDirPath, { recursive: true }); + fs.mkdirSync(testFolderPath, { recursive: true }); + fs.mkdirSync(nestedTestFolderPath, { recursive: true }); + fs.mkdirSync(test1FolderPath, { recursive: true }); + + fs.writeFileSync( + path.join(testFolderPath, 'file.json'), + JSON.stringify({ data: "this is test" }) + ); + fs.writeFileSync( + path.join(nestedTestFolderPath, 'nestedFile.json'), + JSON.stringify({ data: "this is a nested folder", data2: "other data" }) + ); + fs.writeFileSync( + path.join(test1FolderPath, 'file1.json'), + JSON.stringify({ data: "this is test1" }) + ); + }); + + it("should build the correct object based on folder structure", async () => { + const config = { + data: { + include: [tempDirPath] + }, + account: 'myaccount' + }; + + const result = {}; + + for (const folder of config.data.include) { + const folderName = path.basename(folder); + result[folderName] = {}; + await processDirectory(folder, '', result[folderName]); + } + + expect(result).toEqual({ + 'temp_test_dir': { + 'test.nestedTest.nestedFile': { + '': JSON.stringify({ data: "this is a nested folder", data2: "other data" }) + }, + 'test.file': { + '': JSON.stringify({ data: "this is test" }) + }, + 'test1.file1': { + '': JSON.stringify({ data: "this is test1" }) + } + } + }); + }); + + afterAll(() => { + fs.rmSync(tempDirPath, { recursive: true, force: true }); + }); +});