Skip to content

Commit

Permalink
fix: fixes logging level restore; initializes on reset
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 11, 2019
1 parent 41b3118 commit 48da81b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/bin/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import parallel from './parallel';
import list from './list';
import raise from './raise';
import stream from './stream';
import logger from '~/utils/logger';
import logger, { setLevel } from '~/utils/logger';

export default async function main(argv: string[]): Promise<void> {
const pkg = await loadPackage(__dirname, { title: true });
Expand Down Expand Up @@ -84,6 +84,7 @@ export default async function main(argv: string[]): Promise<void> {
return acc;
}, {})
};
if (options.log) setLevel(options.log);

let first = cmd._.shift();
const scopes: string[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/commands/raise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,5 @@ export default async function raise(

await fs.writeFile(paths.pkg, JSON.stringify(pkg, null, 2));
// As package.json has changed, we need to refetch on core
core.reset();
await core.reset();
}
41 changes: 22 additions & 19 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IPaths, ILoaded, IChild, ITasks, ITask } from './types';
import getBinPaths from '~/utils/paths';
import logger from '~/utils/logger';
import { SilentError } from '~/utils/errors';
import { KPO_LOG_ENV } from '~/constants';

const lazy = <T>(fn: () => Promise<T>): Promise<T> =>
_lazy((resolve, reject) =>
Expand Down Expand Up @@ -53,10 +54,8 @@ export default async function contain<T>(
return res;
}

export function getCore(options: ICliOptions, nesting: ICore[] = []): ICore {
const manager = nesting.length
? nesting[0].manager
: new EnvManager(process.env);
export function getCore(options: ICliOptions, parent?: ICore): ICore {
const manager = parent ? parent.manager : new EnvManager(process.env);
const cli = merge(manager, options);
const restoreLogger = setLogger(manager, cli);
const cwd = process.cwd();
Expand Down Expand Up @@ -131,7 +130,7 @@ export function getCore(options: ICliOptions, nesting: ICore[] = []): ICore {
if (scope) {
const core = await getCore(
{ ...options, file: null, directory: scope.directory },
nesting.concat(this)
this
).scope(names.slice(1));
await core.initialize();
return core;
Expand All @@ -141,27 +140,31 @@ export function getCore(options: ICliOptions, nesting: ICore[] = []): ICore {
return this.scope(['root'].concat(names));
},
async initialize(): Promise<void> {
const { paths, options } = await promise;
const bin = await this.bin;
this.restore();

const { paths, options: opts } = await promise;
const bin = await this.bin;
logger.debug(`Initializing scope with path: ${paths.directory}`);
setLogger(manager, options);

// Use cli options to set hard logging level
if (options.log) manager.set(KPO_LOG_ENV, options.log);

// Set environmentals
setLogger(manager, opts);
process.chdir(paths.directory);
if (options.env) manager.assign(options.env);
if (opts.env) manager.assign(opts.env);
if (bin.length) manager.addPaths(bin);
},
restore(): void {
if (nesting.length) {
nesting[0].restore();
} else {
restoreLogger();
manager.restore();
process.chdir(cwd);
}
if (parent) return parent.restore();

restoreLogger();
manager.restore();
process.chdir(cwd);
},
reset(): void {
const current = Object.assign({}, this);
Object.assign(this, getCore(options, nesting.concat(current)));
async reset(): Promise<void> {
Object.assign(this, getCore(options, parent));
await this.initialize();
}
};
}
15 changes: 8 additions & 7 deletions src/core/merge-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ export default function mergeOptions(
): TCoreOptions {
const options: TCoreOptions = {
...initial,
log: (manager.get(KPO_LOG_ENV) as TLogger) || initial.log,
...scope,
...stripUndefined(cli),
env: Object.assign({}, initial.env, scope.env, cli.env)
env: Object.assign({}, initial.env, scope.env, cli.env),
log:
cli.log ||
(manager.get(KPO_LOG_ENV) as TLogger) ||
scope.log ||
initial.log
};

// ensure cli own properties are of cli
Expand All @@ -49,12 +53,9 @@ export function setLogger(
options: TCoreOptions
): () => void {
const initial = logger.getLevel();
const level = (options.log ||
manager.get(KPO_LOG_ENV) ||
DEFAULT_LOG_LEVEL) as TLogger;

const level: TLogger =
options.log || (manager.get(KPO_LOG_ENV) as any) || DEFAULT_LOG_LEVEL;
setLevel(level);
manager.set(KPO_LOG_ENV, level);

return function restore() {
setLevel(initial as any);
Expand Down

0 comments on commit 48da81b

Please sign in to comment.