From 48da81b9a735628c5b8503886b155d4cd6021de5 Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Sat, 11 May 2019 18:36:40 +0200 Subject: [PATCH] fix: fixes logging level restore; initializes on reset --- src/bin/main/index.ts | 3 ++- src/commands/raise.ts | 2 +- src/core/index.ts | 41 +++++++++++++++++++++------------------ src/core/merge-options.ts | 15 +++++++------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/bin/main/index.ts b/src/bin/main/index.ts index 52156ae..5781eb3 100644 --- a/src/bin/main/index.ts +++ b/src/bin/main/index.ts @@ -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 { const pkg = await loadPackage(__dirname, { title: true }); @@ -84,6 +84,7 @@ export default async function main(argv: string[]): Promise { return acc; }, {}) }; + if (options.log) setLevel(options.log); let first = cmd._.shift(); const scopes: string[] = []; diff --git a/src/commands/raise.ts b/src/commands/raise.ts index a4ce7ce..ebf1c31 100644 --- a/src/commands/raise.ts +++ b/src/commands/raise.ts @@ -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(); } diff --git a/src/core/index.ts b/src/core/index.ts index b428058..3bae097 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -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 = (fn: () => Promise): Promise => _lazy((resolve, reject) => @@ -53,10 +54,8 @@ export default async function contain( 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(); @@ -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; @@ -141,27 +140,31 @@ export function getCore(options: ICliOptions, nesting: ICore[] = []): ICore { return this.scope(['root'].concat(names)); }, async initialize(): Promise { - 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 { + Object.assign(this, getCore(options, parent)); + await this.initialize(); } }; } diff --git a/src/core/merge-options.ts b/src/core/merge-options.ts index 81220a0..21bea90 100644 --- a/src/core/merge-options.ts +++ b/src/core/merge-options.ts @@ -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 @@ -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);