From 0005bc0439d09a61adf40dc63aac9bd33054c056 Mon Sep 17 00:00:00 2001 From: Andreas Hippler Date: Sat, 11 Apr 2020 13:38:55 +0200 Subject: [PATCH] feat: check for executable config and directory --- denon.ts | 61 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/denon.ts b/denon.ts index ff88556..64015b8 100644 --- a/denon.ts +++ b/denon.ts @@ -190,8 +190,13 @@ if (import.meta.main) { if (!(await exists(file))) { fail(`Could not start denon because file "${file}" does not exist`); } + const filepath = resolve(file); + const fileInfo = await Deno.lstat(filepath); + if (fileInfo.isDirectory()) { + fail(`Could not start denon because "${file}" is a directory`); + } - config.files.push(resolve(file)); + config.files.push(filepath); } // Remove duplicates @@ -213,10 +218,7 @@ if (import.meta.main) { config.watch = [...new Set(config.watch)]; debug(`Paths: ${config.watch}`); - const executors: { - [extension: string]: { [file: string]: () => void }; - } = {}; - + const executors: (() => void)[] = []; const execute = (...args: string[]) => { let proc: Deno.Process | undefined; @@ -232,26 +234,29 @@ if (import.meta.main) { }; }; - for (const extension in config.execute) { - executors[extension] = {}; - const cmds = config.execute[extension]; - const binary = cmds[0]; - - for (const file of config.files) { - if (extname(file) === extension) { - executors[extension][file] = execute( - ...cmds, - ...(binary === "deno" ? flags.deno_args : []), - file, - ...flags.runnerFlags, - ); - - if (config.fullscreen) { - console.clear(); - } - - executors[extension][file](); + for (const file of config.files) { + const extension = extname(file); + const cmds = config.execute[extension] as string[] | undefined; + + if (cmds) { + const binary = cmds[0]; + + const executor = execute( + ...cmds, + ...(binary === "deno" ? flags.deno_args : []), + file, + ...flags.runnerFlags, + ); + + executors.push(executor); + + if (config.fullscreen) { + console.clear(); } + + executor(); + } else { + fail(`Can not run ${file}. No config for "${extension}" found`); } } @@ -287,12 +292,6 @@ if (import.meta.main) { debug(`File "${change.path}" was ${change.event}`); } - for (const extension in config.execute) { - for (const file of config.files) { - if (executors[extension][file]) { - executors[extension][file](); - } - } - } + executors.forEach((ex) => ex()); } }