Skip to content

Commit

Permalink
feat(state): adds scope
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 23, 2019
1 parent 05c6d31 commit c691501
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/exposed/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import state from '~/state';
import { IScopeOptions } from '~/types';

export default function options(opts: IScopeOptions): void {
return state.scope(opts);
return state.setOptions(opts);
}
42 changes: 30 additions & 12 deletions src/state/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
import paths, { IPaths } from './paths';
import mergewith from 'lodash.mergewith';
import { IBaseOptions, IScopeOptions, TOptions } from '~/types';
import { IBaseOptions, IScopeOptions } from '~/types';
import { DEFAULT_LOG_LEVEL } from '~/constants';
import { setLevel } from '~/utils/logger';
import { lazy } from 'promist';
import load, { ILoaded } from './load';
import scope from './scope';

export const states = {
base: {
file: undefined,
directory: undefined,
file: null,
directory: null,
env: {},
silent: false,
log: DEFAULT_LOG_LEVEL
} as IBaseOptions,
scope: {} as IScopeOptions

options: {} as IScopeOptions,

internal: {
scopes: ['self'] as string[]
}
};

export type TState = IBaseOptions & IScopeOptions & typeof states.internal;

let promise: Promise<IPaths>;
let state: TOptions = {};
let state: TState = Object.assign({}, states.internal);
merge();

export default {
base(options: IBaseOptions): void {
setBase(options: IBaseOptions): void {
mergewith(states.base, options, (obj, src) => {
if (obj === 'undefined') return src;
});
merge();
},
scope(options: IScopeOptions): void {
states.scope = options;
setOptions(options: IScopeOptions = {}): void {
states.options = options;
merge();
},
get(key: keyof TOptions): any {
async setScope(name: string): Promise<void> {
const scopeName = await scope(name);
if (scopeName) states.internal.scopes.push(scopeName);
this.setOptions();
},
get(key: keyof TState): any {
return state[key];
},
paths(): Promise<IPaths> {
Expand All @@ -45,8 +58,8 @@ export default {

function merge(): void {
// merge base and scope
state = Object.assign({}, states.base, states.scope, {
env: Object.assign({}, states.base.env, states.scope.env)
state = Object.assign({}, states.base, states.options, states.internal, {
env: Object.assign({}, states.base.env, states.options.env)
});

// ensure base own properties are of base
Expand All @@ -58,6 +71,11 @@ function merge(): void {

// Set config lazy promise
promise = lazy((resolve) => {
resolve(paths({ file: state.file, directory: state.directory }));
resolve(
paths({
file: state.file || undefined,
directory: state.directory || undefined
})
);
});
}
23 changes: 23 additions & 0 deletions src/state/scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import state from './index';
import logger from '~/utils/logger';

export default async function setScope(scope: string): Promise<string | null> {
if (scope === 'self') return null;

const paths = await state.paths();
if (scope === 'root') {
if (paths.root) {
return set('root', paths.root.directory);
} else {
logger.debug('root scope was not found and was assigned to self');
return null;
}
}

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

export function set(name: string, path: string): string {
state.setBase({ file: null, directory: path });
return name;
}
6 changes: 2 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ export interface IScripts {
[key: string]: TScript | TScript[] | IScripts;
}

export type TOptions = IBaseOptions & IScopeOptions;

export interface ICoreOptions {
env?: IOfType<string>;
silent?: boolean;
log?: TLogger;
}

export interface IBaseOptions extends ICoreOptions {
file?: string;
directory?: string;
file?: string | null;
directory?: string | null;
}

export interface IScopeOptions extends ICoreOptions {
Expand Down

0 comments on commit c691501

Please sign in to comment.