Skip to content

Commit

Permalink
chore: Inline parts of to-absolute-glob dependency.
Browse files Browse the repository at this point in the history
For #921.
  • Loading branch information
dsherret committed Feb 28, 2021
1 parent 2afd462 commit d9aa1f3
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 24 deletions.
1 change: 0 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"rollup": "rollup --config && ts-node scripts/bundleLocalTs.ts"
},
"dependencies": {
"@dsherret/to-absolute-glob": "^2.0.2",
"fast-glob": "^3.2.5",
"mkdirp": "^1.0.4",
"multimatch": "^5.0.0"
Expand Down
4 changes: 1 addition & 3 deletions packages/common/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import typescript from "rollup-plugin-typescript2";

export default [{
input: ["./src/index.ts"],
external: [
"@dsherret/to-absolute-glob",
],
external: [],
output: {
file: "./dist/ts-morph-common.js",
format: "cjs",
Expand Down
60 changes: 58 additions & 2 deletions packages/common/src/fileSystem/FileUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import toAbsoluteGlob from "@dsherret/to-absolute-glob";
import * as path from "path";
import { ArrayUtils, StringUtils } from "../utils";
import { FileSystemHost } from "./FileSystemHost";
Expand Down Expand Up @@ -262,7 +261,33 @@ export class FileUtils {
* @param cwd - Current working directory.
*/
static toAbsoluteGlob(glob: string, cwd: string) {
return toAbsoluteGlob(glob, { cwd });
// adapted from https://github.com/micromatch/to-absolute-glob
// trim starting ./ from glob patterns
if (glob.slice(0, 2) === "./")
glob = glob.slice(2);

// when the glob pattern is only a . use an empty string
if (glob.length === 1 && glob === ".")
glob = "";

// store last character before glob is modified
const suffix = glob.slice(-1);

// check to see if glob is negated (and not a leading negated-extglob)
const isNegated = FileUtils.isNegatedGlob(glob);
if (isNegated)
glob = glob.slice(1); // remove the leading "!"

// make glob absolute
if (!isAbsolutePath(glob) || glob.slice(0, 1) === "\\")
glob = globJoin(cwd, glob);

// if glob had a trailing `/`, re-add it now in case it was removed
if (suffix === "/" && glob.slice(-1) !== "/")
glob += "/";

// re-add leading `!` if it was removed
return isNegated ? "!" + glob : glob;
}

/**
Expand All @@ -274,3 +299,34 @@ export class FileUtils {
return glob[0] === "!" && glob[1] !== "(";
}
}

function globJoin(dir: string, glob: string) {
// from https://github.com/micromatch/to-absolute-glob
if (dir.charAt(dir.length - 1) === "/")
dir = dir.slice(0, -1);
if (glob.charAt(0) === "/")
glob = glob.slice(1);
if (!glob)
return dir;
return dir + "/" + glob;
}

// Code adapted from https://github.com/jonschlinkert/is-absolute/blob/master/index.js
function isAbsolutePath(filePath: string) {
return filePath.startsWith("/") || isWindowsAbsolutePath(filePath);
}

function isWindowsAbsolutePath(filePath: string) {
return isWindowsRootDirRegex.test(filePath) || isAzureAbsolutePath(filePath) || isUncPath(filePath);
}

function isAzureAbsolutePath(filePath: string) {
// Microsoft Azure absolute filepath apparently
return filePath.startsWith("\\\\");
}

// https://github.com/regexhq/unc-path-regex/blob/master/index.js
const uncPathRegex = /^[\\\/]{2,}[^\\\/]+[\\\/]+[^\\\/]+/;
function isUncPath(filePath: string) {
return uncPathRegex.test(filePath);
}
3 changes: 1 addition & 2 deletions packages/common/src/fileSystem/matchGlobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ export function matchGlobs(paths: ReadonlyArray<string>, patterns: ReadonlyArray
else
patterns = patterns.map(p => FileUtils.toAbsoluteGlob(p, cwd));

// @types/multimatch incorrectly specifies `string[]` type despite not modifying the array
return multimatch(paths as string[], patterns as string[]);
return multimatch(paths, patterns);
}
1 change: 0 additions & 1 deletion packages/common/src/typings/index.d.ts

This file was deleted.

15 changes: 0 additions & 15 deletions packages/common/src/typings/to-absolute-glob.d.ts

This file was deleted.

0 comments on commit d9aa1f3

Please sign in to comment.