Skip to content

Commit

Permalink
feat: add custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu4k committed Jul 25, 2020
1 parent a5f573f commit c3d22cc
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 404 deletions.
17 changes: 10 additions & 7 deletions denon.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
48 changes: 20 additions & 28 deletions denon.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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");
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export {
reset,
bold,
blue,
green,
yellow,
italic,
red,
gray,
} from "https://deno.land/std@0.61.0/fmt/colors.ts";
Expand Down
9 changes: 2 additions & 7 deletions examples/denon.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
6 changes: 1 addition & 5 deletions examples/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 6 additions & 17 deletions src/args.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit c3d22cc

Please sign in to comment.