From c3d22ccb1898bd396f04444a4584e848845f541b Mon Sep 17 00:00:00 2001 From: Qu4k Date: Sat, 25 Jul 2020 23:51:17 +0200 Subject: [PATCH] feat: add custom logger --- denon.config.ts | 17 +++-- denon.ts | 48 +++++------- deps.ts | 2 + examples/denon.config.ts | 9 +-- examples/simple.ts | 6 +- src/args.ts | 23 ++---- src/cli.ts | 94 ++++++++++------------- src/config.ts | 60 ++++++--------- src/daemon.ts | 65 ++++++++-------- src/log.ts | 157 +++++++++++++++++++++++++++++---------- src/merge.ts | 6 +- src/runner.ts | 63 +++++++--------- src/runner_test.ts | 21 ++---- src/scripts.ts | 118 ++++++++++------------------- src/scripts_test.ts | 16 ++-- src/watcher.ts | 55 ++++++-------- src/watcher_test.ts | 7 +- 17 files changed, 363 insertions(+), 404 deletions(-) diff --git a/denon.config.ts b/denon.config.ts index 0b1e2a8..03c9be6 100644 --- a/denon.config.ts +++ b/denon.config.ts @@ -5,19 +5,22 @@ const config: DenonConfig = { test: [ { cmd: "deno fmt", - desc: "run denon format", + desc: "format code", + }, + { + cmd: "deno lint --unstable", + desc: "lint code", }, { cmd: "deno test", - desc: "run denon format", - allow: [ - "all", - ], - unstable: true, - watch: false, + desc: "test code", + allow: "all", }, ], }, + logger: { + debug: true, + }, }; export default config; diff --git a/denon.ts b/denon.ts index 4bf0bff..ec97ab8 100644 --- a/denon.ts +++ b/denon.ts @@ -1,7 +1,5 @@ // Copyright 2020-present the denosaurs team. All rights reserved. MIT license. -import { log } from "./deps.ts"; - import { Watcher, FileEvent } from "./src/watcher.ts"; import { Runner } from "./src/runner.ts"; import { Daemon } from "./src/daemon.ts"; @@ -14,27 +12,23 @@ import { upgrade, autocomplete, } from "./src/cli.ts"; -import { - readConfig, - CompleteDenonConfig, - reConfig, -} from "./src/config.ts"; +import { readConfig, CompleteDenonConfig, reConfig } from "./src/config.ts"; import { parseArgs } from "./src/args.ts"; -import { setupLog } from "./src/log.ts"; +import log from "./src/log.ts"; export const VERSION = "v2.2.1"; export const BRANCH = "master"; -/** - * Events you can listen to when creating a `denon` +const logger = log.prefix("main"); + +/** Events you can listen to when creating a `denon` * instance as module: * ```typescript * const denon = new Denon(config); * for await (let event of denon.run(script)) { * // event handling here * } - * ``` - */ + * ``` */ export declare type DenonEventType = | "start" | "reload" @@ -72,11 +66,9 @@ export declare interface DenonExitEvent { type: "exit"; } -/** - * Denon instance. +/** Denon instance. * Holds loaded configuration and handles creation - * of daemons with the `start(script)` method. - */ + * of daemons with the `start(script)` method. */ export class Denon { watcher: Watcher; runner: Runner; @@ -91,20 +83,18 @@ export class Denon { } } -/** - * CLI starts here, +/** CLI starts here, * other than the awesome `denon` cli this is an - * example on how the library should be used if - * included as a module. - */ + * example on how the library could be used if + * included as a module. */ if (import.meta.main) { - await setupLog(); + await log.setup(); await grantPermissions(); const args = parseArgs(Deno.args); let config = await readConfig(args.config); - await setupLog(config.logger); + await log.setup(config.logger); autocomplete(config); @@ -117,7 +107,7 @@ if (import.meta.main) { } // show version number. - log.info(VERSION); + logger.info(`${VERSION}-${BRANCH}`); if (args.version) Deno.exit(0); // update denon to latest release @@ -144,22 +134,24 @@ if (import.meta.main) { if (config.logger.fullscreen) console.clear(); + const conf = log.prefix("conf"); if (config.watcher.match) { - log.info(`watching path(s): ${config.watcher.match.join(" ")}`); + conf.info(`watching path(s): ${config.watcher.match.join(" ")}`); } if (config.watcher.exts) { - log.info(`watching extensions: ${config.watcher.exts.join(",")}`); + conf.info(`watching extensions: ${config.watcher.exts.join(",")}`); } // TODO(@qu4k): events for await (let event of denon.run(script)) { if (event.type === "reload") { if ( - event.change.some((_) => - reConfig.test(_.path) && _.path === config.configPath + event.change.some( + (_) => reConfig.test(_.path) && _.path === config.configPath, ) ) { config = await readConfig(args.config); + logger.debug("reloading config"); } } } diff --git a/deps.ts b/deps.ts index 0dbf1b9..16f7c05 100644 --- a/deps.ts +++ b/deps.ts @@ -15,7 +15,9 @@ export { reset, bold, blue, + green, yellow, + italic, red, gray, } from "https://deno.land/std@0.61.0/fmt/colors.ts"; diff --git a/examples/denon.config.ts b/examples/denon.config.ts index f27504f..5d19f1a 100644 --- a/examples/denon.config.ts +++ b/examples/denon.config.ts @@ -6,17 +6,12 @@ const config: DenonConfig = { run: { cmd: "simple.ts", desc: "Run main app", - allow: [ - "env", - ], + allow: ["env"], }, oak: { cmd: "oak.ts", desc: "Run oak instance", - allow: [ - "env", - "net", - ], + allow: ["env", "net"], env: { PORT: "9001", }, diff --git a/examples/simple.ts b/examples/simple.ts index 432c934..3406ad1 100644 --- a/examples/simple.ts +++ b/examples/simple.ts @@ -9,11 +9,7 @@ console.log( Deno.env.get("SECRET_ENV_VARIABLE"), ); -console.log( - Deno.pid, - green("args:"), - Deno.args, -); +console.log(Deno.pid, green("args:"), Deno.args); let i = 50; diff --git a/src/args.ts b/src/args.ts index 15ea76f..9c0b177 100644 --- a/src/args.ts +++ b/src/args.ts @@ -1,16 +1,10 @@ // Copyright 2020-present the denosaurs team. All rights reserved. MIT license. -import { reConfig } from "./config.ts"; - -/** - * Regex to test if string matches version format - */ +/** Regex to test if string matches version format */ const reVersion = /^v?[0-9]+\.[0-9]+\.[0-9]+$/; -/** - * Map of supported flags that modify - * `denon` behavior. - */ +/** Map of supported flags that modify + * `denon` behavior. */ export interface Args { help: boolean; version: boolean; @@ -22,10 +16,8 @@ export interface Args { cmd: string[]; } -/** - * Parse Deno.args into a flag map (`Args`) - * to be handled by th CLI. - */ +/** Parse Deno.args into a flag map (`Args`) + * to be handled by th CLI. */ export function parseArgs(args: string[] = Deno.args): Args { if (args[0] === "--") { args = args.slice(1); @@ -40,10 +32,7 @@ export function parseArgs(args: string[] = Deno.args): Args { cmd: [], }; - if ( - (args.includes("--config") || args.includes("-c")) && - args.length > 1 - ) { + if ((args.includes("--config") || args.includes("-c")) && args.length > 1) { flags.config = args[1]; args = args.slice(2); } diff --git a/src/cli.ts b/src/cli.ts index 1d11334..770eca7 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,7 +1,6 @@ // Copyright 2020-present the denosaurs team. All rights reserved. MIT license. import { - log, yellow, bold, gray, @@ -11,7 +10,6 @@ import { grant, exists, omelette, - red, } from "../deps.ts"; import { @@ -21,10 +19,12 @@ import { } from "./config.ts"; import { Runner } from "./runner.ts"; import { VERSION } from "../denon.ts"; -import { Watcher } from "./watcher.ts"; -/** - * These are the permissions required for a clean run +import log from "./log.ts"; + +const logger = log.prefix("main"); + +/** These are the permissions required for a clean run * of `denon`. If not provided through installation they * will be asked on every run by the `grant()` std function. * @@ -35,8 +35,7 @@ import { Watcher } from "./watcher.ts"; * - *write*, write configuration templates. * - *run*, used to run scripts as child processes. * - *write*, download configuration templates and import - * `denon.config.ts` file. - */ + * `denon.config.ts` file. */ export const PERMISSIONS: Deno.PermissionDescriptor[] = [ { name: "read" }, { name: "write" }, @@ -44,11 +43,9 @@ export const PERMISSIONS: Deno.PermissionDescriptor[] = [ { name: "net" }, ]; -/** - * These permissions are required on specific situations, +/** These permissions are required on specific situations, * `denon` should not be installed with this permissions - * but you should be granting them when they are required. - */ + * but you should be granting them when they are required. */ export const PERMISSION_OPTIONAL: { [key: string]: Deno.PermissionDescriptor[]; } = { @@ -60,41 +57,36 @@ export async function grantPermissions(): Promise { // @see PERMISSIONS . let permissions = await grant([...PERMISSIONS]); if (!permissions || permissions.length < PERMISSIONS.length) { - log.critical("Required permissions `read` and `run` not granted"); + logger.critical("Required permissions `read` and `run` not granted"); Deno.exit(1); } } - -/** - * Create configuration file in the root of current work directory. - * // TODO: make it interactive - */ +/** Create configuration file in the root of current work directory. + * // TODO: make it interactive */ export async function initializeConfig(template: string): Promise { let permissions = await grant(PERMISSION_OPTIONAL.initializeConfig); if ( !permissions || permissions.length < PERMISSION_OPTIONAL.initializeConfig.length ) { - log.critical("Required permissions for this operation not granted"); + logger.critical("Required permissions for this operation not granted"); Deno.exit(1); } - if (!await exists(template)) { + if (!(await exists(template))) { await writeConfigTemplate(template); } else { - log.error(`\`${template}\` already exists in root dir`); + logger.error(`\`${template}\` already exists in root dir`); } } -/** - * Grab a fresh copy of denon - */ +/** Grab a fresh copy of denon */ export async function upgrade(version?: string): Promise { const url = `https://deno.land/x/denon${ version ? `@${version}` : "" }/denon.ts`; if (version === VERSION) { - log.info(`Version ${version} already installed`); + logger.info(`Version ${version} already installed`); Deno.exit(0); } @@ -103,19 +95,17 @@ export async function upgrade(version?: string): Promise { !permissions || permissions.length < PERMISSION_OPTIONAL.upgradeExe.length ) { - log.critical("Required permissions for this operation not granted"); + logger.critical("Required permissions for this operation not granted"); Deno.exit(1); } - log.debug(`Checking if ${url} exists`); + logger.debug(`Checking if ${url} exists`); if ((await fetch(url)).status !== 200) { - log.critical(`Upgrade url ${url} does not exist`); + logger.critical(`Upgrade url ${url} does not exist`); Deno.exit(1); } - log.info( - `Running \`deno install -Af --unstable ${url}\``, - ); + logger.info(`Running \`deno install -Af --unstable ${url}\``); await Deno.run({ cmd: [ "deno", @@ -132,9 +122,7 @@ export async function upgrade(version?: string): Promise { Deno.exit(0); } -/** - * Generate autocomplete suggestions - */ +/** Generate autocomplete suggestions */ export function autocomplete(config: CompleteDenonConfig): void { // Write your CLI template. const completion = omelette.default(`denon