Skip to content

Commit

Permalink
fix: merge pr #106
Browse files Browse the repository at this point in the history
closes #101
  • Loading branch information
eliassjogreen committed Oct 9, 2020
2 parents b42f47a + 470c2fe commit c1f2b85
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 58 deletions.
16 changes: 9 additions & 7 deletions denon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

import { log } from "./deps.ts";

import { Watcher, FileEvent } from "./src/watcher.ts";
import { FileEvent, Watcher } from "./src/watcher.ts";
import { Runner } from "./src/runner.ts";
import { Daemon } from "./src/daemon.ts";

import {
grantPermissions,
initializeConfig,
printAvailableScripts,
printHelp,
initializeConfig,
grantPermissions,
upgrade,
} from "./src/cli.ts";
import { readConfig, CompleteDenonConfig, reConfig } from "./src/config.ts";
import { CompleteDenonConfig, readConfig, reConfig } from "./src/config.ts";
import { parseArgs } from "./src/args.ts";

import { VERSION, BRANCH } from "./info.ts";
import { BRANCH, VERSION } from "./info.ts";

const logger = log.create("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)) {
* for await (const event of denon.run(script)) {
* // event handling here
* }
* ``` */
Expand Down Expand Up @@ -122,6 +122,8 @@ if (import.meta.main) {

let config = await readConfig(args.config);

await log.setup({ filter: config.logger.debug ? "DEBUG" : "INFO" });

// autocomplete(config);

config.args = args;
Expand Down Expand Up @@ -159,7 +161,7 @@ if (import.meta.main) {
const denon = new Denon(config);

// TODO(@qu4k): events
for await (let event of denon.run(script)) {
for await (const event of denon.run(script)) {
if (event.type === "reload") {
if (
event.change.some(
Expand Down
26 changes: 13 additions & 13 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@ export * as log from "https://deno.land/x/branch@0.1.2/mod.ts";

// colors for a pretty cli
export {
setColorEnabled,
reset,
bold,
blue,
bold,
gray,
green,
yellow,
italic,
red,
gray,
} from "https://deno.land/std@0.71.0/fmt/colors.ts";
reset,
setColorEnabled,
yellow,
} from "https://deno.land/std@0.74.0/fmt/colors.ts";

// configuration reading
export {
exists,
existsSync,
walk, // ... and one type of file monitoring
} from "https://deno.land/std@0.71.0/fs/mod.ts";
} from "https://deno.land/std@0.74.0/fs/mod.ts";

// configuration parsing (YAML)
export {
JSON_SCHEMA,
parse as parseYaml,
} from "https://deno.land/std@0.71.0/encoding/yaml.ts";
} from "https://deno.land/std@0.74.0/encoding/yaml.ts";

// file watching and directory matching
export {
relative,
dirname,
extname,
resolve,
globToRegExp,
} from "https://deno.land/std@0.71.0/path/mod.ts";
relative,
resolve,
} from "https://deno.land/std@0.74.0/path/mod.ts";

// event control
export { deferred, delay } from "https://deno.land/std@0.71.0/async/mod.ts";
export { deferred, delay } from "https://deno.land/std@0.74.0/async/mod.ts";

// permission management
export { grant } from "https://deno.land/std@0.71.0/permissions/mod.ts";
export { grant } from "https://deno.land/std@0.74.0/permissions/mod.ts";
4 changes: 4 additions & 0 deletions examples/issue_101/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { bar } from "./src/foo.ts";

console.log("Hello from main!");
console.log(bar);
15 changes: 15 additions & 0 deletions examples/issue_101/scripts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://deno.land/x/denon/schema.json",
"scripts": {
"start": {
"cmd": "deno run ./main.ts"
}
},
"watcher": {
"match": ["src/**/*.ts", "./main.ts"]
},
"logger": {
"debug": true,
"fullscreen": true
}
}
1 change: 1 addition & 0 deletions examples/issue_101/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const bar = "Hello from foo!";
4 changes: 2 additions & 2 deletions examples/oak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { green } from "https://deno.land/std/fmt/colors.ts";

const app = new Application();

let PORT = 8000;
const PORT = 8000;

let port = Deno.env.get("PORT");
const port = Deno.env.get("PORT");
if (port) {
PORT = Number.parseInt(port);
}
Expand Down
20 changes: 10 additions & 10 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright 2020-present the denosaurs team. All rights reserved. MIT license.

import {
yellow,
blue,
bold,
exists,
grant,
gray,
blue,
log,
reset,
setColorEnabled,
grant,
exists,
log,
yellow,
} from "../deps.ts";

import {
writeConfigTemplate,
getConfigFilename,
CompleteDenonConfig,
getConfigFilename,
writeConfigTemplate,
} from "./config.ts";
import { Runner } from "./runner.ts";

Expand Down Expand Up @@ -53,7 +53,7 @@ export const PERMISSION_OPTIONAL: {

export async function grantPermissions(): Promise<void> {
// @see PERMISSIONS .
let permissions = await grant([...PERMISSIONS]);
const permissions = await grant([...PERMISSIONS]);
if (!permissions || permissions.length < PERMISSIONS.length) {
logger.critical("Required permissions `read` and `run` not granted");
Deno.exit(1);
Expand All @@ -62,7 +62,7 @@ export async function grantPermissions(): Promise<void> {
/** Create configuration file in the root of current work directory.
* // TODO: make it interactive */
export async function initializeConfig(type = "json"): Promise<void> {
let permissions = await grant(PERMISSION_OPTIONAL.initializeConfig);
const permissions = await grant(PERMISSION_OPTIONAL.initializeConfig);
if (
!permissions ||
permissions.length < PERMISSION_OPTIONAL.initializeConfig.length
Expand Down Expand Up @@ -146,7 +146,7 @@ export async function printAvailableScripts(
console.log(` ${script.desc}`);
}

let commands = runner
const commands = runner
.build(name)
.map((command) => command.cmd.join(" "))
.join(bold(" && "));
Expand Down
38 changes: 30 additions & 8 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import {
existsSync,
extname,
globToRegExp,
JSON_SCHEMA,
log,
parseYaml,
resolve,
globToRegExp,
log,
} from "../deps.ts";

import type { Args } from "./args.ts";
Expand Down Expand Up @@ -49,6 +49,11 @@ export interface CompleteDenonConfig extends RunnerConfig {
[key: string]: unknown;
watcher: WatcherConfig;
args?: Args;
logger: {
quiet: boolean;
debug: boolean;
fullscreen: boolean;
};
configPath: string;
}

Expand All @@ -63,7 +68,11 @@ export const DEFAULT_DENON_CONFIG: CompleteDenonConfig = {
skip: ["**/.git/**"],
},
watch: true,
logger: {},
logger: {
quiet: false,
debug: false,
fullscreen: false,
},
configPath: "",
};

Expand All @@ -87,7 +96,6 @@ async function importConfig(
file: string,
): Promise<Partial<DenonConfig> | undefined> {
try {
// deno-lint-ignore no-undef
const configRaw = await import(`file://${resolve(file)}`);
return configRaw.default as Partial<DenonConfig>;
} catch (error) {
Expand All @@ -101,10 +109,24 @@ function cleanConfig(
config: Partial<DenonConfig>,
file?: string,
): Partial<DenonConfig> {
if (config.watcher && config.watcher.exts) {
config.watcher.exts = config.watcher.exts.map((_) =>
_.startsWith(".") ? _.substr(0) : _
);
if (config.watcher) {
if (config.watcher.exts) {
config.watcher.exts = config.watcher.exts.map((_) =>
_.startsWith(".") ? _.substr(1) : _
);
}

if (config.watcher.skip) {
config.watcher.skip = config.watcher.skip.map((_) =>
_.startsWith("./") ? _.substr(2) : _
);
}

if (config.watcher.match) {
config.watcher.match = config.watcher.match.map((_) =>
_.startsWith("./") ? _.substr(2) : _
);
}
}
if (file) {
config.configPath = resolve(file);
Expand Down
12 changes: 8 additions & 4 deletions src/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class Daemon implements AsyncIterable<DenonEvent> {
private async reload(): Promise<void> {
logger.info("restarting due to changes...");

if (this.#config.logger.fullscreen) {
console.clear();
}

this.killAll();

await this.start();
Expand Down Expand Up @@ -59,7 +63,7 @@ export class Daemon implements AsyncIterable<DenonEvent> {
plog.info(`starting sequential \`${command.cmd.join(" ")}\``);
}

let process = command.exe();
const process = command.exe();
plog.debug(`starting process with pid ${process.pid}`);

if (last) {
Expand All @@ -79,9 +83,9 @@ export class Daemon implements AsyncIterable<DenonEvent> {
`killing ${Object.keys(this.#processes).length} orphan process[es]`,
);
// kill all processes spawned
let pcopy = Object.assign({}, this.#processes);
const pcopy = Object.assign({}, this.#processes);
this.#processes = {};
for (let id in pcopy) {
for (const id in pcopy) {
const p = pcopy[id];
if (Deno.build.os === "windows") {
logger.debug(`closing (windows) process with pid ${p.pid}`);
Expand All @@ -107,7 +111,7 @@ export class Daemon implements AsyncIterable<DenonEvent> {
} catch (error) {
logger.debug(`error getting status of process with pid ${process.pid}`);
}
let p = this.#processes[pid];
const p = this.#processes[pid];
if (p) {
logger.debug(`process with pid ${process.pid} exited on its own`);
// process exited on its own, so we should wait a reload
Expand Down
2 changes: 1 addition & 1 deletion src/runner.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2020-present the denosaurs team. All rights reserved. MIT license.

import { RunnerConfig, Runner } from "./runner.ts";
import { Runner, RunnerConfig } from "./runner.ts";
import { assertEquals } from "../test_deps.ts";

Deno.test({
Expand Down
8 changes: 4 additions & 4 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class Runner {
args: string[],
): Command[] {
if (typeof script === "object") {
let options = Object.assign({}, merge(global, script));
const options = Object.assign({}, merge(global, script));
return this.buildStringCommands(script.cmd, options, args);
}

Expand All @@ -82,7 +82,7 @@ export class Runner {
args: string[],
): Command[] {
if (script.includes("&&")) {
let commands: Command[] = [];
const commands: Command[] = [];
script.split("&&").map((s) => {
commands.push(this.buildCommand(s, global, args));
});
Expand All @@ -99,7 +99,7 @@ export class Runner {
): Command {
let out: string[] = [];
cmd = stdCmd(cmd).join(" ");
let denoAction = reDenoAction.exec(cmd);
const denoAction = reDenoAction.exec(cmd);
if (denoAction && denoAction.length === 3) {
const action = denoAction[1];
const args = denoAction[2];
Expand Down Expand Up @@ -141,7 +141,7 @@ export class Runner {
}
}

let args = this.#args.slice(1);
const args = this.#args.slice(1);

let commands: Command[] = [];

Expand Down
2 changes: 1 addition & 1 deletion src/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface ScriptOptions {
/** Build deno flags from ScriptOptions.
* `{ allow: [ run, env ]}` -> `[--allow-run, --allow-env]` */
export function buildFlags(options: ScriptOptions): string[] {
let flags: string[] = [];
const flags: string[] = [];
if (options.allow) {
if (Array.isArray(options.allow)) {
options.allow.forEach((flag) => flags.push(`--allow-${flag}`));
Expand Down
Loading

0 comments on commit c1f2b85

Please sign in to comment.