Skip to content

Commit

Permalink
feat(core): adds children to core entry
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 27, 2019
1 parent e233dcf commit 95bda68
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 55 deletions.
12 changes: 9 additions & 3 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import _cache from '~/utils/cache';
import options, { raw } from './options';
import { getSelfPaths, getRootPaths } from './paths';
import load from './load';
import setScope from './scope';
import { setScope, getChildren } from './scope';
import { getTask, getAllTasks } from './tasks';
import { IPaths, ILoaded, ITask, ITasks } from './types';
import { IPaths, ILoaded, ITask, ITasks, IChild } from './types';
import getBin from './bin';
import exec from './exec';
import run from './run';
Expand Down Expand Up @@ -50,6 +50,12 @@ const core = {
root: await core.get('root')
});
}),
children: cache(async function(): Promise<IChild[]> {
const paths = await core.paths();
const children = await core.get('children');

return getChildren(paths.directory, children);
}),
load: cache(async function(): Promise<ILoaded> {
return load(await core.paths());
}),
Expand Down Expand Up @@ -104,7 +110,7 @@ const core = {
const { next, scope } = await setScope(
names,
{ self: paths.directory, root: root ? root.directory : undefined },
await core.get('children')
await core.children()
);
if (scope) {
logger.debug(`${scope.name} scope set`);
Expand Down
54 changes: 2 additions & 52 deletions src/core/scope/index.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,2 @@
import logger from '~/utils/logger';
import getChildren from './children';
import { IScope } from '../types';
import { TChildrenDefinition } from '~/types';

export default async function setScope(
scopes: string[],
directories: { self: string; root?: string },
definitions?: TChildrenDefinition
): Promise<{ next: string[]; scope?: IScope }> {
if (!scopes.length) return { next: [] };

const name = scopes[0];
const next = scopes.slice(1);

// root scope
if (name === 'root') {
if (directories.root) {
return { next, scope: { name: 'root', directory: directories.root } };
} else {
logger.debug('root scope was not found and was assigned to self');
return { next };
}
}

// child scopes
const children = await getChildren(directories.self, definitions);
const matches = children
.filter((child) => child.matcher(name))
.map((child) => child.directory);

if (matches.length) {
logger.debug(`scopes found for ${name}:\n${matches.join('\n')}`);

if (matches.length > 1) {
throw Error(`Several scopes matched name "${name}"`);
}

return { next, scope: { name, directory: matches[0] } };
}

// it was not found as child, try scaling up to root
if (directories.root) {
logger.debug(`scope ${name} not found, scaling to root scope`);
return {
next: next.concat(name),
scope: { name: 'root', directory: directories.root }
};
}

throw Error(`Scope "${name}" was not found`);
}
export { default as setScope } from './set';
export { default as getChildren } from './children';
49 changes: 49 additions & 0 deletions src/core/scope/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import logger from '~/utils/logger';
import { IScope, IChild } from '../types';

export default async function setScope(
scopes: string[],
directories: { self: string; root?: string },
children: IChild[]
): Promise<{ next: string[]; scope?: IScope }> {
if (!scopes.length) return { next: [] };

const name = scopes[0];
const next = scopes.slice(1);

// root scope
if (name === 'root') {
if (directories.root) {
return { next, scope: { name: 'root', directory: directories.root } };
} else {
logger.debug('root scope was not found and was assigned to self');
return { next };
}
}

// child scopes
const matches = children
.filter((child) => child.matcher(name))
.map((child) => child.directory);

if (matches.length) {
logger.debug(`scopes found for ${name}:\n${matches.join('\n')}`);

if (matches.length > 1) {
throw Error(`Several scopes matched name "${name}"`);
}

return { next, scope: { name, directory: matches[0] } };
}

// it was not found as child, try scaling up to root
if (directories.root) {
logger.debug(`scope ${name} not found, scaling to root scope`);
return {
next: next.concat(name),
scope: { name: 'root', directory: directories.root }
};
}

throw Error(`Scope "${name}" was not found`);
}

0 comments on commit 95bda68

Please sign in to comment.