-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): adds children to core entry
- Loading branch information
Showing
3 changed files
with
60 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} |