Skip to content

Commit

Permalink
fix(core/options): fixes setters; doesn't call initialize on set on g…
Browse files Browse the repository at this point in the history
…et; rolls back to using object
  • Loading branch information
rafamel committed May 7, 2019
1 parent b44f6a1 commit 05b6308
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@types/jest": "^24.0.12",
"@types/js-yaml": "^3.12.1",
"@types/loglevel": "^1.5.4",
"@types/object-hash": "^1.2.0",
"@types/pify": "^3.0.2",
"@types/prompts": "^2.4.0",
"@types/ps-tree": "^1.1.0",
Expand Down Expand Up @@ -126,6 +127,7 @@
"js-yaml": "^3.13.1",
"loglevel": "^1.6.1",
"manage-path": "^2.0.0",
"object-hash": "^1.3.1",
"path-key": "^3.1.0",
"pify": "^4.0.1",
"promist": "^0.5.3",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default async function main(argv: string[]): Promise<void> {
throw Error(`A command is required`);
}

await core.options.setCli({
core.options.setCli({
file: cmd['--file'],
directory: cmd['--dir'],
silent: cmd['--silent'],
Expand Down
2 changes: 1 addition & 1 deletion src/commands/raise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ function raise(options: IRaiseOptions = {}): () => Promise<void> {
};

await fs.writeFile(paths.pkg, JSON.stringify(pkg, null, 2));
await core.options.forceUpdate();
core.options.forceUpdate();
};
}
6 changes: 3 additions & 3 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const core = {
},
children: cache(null, async function(): Promise<IChild[]> {
const { paths } = await initialize();
const children = await core.options.get('children');
const children = core.options.get('children');

return getChildren(
{
Expand Down Expand Up @@ -72,9 +72,9 @@ const core = {
// keep track of scope branches
scopes = scopes.concat(scope.name);
// reset scope options
await core.options.resetScope(false);
core.options.resetScope();
// set current directory as the the one of the scope
await core.options.setCli({ file: null, directory: scope.directory });
core.options.setCli({ file: null, directory: scope.directory });
}
// Continue recursively
if (next.length) return core.setScope(next);
Expand Down
4 changes: 2 additions & 2 deletions src/core/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function requireLocal(file: string): Promise<IOfType<any>> {
export async function processKpo(
kpo: IOfType<any>
): Promise<IOfType<any> | null> {
if (kpo.options) await options.setScope(kpo.options);
if (kpo.options) options.setScope(kpo.options);

return kpo.scripts || null;
}
Expand All @@ -71,6 +71,6 @@ export async function processPkg(
opts.cwd = absolute({ path: opts.cwd, cwd: path.parse(file).dir });
}

await options.setScope(opts);
options.setScope(opts);
return pkg;
}
35 changes: 15 additions & 20 deletions src/core/options.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ICliOptions, IScopeOptions, TCoreOptions, IOfType } from '~/types';
import hash from 'object-hash';
import { DEFAULT_LOG_LEVEL } from '~/constants';
import { setLevel } from '~/utils/logger';
import environmentals from '~/utils/environmentals';
import initialize from './initialize';
import { ICliOptions, IScopeOptions, TCoreOptions, IOfType } from '~/types';

export const state = {
id: 0,
force: 0,
base: {
file: null,
directory: null,
Expand All @@ -17,39 +17,32 @@ export const state = {
scope: {} as IScopeOptions
};

let id = '';
let options: TCoreOptions = {};
merge();

export default {
get id(): number {
return state.id;
get id(): string {
return id;
},
get<T extends keyof TCoreOptions>(key: T): TCoreOptions[T] {
return options[key];
},
async setCli(opts: ICliOptions): Promise<void> {
setCli(opts: ICliOptions): void {
Object.assign(state.cli, stripUndefined(opts));
state.id += 1;
merge();
await initialize();
},
async setScope(opts: IScopeOptions = {}): Promise<void> {
setScope(opts: IScopeOptions = {}): void {
Object.assign(state.scope, opts);
state.id += 1;
merge();
await initialize();
},
async resetScope(update: boolean): Promise<void> {
resetScope(): void {
state.scope = {};
state.id += 1;
if (update) {
merge();
await initialize();
}
merge();
},
async forceUpdate(): Promise<void> {
state.id += 1;
await initialize();
forceUpdate(): void {
state.force += 1;
merge();
}
};

Expand All @@ -68,6 +61,8 @@ function merge(): void {
setLevel(options.log);
environmentals.set('kpo_log', options.log);
}

id = hash(options) + state.force;
}

function stripUndefined(obj: IOfType<any>): IOfType<any> {
Expand Down

0 comments on commit 05b6308

Please sign in to comment.