Created
February 10, 2023 14:08
-
-
Save rcaetano/65733579b3fe69ecf1958a069609e1bb to your computer and use it in GitHub Desktop.
Create manifest from Akord Vault
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
{ | |
"name": "manifest", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"type": "module", | |
"dependencies": { | |
"@akord/akord-js": "^3.2.0" | |
} | |
} |
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
import { Akord } from "@akord/akord-js"; | |
const generateManifest = async (akord, vaultId, indexName) => { | |
// takes a flat list of folders and stacks and generates a tree | |
const treeify = (folders, stacks) => { | |
// initalize our treelist with a root folder + stacks | |
var treeList = [{ id: null, parentId: null, name: null, stacks: [] }]; | |
stacks.forEach((s) => { | |
if (!s["parentId"]) treeList[0]["stacks"].push(s); | |
}); | |
// setup a lookup table | |
var lookup = {}; | |
folders.forEach(function (obj) { | |
lookup[obj["id"]] = obj; | |
obj["children"] = []; | |
obj["stacks"] = []; | |
// add the related stacks to this folder | |
stacks.forEach((s) => { | |
if (s["parentId"] === obj["id"]) obj["stacks"].push(s); | |
}); | |
}); | |
// add the folders to its parent folder (tree) | |
folders.forEach(function (obj) { | |
if (obj["parentId"] != null) { | |
lookup[obj["parentId"]]["children"].push(obj); | |
} else { | |
treeList.push(obj); | |
} | |
}); | |
return treeList; | |
}; | |
// take the hierachical tree and compute the folder paths | |
const computePaths = (tree, path) => { | |
var paths = []; | |
tree.forEach((folder) => { | |
folder.stacks.forEach((stack) => { | |
// construct the path name | |
var pathName = [path, folder.name, stack.name] | |
.filter((p) => p != null) | |
.join("/"); | |
var arweaveId = stack.versions | |
.slice(-1)[0] | |
.resourceUri.filter((r) => { | |
if (r.split(":")[0] == "arweave") return r; | |
}) | |
.map((r) => r.split(":")[1])[0]; | |
paths.push({ | |
id: arweaveId, | |
path: pathName, | |
}); | |
}); | |
// process the children | |
if (folder.children) { | |
var pathName = folder.name; | |
if (path) pathName = [path, folder.name].join("/"); | |
var children = computePaths(folder.children, pathName); | |
paths.push(...children); | |
} | |
}); | |
return paths; | |
}; | |
// load and clean list of folders | |
var folders = (await akord.folder.list(vaultId)).items.map((n) => { | |
const { id, parentId, name } = n; | |
return { id, parentId, name }; | |
}); | |
// console.log(JSON.stringify(folders, null, 2)); | |
// load and clean list of stacks | |
const stacks = (await akord.stack.list(vaultId)).items.map((s) => { | |
const { id, parentId, name, versions } = s; | |
return { id, parentId, name, versions }; | |
}); | |
const tree = treeify(folders, stacks); | |
// console.log(JSON.stringify(tree, null, 2)); | |
const paths = computePaths(tree, null); | |
// console.log(JSON.stringify(paths, null, 2)); | |
// map paths to manifest hash | |
var manifest = {}; | |
paths.forEach((path) => { | |
manifest[path.path] = { id: path.id }; | |
}); | |
return { | |
manifest: "arweave/paths", | |
version: "0.1.0", | |
index: { | |
path: indexName || "index.html", | |
}, | |
paths: manifest, | |
}; | |
}; | |
// test | |
const email = "-- akord username --"; | |
const pass = "-- akord password --"; | |
const vaultId = "PQ0BKmRJHoZFLfD_DsHebKjhD7VZy5bK8W8QylJHK0I"; | |
const { akord } = await Akord.auth.signIn(email, pass); | |
// const { akord } = new Akord(); | |
const manifest = await generateManifest(akord, vaultId); | |
console.log(JSON.stringify(manifest, null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment