Skip to content

Commit

Permalink
Add support for nested folders
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-face committed Aug 4, 2024
1 parent c369538 commit 2b02aea
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
3 changes: 3 additions & 0 deletions examples/single/bos.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"aliases": ["./aliases.testnet.json"],
"index": "quickstart.testnet/widget/home"
}
},
"data": {
"include": ["test", "test1"]
}
}
3 changes: 3 additions & 0 deletions examples/single/test/nestedTest/nestedTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": "this is a nested folder"
}
13 changes: 2 additions & 11 deletions lib/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fs from "fs";
import { buildApp } from "@/lib/build";
import { readConfig } from "@/lib/config";
import { Network } from "@/lib/types";
import { move, pathExists, readdir, remove } from "@/lib/utils/fs";
import { move, pathExists, processDirectory, readdir, remove } from "@/lib/utils/fs";
import { readWorkspace } from "@/lib/workspace";
import { SOCIAL_CONTRACT } from './server';

Expand Down Expand Up @@ -191,16 +191,7 @@ export async function deployAppDataFolders(appName: string, opts: DeployOptions)

for (const folder of config.data.include) {
try {
const files = await fs.readdirSync(folder);

for (const file of files) {
const filePath = path.join(folder, file);
const fileContent = await fs.readFileSync(filePath, 'utf8');
const jsonContent = JSON.parse(fileContent);

const key = path.basename(file, '.json');
result[key] = jsonContent;
}
await processDirectory(folder, result);
} catch (error) {
console.error(`Error processing folder ${folder}:`, error);
}
Expand Down
23 changes: 22 additions & 1 deletion lib/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { copy, ensureDir, move, outputFile, pathExists, readdir, readFile, readJson, remove, writeJson, existsSync, promises } from 'fs-extra';
import { readdirSync, readFileSync } from 'fs';
import path from 'path';

async function loopThroughFiles(pwd: string, callback: (file: string) => Promise<void>) {
Expand All @@ -16,4 +17,24 @@ async function loopThroughFiles(pwd: string, callback: (file: string) => Promise
}
}

export { copy, ensureDir, loopThroughFiles, move, outputFile, pathExists, readdir, readFile, readJson, remove, writeJson, existsSync, promises };
async function processDirectory(dir, result) {
const files = await readdirSync(dir, { withFileTypes: true });
for (const file of files) {
const filePath = path.join(dir, file.name);
if (file.isDirectory()) {
await processDirectory(filePath, result);
} else if (path.extname(file.name).toLowerCase() === '.json') {
try {
const fileContent = await readFileSync(filePath, '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;
} catch (error) {
console.error(`Error processing file ${filePath}:`, error);
}
}
}
}

export { copy, ensureDir, loopThroughFiles, move, outputFile, pathExists, readdir, readFile, readJson, remove, writeJson, existsSync, promises, processDirectory };

0 comments on commit 2b02aea

Please sign in to comment.