Skip to content

Commit

Permalink
refactor: extract ProjectKey into proper interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Jun 3, 2021
1 parent 718dfb5 commit 9e4d278
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ All notable changes to this project will be documented in this file. See [standa
### Documentation

* add playground ([347f7c9](https://github.com/dico-app/dico-cli/commit/347f7c919e1227fa308b04eb960975ece3a34b29))
* rename playground files to prevent conflicts ([a82bf08](https://github.com/dico-app/dico-cli/commit/a82bf08a01810d918c75f305dabe1673bcb039bb))
* rename playground files to prevent conflicts ([32365e9](https://github.com/dico-app/dico-cli/commit/32365e9b74e3bbcb2bf4891d2dce43888f688329))
* update readme ([b34f0f0](https://github.com/dico-app/dico-cli/commit/b34f0f08e1c1286be9fcef9ec03eb4481b4859e1))

Expand Down
27 changes: 16 additions & 11 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import * as project from "../core/project";
import * as messages from "../messages";
import exit from "exit";
import { DEFAULT_TIMEOUT } from "../const";
import { ConfigJSON } from "../types";
import { ConfigJSON, ProjectKey } from "../types";

class BuildError extends Error {}

interface BuildDicoContext {
paths: string[];
keys: { path: string; key: string }[];
keys: ProjectKey[];
schema: ConfigJSON["schema"];
conflicts: { path: string; key: string }[][];
conflicts: ProjectKey[][];
}

const buildDico = new Listr(
Expand Down Expand Up @@ -62,12 +62,12 @@ const buildDico = new Listr(
const path = ctx.paths[i];

promises.push(
new Promise<{ path: string; key: string }[]>((res, rej) => {
new Promise<ProjectKey[]>((res, rej) => {
setTimeout(() => {
observer.next(`(${i + 1}/${ctx.paths.length}) ${path}`);
try {
const keys = project.crawlFile(path);
res(keys.map(key => ({ path, key })));
res(keys);
} catch (error) {
rej(error);
}
Expand Down Expand Up @@ -149,7 +149,8 @@ export const build = async (
lineBreak();

try {
await buildDico.run();
const { keys } = await buildDico.run();
console.log(keys);
} catch (error) {
// Handle conflicts error
if (error instanceof BuildError && error.message.includes("conflict")) {
Expand All @@ -165,17 +166,21 @@ export const build = async (
if (conflict.length === 1) {
console.log(
` Key ${chalk.cyan(conflict[0].key)} (${chalk.cyan(
conflict[0].path
)}) is conflicting`
`${conflict[0].file}:${conflict[0].line}:${conflict[0].column}`
)}) is conflicting with at least another unknown declaration, this shouldn't happen`
);
} else {
console.log(
` Key ${chalk.cyan(conflict[0].key)} (${chalk.cyan(
conflict[0].path
`${conflict[0].file}:${conflict[0].line}:${conflict[0].column}`
)}) is conflicting with key ${chalk.cyan(
conflict[1].key
)} (${chalk.cyan(conflict[1].path)})${
conflict.length > 2 ? ` and ${conflict.length - 2} more...` : ""
)} (${chalk.cyan(
`${conflict[1].file}:${conflict[1].line}:${conflict[1].column}`
)})${
conflict.length > 2
? ` and ${conflict.length - 2} more from that collection...`
: ""
}`
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/dicojson.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import path from "path";
import { JSON_FILE } from "../const";
import { ConfigJSON } from "../types";
import { ConfigJSON, ProjectKey } from "../types";
import * as messages from "../messages";
import detectIndent from "detect-indent";

Expand Down Expand Up @@ -47,13 +47,13 @@ export const write = (config: ConfigJSON): void => {

// TODO: Better test this
export const createSchema = (
keys: { path: string; key: string }[]
keys: ProjectKey[]
): {
schema: ConfigJSON["schema"];
conflicts: { path: string; key: string }[][];
conflicts: ProjectKey[][];
} => {
const schema: ConfigJSON["schema"] = {};
const conflicts: { path: string; key: string }[][] = [];
const conflicts: ProjectKey[][] = [];

const uniqueKeys = keys.filter((keyObj, i, keys) => {
return keys.findIndex(i => i.key === keyObj.key) === i;
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ export interface Structure {
schema: { [key: string]: unknown };
updated_at: string;
}

export interface ProjectKey {
key: string;
file: string;
line: number;
column: number;
}

0 comments on commit 9e4d278

Please sign in to comment.