Skip to content

Commit

Permalink
fix(core): ensures options have been loaded when requesting options; …
Browse files Browse the repository at this point in the history
…makes options part of core
  • Loading branch information
rafamel committed Apr 25, 2019
1 parent c3640b0 commit 492c737
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/bin/kpo.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/env node

import main from './main';
import options from '~/options';
import core from '~/core';
import { error } from 'cli-belt';
import logger from '~/utils/logger';
import { OpenError } from '~/utils/errors';

main(process.argv.slice(2)).catch((err) => {
main(process.argv.slice(2)).catch(async (err) => {
const isOpen = err instanceof OpenError;

return error(isOpen ? err.root : err, {
exit: options.get('silent') ? 0 : 1,
exit: (await core.get('silent')) ? 0 : 1,
debug: true,
logger: {
error: logger.error,
Expand Down
3 changes: 1 addition & 2 deletions src/bin/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { loadPackage, flags, safePairs } from 'cli-belt';
import { stripIndent as indent } from 'common-tags';
import arg from 'arg';
import chalk from 'chalk';
import options from '~/options';
import core from '~/core';
import core, { options } from '~/core';
import { TLogger, IOfType } from '~/types';
import run from '~/commands/run';
import logger from '~/utils/logger';
Expand Down
24 changes: 17 additions & 7 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import _cache from '~/utils/cache';
import options from '~/options';
import options, { raw } from './options';
import { getSelfPaths, getRootPaths } from './paths';
import load from './load';
import setScope from './scope';
import { IPaths, ILoaded } from './types';
import getBin from './bin';
import exec from './exec';
import logger from '~/utils/logger';
import { TCoreOptions } from '~/types';

export interface ICoreState {
scopes: string[];
Expand All @@ -21,17 +22,26 @@ const core = {
get state(): ICoreState {
return state;
},
async get<T extends keyof TCoreOptions>(key: T): Promise<TCoreOptions[T]> {
// we're ensuring we've loaded user options when
// any option is requested
await core.load();

return raw()[key];
},
paths: cache(async function(): Promise<IPaths> {
const options = raw();
return getSelfPaths({
file: options.get('file') || undefined,
directory: options.get('directory') || undefined
file: options.file || undefined,
directory: options.directory || undefined
});
}),
root: cache(async function(): Promise<IPaths | null> {
const paths = await core.paths();

return getRootPaths({
self: paths.directory,
root: options.get('root')
root: await core.get('root')
});
}),
load: cache(async function(): Promise<ILoaded> {
Expand All @@ -47,7 +57,7 @@ const core = {
async exec(command: string): Promise<void> {
const paths = await core.paths();
const bin = await core.bin();
const env = options.get('env');
const env = await core.get('env');
return exec(command, paths.directory, bin, env);
},
async setScope(names: string[]): Promise<void> {
Expand All @@ -57,7 +67,7 @@ const core = {
const { next, scope } = await setScope(
names,
{ self: paths.directory, root: root ? root.directory : undefined },
options.get('children')
await core.get('children')
);
if (scope) {
logger.debug(`${scope.name} scope set`);
Expand All @@ -73,4 +83,4 @@ const core = {
}
};

export default core;
export { core as default, options };
12 changes: 5 additions & 7 deletions src/options.ts → src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ let id = uuid();
let options: TCoreOptions = {};
merge();

export const raw = (): TCoreOptions => options;
export default {
get id(): string {
return id;
},
get<T extends keyof TCoreOptions>(key: T): TCoreOptions[T] {
return options[key];
},
setBase(options: IBaseOptions): void {
mergewith(state.base, options, (obj, src) => {
setBase(opts: IBaseOptions): void {
mergewith(state.base, opts, (obj, src) => {
if (obj === 'undefined') return src;
});
merge();
},
setScope(options: IScopeOptions = {}): void {
state.scope = options;
setScope(opts: IScopeOptions = {}): void {
state.scope = opts;
merge();
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/exposed/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _options from '~/options';
import { options as _options } from '~/core';
import { IScopeOptions } from '~/types';

export default function options(opts: IScopeOptions): void {
Expand Down

0 comments on commit 492c737

Please sign in to comment.