0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -05:00
deno/tools/util.ts
Kitson Kelly a21a5ad2fa Add Deno global namespace (#1748)
Resolves #1705

This PR adds the Deno APIs as a global namespace named `Deno`. For backwards
compatibility, the ability to `import * from "deno"` is preserved. I have tried
to convert every test and internal code the references the module to use the
namespace instead, but because I didn't break compatibility I am not sure.

On the REPL, `deno` no longer exists, replaced only with `Deno` to align with
the regular runtime.

The runtime type library includes both the namespace and module. This means it
duplicates the whole type information. When we remove the functionality from the
runtime, it will be a one line change to the library generator to remove the
module definition from the type library.

I marked a `TODO` in a couple places where to remove the `"deno"` module, but
there are additional places I know I didn't mark.
2019-02-12 10:08:56 -05:00

72 lines
1.7 KiB
TypeScript

// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { join } from "../js/deps/https/deno.land/x/std/fs/path/mod.ts";
const { platform, lstatSync, readDirSync } = Deno;
export interface FindOptions {
skip?: string[];
depth?: number;
}
/**
* Finds files of the give extensions under the given paths recursively.
* @param dirs directories
* @param exts extensions
* @param skip patterns to ignore
* @param depth depth to find
*/
export function findFiles(
dirs: string[],
exts: string[],
{ skip = [], depth = 20 }: FindOptions = {}
) {
return findFilesWalk(dirs, depth).filter(
path =>
exts.some(ext => path.endsWith(ext)) &&
skip.every(pattern => !path.includes(pattern))
);
}
function findFilesWalk(paths: string[], depth: number) {
if (depth < 0) {
return [];
}
const foundPaths = paths.map(path =>
lstatSync(path).isDirectory()
? findFilesWalk(readDirSync(path).map(f => f.path), depth - 1)
: path
);
return [].concat(...foundPaths);
}
export const executableSuffix = platform.os === "win" ? ".exe" : "";
/** Returns true if the path exists. */
export function existsSync(path: string): boolean {
try {
lstatSync(path);
} catch (e) {
return false;
}
return true;
}
/**
* Looks up the available deno path with the priority
* of release -> debug -> global
*/
export function lookupDenoPath(): string {
const denoExe = "deno" + executableSuffix;
const releaseExe = join("target", "release", denoExe);
const debugExe = join("target", "debug", denoExe);
if (existsSync(releaseExe)) {
return releaseExe;
} else if (existsSync(debugExe)) {
return debugExe;
}
return denoExe;
}