Skip to content

Commit

Permalink
fix(core/tasks): fixes path parsing and printing
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 3, 2019
1 parent 25282d1 commit 51d34a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/core/tasks/from-kpo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { IScripts } from '~/types';
import purePath from './pure-path';

export default function getFromKpo(path: string, kpo: IScripts): ITask {
const task = trunk(path.split('.'), kpo, '');
return { ...task, path: purePath(task.path.slice(1)) };
const task = trunk(purePath(path).split('.'), kpo, '');
return { ...task, path: purePath(task.path) };
}

export function trunk(arr: string[], obj: any, path: string): ITask {
Expand All @@ -18,7 +18,7 @@ export function trunk(arr: string[], obj: any, path: string): ITask {
const task = trunk(['default'], obj, path);
if (obj.hasOwnProperty('_description')) {
if (task.hasOwnProperty('description')) {
throw Error(`There are several descriptions for ${path}`);
throw Error(`There are several descriptions for ${purePath(path)}`);
}
return { ...task, description: obj._description };
}
Expand All @@ -29,20 +29,27 @@ export function trunk(arr: string[], obj: any, path: string): ITask {

const key = arr.shift() as string;
if (typeof obj !== 'object' || obj === null || obj instanceof Error) {
throw Error(`${path} is not an object`);
throw Error(`${purePath(path)} is not an object`);
}

const keys = key[0] === '$' ? [key] : [key, `$${key}`];
const props = keys.filter((key) => obj.hasOwnProperty(key));
if (props.length > 1) {
throw Error(
`There are several tasks matching ${path ? `${path}.${key}` : key}`
`There are several tasks matching ` +
purePath(path ? `${path}.${key}` : key)
);
}
if (!props.length) {
throw Error(`There is no task matching ${path ? `${path}.${key}` : key}`);
throw Error(
`There are no tasks matching ${purePath(path ? `${path}.${key}` : key)}`
);
}
const actualKey = props.shift() as string;
const task = trunk(arr, obj[actualKey], `${path}.${actualKey}`);
const task = trunk(
arr,
obj[actualKey],
path ? `${path}.${actualKey}` : actualKey
);
return actualKey[0] === '$' ? { ...task, hidden: true } : task;
}
2 changes: 1 addition & 1 deletion src/core/tasks/pure-path.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function purePath(path: string): string {
return path.replace(/\.\$?default$/g, '');
return path.replace(/\.\$?default\./g, '.').replace(/\.\$?default$/, '');
}

0 comments on commit 51d34a3

Please sign in to comment.