From 51d34a3f21780bf83eea8f12d55804b141b385de Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Fri, 3 May 2019 05:36:10 +0200 Subject: [PATCH] fix(core/tasks): fixes path parsing and printing --- src/core/tasks/from-kpo.ts | 21 ++++++++++++++------- src/core/tasks/pure-path.ts | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/core/tasks/from-kpo.ts b/src/core/tasks/from-kpo.ts index 6be2b7c..c72ec72 100644 --- a/src/core/tasks/from-kpo.ts +++ b/src/core/tasks/from-kpo.ts @@ -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 { @@ -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 }; } @@ -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; } diff --git a/src/core/tasks/pure-path.ts b/src/core/tasks/pure-path.ts index ee69ddc..383d53d 100644 --- a/src/core/tasks/pure-path.ts +++ b/src/core/tasks/pure-path.ts @@ -1,3 +1,3 @@ export default function purePath(path: string): string { - return path.replace(/\.\$?default$/g, ''); + return path.replace(/\.\$?default\./g, '.').replace(/\.\$?default$/, ''); }