Skip to content

Commit

Permalink
refactor: move unique key function to listr tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Jun 3, 2021
1 parent 8e9d00a commit b67a43f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
13 changes: 8 additions & 5 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ const buildDico = new Listr(
.then(keys => {
const flatKeys = keys.flat();

if (flatKeys.length === 1) {
const uniqueKeys = flatKeys.filter((keyObj, i, flatKeys) => {
return flatKeys.findIndex(i => i.key === keyObj.key) === i;
});

if (uniqueKeys.length === 1) {
task.title = "One key found";
} else {
task.title = `${flatKeys.length} keys found`;
task.title = `${uniqueKeys.length} keys found`;
}

ctx.keys = flatKeys;
ctx.keys = uniqueKeys;
observer.complete();
})
.catch(error => {
Expand Down Expand Up @@ -149,8 +153,7 @@ export const build = async (
lineBreak();

try {
const { keys } = await buildDico.run();
console.log(keys);
await buildDico.run();
} catch (error) {
// Handle conflicts error
if (error instanceof BuildError && error.message.includes("conflict")) {
Expand Down
10 changes: 3 additions & 7 deletions src/core/dicojson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ export const createSchema = (
const schema: ConfigJSON["schema"] = {};
const conflicts: ProjectKey[][] = [];

const uniqueKeys = keys.filter((keyObj, i, keys) => {
return keys.findIndex(i => i.key === keyObj.key) === i;
});

keyloop: for (const keyObject of uniqueKeys) {
keyloop: for (const keyObject of keys) {
const parents = keyObject.key.replace(/^\$dico\./, "").split(".");
let pointer = schema;
const key = parents.pop() as string;
Expand All @@ -75,7 +71,7 @@ export const createSchema = (
typeof pointer[parent] === "string"
) {
const conflictPath = ["$dico", ...traversedPath].join(".");
const conflictKeyObj = uniqueKeys.find(i => i.key === conflictPath);
const conflictKeyObj = keys.find(i => i.key === conflictPath);
if (conflictKeyObj) {
const existing = conflicts.find(i => i[0].key === conflictKeyObj.key);
if (existing) {
Expand All @@ -93,7 +89,7 @@ export const createSchema = (
typeof pointer[parent] === "object" &&
typeof (pointer[parent] as ConfigJSON["schema"])[key] !== "undefined"
) {
const conflictKeyObj = uniqueKeys.filter(
const conflictKeyObj = keys.filter(
i => i.key !== keyObject.key && i.key.startsWith(keyObject.key)
);
conflicts.push([keyObject, ...conflictKeyObj]);
Expand Down

0 comments on commit b67a43f

Please sign in to comment.