Skip to content

Commit

Permalink
feat(cli): add helpful message when scripts are not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu4k committed May 22, 2020
1 parent 4887d6b commit fe287d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
19 changes: 17 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
bold,
gray,
blue,
reset,
setColorEnabled,
grant,
exists,
} from "../deps.ts";

import { DenonConfig, writeConfig } from "./config.ts";
import { DenonConfig, writeConfig, getConfigFilename } from "./config.ts";
import { Runner } from "./runner.ts";

/**
Expand Down Expand Up @@ -41,7 +42,7 @@ const PERMISSION_OPTIONAL: { [key: string]: Deno.PermissionDescriptor[] } = {

export async function grantPermissions() {
// @see PERMISSIONS .
let permissions = await grant(PERMISSIONS);
let permissions = await grant([...PERMISSIONS]);
if (!permissions || permissions.length < 2) {
log.critical("Required permissions `read` and `run` not granted");
Deno.exit(1);
Expand Down Expand Up @@ -95,6 +96,20 @@ export function printAvailableScripts(config: DenonConfig) {
console.log(
`You can run scripts with \`${blue("denon")} ${yellow("<script>")}\``,
);
} else {
log.error("It looks like you don't have any scripts...");
const config = getConfigFilename();
if (config) {
log.info(
`You can add scripts to your \`${config}\` file. Check the docs.`,
);
} else {
log.info(
`You can create a config to add scripts to with \`${blue("denon")} ${
yellow("--init")
}${reset("\`.")}`,
);
}
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const DEFAULT_DENON_CONFIG: DenonConfig = {
scripts: {},
watcher: {
interval: 350,
paths: [Deno.cwd()],
paths: [],
exts: ["ts", "js", "json"],
match: ["*.*"],
skip: ["**/.git/**"],
Expand Down Expand Up @@ -82,15 +82,23 @@ export function cleanConfig(
return config;
}

/**
* Returns, if exists, the config filename
*/
export function getConfigFilename(): string | undefined {
return configs.find((filename) => {
return existsSync(filename);
});
}

/**
* Reads the denon config from a file
*/
export async function readConfig(
file: string | undefined = configs.find((filename) => {
return existsSync(filename);
}),
file: string | undefined = getConfigFilename(),
): Promise<DenonConfig> {
let config: DenonConfig = DEFAULT_DENON_CONFIG;
config.watcher.paths.push(Deno.cwd());

if (file) {
try {
Expand Down

0 comments on commit fe287d8

Please sign in to comment.