-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add `deno` entry * chore: rename `index.ts` -> `async.ts` * feat: add `deno/sync` entry * chore: add jsdocs * chore: add "deno" section to readme
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { dirname, resolve } from 'https://deno.land/std/path/mod.ts' | ||
|
||
type Promisable<T> = T | Promise<T>; | ||
export type Callback = (directory: string, files: string[]) => Promisable<string | false | void> | ||
|
||
async function toItems(dir: string) { | ||
let list = []; | ||
for await (let tmp of Deno.readDir(dir)) { | ||
list.push(tmp.name); | ||
} | ||
return list; | ||
} | ||
|
||
/** Requires `allow-read` permission. */ | ||
export default async function (start: string, callback: Callback) { | ||
let dir = resolve('.', start); | ||
let stats = await Deno.stat(dir); | ||
|
||
if (!stats.isDirectory) { | ||
dir = dirname(dir); | ||
} | ||
|
||
while (true) { | ||
let tmp = await callback(dir, await toItems(dir)); | ||
if (tmp) return resolve(dir, tmp); | ||
dir = dirname(tmp = dir); | ||
if (tmp === dir) break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { dirname, resolve } from 'https://deno.land/std/path/mod.ts' | ||
|
||
export type Callback = (directory: string, files: string[]) => string | false | void; | ||
|
||
function toItems(dir: string) { | ||
let list = []; | ||
for (let tmp of Deno.readDirSync(dir)) { | ||
list.push(tmp.name); | ||
} | ||
return list; | ||
} | ||
|
||
/** Requires `allow-read` permission. */ | ||
export default function (start: string, callback: Callback) { | ||
let dir = resolve('.', start); | ||
let stats = Deno.statSync(dir); | ||
|
||
if (!stats.isDirectory) { | ||
dir = dirname(dir); | ||
} | ||
|
||
while (true) { | ||
let tmp = callback(dir, toItems(dir)); | ||
if (tmp) return resolve(dir, tmp); | ||
dir = dirname(tmp = dir); | ||
if (tmp === dir) break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters