Skip to content

Commit 8b688b5

Browse files
committed
add convertLocal utility function
1 parent b2c789c commit 8b688b5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

cpkernel/src/resolver_local.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,54 @@ async function pastePod({ appDir, reponame, id, parentId, index }) {
101101
return true;
102102
}
103103

104+
// Deprecated Utility function
105+
async function convertLocal(podsdir) {
106+
// convert local files
107+
// 1. read local files
108+
let jsons = await fs.promises.readdir(podsdir);
109+
let podlst = [];
110+
for (let jsonfile of jsons) {
111+
let jobj = JSON.parse(
112+
await fs.promises.readFile(path.join(podsdir, jsonfile))
113+
);
114+
podlst.push(jobj);
115+
}
116+
// 2. build d
117+
podlst.forEach((pod) => {
118+
pod.parent = pod.parentId || "ROOT";
119+
delete pod.parentId;
120+
pod.children = [];
121+
});
122+
// children
123+
let d = {};
124+
d["ROOT"] = {
125+
type: "DECK",
126+
id: "ROOT",
127+
children: [],
128+
};
129+
podlst.forEach((pod) => {
130+
d[pod.id] = pod;
131+
});
132+
// 3. modify format and save back to files
133+
podlst.forEach((pod) => {
134+
d[pod.parent].children.push(pod.id);
135+
});
136+
podlst.forEach((pod) => {
137+
pod.children.sort((a, b) => d[a].index - d[b].index);
138+
});
139+
podlst.forEach((pod) => {
140+
delete pod.index;
141+
});
142+
podlst.push(d["ROOT"]);
143+
// 4. save back to files
144+
podlst.forEach(async (pod) => {
145+
await fs.promises.writeFile(
146+
path.join(podsdir, `${pod.id}.json`),
147+
JSON.stringify(pod, null, 2)
148+
);
149+
});
150+
}
151+
104152
export function getResolvers(appDir) {
105153
if (!fs.existsSync(appDir)) {
106154
fs.mkdirSync(appDir, { recursive: true });

0 commit comments

Comments
 (0)