Created
May 22, 2024 00:58
-
-
Save SgtPooki/e17475cd145add67bbbc95453da6a399 to your computer and use it in GitHub Desktop.
example of creating a directory structure with file using Helia and @helia/unixfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = unixfs(helia) | |
let barDir = await fs.addDirectory({ path: './bar' }) | |
const aFileHtml = await fs.addFile({ path: './bar/a-file.html', content: uint8ArrayFromString('<html><body>Hello world</body></html>') }) | |
barDir = await fs.cp(aFileHtml, barDir, 'a-file.html') | |
let fooDir = await fs.addDirectory({ path: './foo' }) | |
fooDir = await fs.cp(barDir, fooDir, 'bar') | |
let deepDirCid = await fs.addDirectory({}) | |
deepDirCid = await fs.cp(fooDir, deepDirCid, 'foo') | |
// eslint-disable-next-line no-console | |
console.log(await fs.stat(deepDirCid)) | |
async function listAllItems (cid: CID): Promise<void> { | |
// eslint-disable-next-line no-console | |
console.log('listAllItems: ', cid) | |
for await (const item of fs.ls(cid)) { | |
// eslint-disable-next-line no-console | |
console.log('item: ', item) | |
if (item.type === 'directory') { | |
await listAllItems(item.cid) | |
} | |
} | |
} | |
await listAllItems(deepDirCid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment