Skip to content

Commit

Permalink
Async generator for readdir
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Aug 5, 2022
1 parent de3a4fb commit 8d92750
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"deno.enable": true,
"deno.unstable": true
"deno.enable": true
}
43 changes: 26 additions & 17 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,38 @@ import { Command } from "https://deno.land/x/[email protected]/command/mod.ts";
import { basename, join } from "https://deno.land/[email protected]/path/mod.ts";
import { findAndReplace } from "./change.ts";

export async function recursiveReaddir(path: string, ignore: string[]) {
const files: string[] = [];
const getFiles = async (path: string) => {
for await (const dirEntry of Deno.readDir(path)) {
if (ignore.includes(dirEntry.name)) continue;
if (dirEntry.isDirectory) {
await getFiles(join(path, dirEntry.name));
} else if (dirEntry.isFile) {
files.push(join(path, dirEntry.name));
}
export async function* recursiveReaddir(
path: string,
ignore: string[] = [],
): AsyncGenerator<string, void> {
for await (const dirEntry of Deno.readDir(path)) {
if (ignore.includes(dirEntry.name)) continue;

if (dirEntry.isDirectory) {
yield* recursiveReaddir(join(path, dirEntry.name), ignore);
} else if (dirEntry.isFile) {
yield join(path, dirEntry.name);
}
};
await getFiles(path);
return files;
}
}

async function update(
quiet: boolean,
ignore: string[] = [],
lineIgnore: string,
debug = false,
) {
let count = 0;
// TODO .gitignore
const files = await recursiveReaddir(Deno.cwd(), [...ignore, ".git"]);
for (const file of files) {
if (ignore.includes(basename(file))) continue;
for await (const file of recursiveReaddir(Deno.cwd(), [...ignore, ".git"])) {
if (debug) console.log(`Scanning ${file}`);

if (ignore.includes(basename(file))) {
if (debug) console.log(`Ignoring ${file}`);
continue;
}

if (debug) console.log(`Attempting to update ${file}`);
const originalSource = await Deno.readTextFile(file);
const newSource = await findAndReplace(originalSource, lineIgnore);

Expand All @@ -50,6 +55,9 @@ async function update(
await new Command()
.name("deno-outdated")
.version("0.0.1")
.option("-d --debug", "Show all scanned files", {
default: false,
})
.option("-q --quiet", "Silence any output", {
default: false,
})
Expand All @@ -66,11 +74,12 @@ await new Command()
.description(
"Check for outdated dependencies for deno.land/x and other various 3rd party vendors",
)
.action(async ({ quiet, ignore, lineIgnore }) => {
.action(async ({ quiet, ignore, lineIgnore, debug }) => {
const count = await update(
quiet,
Array.isArray(ignore) ? ignore : [],
typeof lineIgnore === "string" ? lineIgnore : "i-deno-outdated",
debug,
);
if (!quiet) console.log(`Updated ${count} file${count === 1 ? "" : "s"}`);
})
Expand Down

0 comments on commit 8d92750

Please sign in to comment.