-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($ci): Truncate long source file names
- Loading branch information
1 parent
5b7818a
commit 5959b80
Showing
5 changed files
with
137 additions
and
53 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,47 @@ | ||
import { escapeMarkdownCharacters, getPrettyPathName } from "./filename-utils" | ||
describe("getPrettyPathName", () => { | ||
it("doesn't change strings equal to the limit", () => { | ||
const input = "/exactly/17/chars" | ||
const output = getPrettyPathName(input, 17) | ||
expect(output).toEqual(input) | ||
}) | ||
|
||
it("elides the middle directories first", () => { | ||
const input = "/just/over/the/limit" | ||
const output = getPrettyPathName(input, 17) | ||
expect(output).toEqual("/just/../limit") | ||
}) | ||
|
||
it("elides the middle directories from right to left, excluding the root directory", () => { | ||
const input = "/just/over/the/limit" | ||
const output = getPrettyPathName(input, 18) | ||
expect(output).toEqual("/just/../the/limit") | ||
}) | ||
|
||
it("elides the root directory second", () => { | ||
const input = "/quite-a-lot-actually/over/the/limit" | ||
const output = getPrettyPathName(input, 17) | ||
expect(output).toEqual("../limit") | ||
}) | ||
|
||
it("truncates the the basename third", () => { | ||
const input = "/quite-a-lot-actually/over/the/limit-and-this-is-long-as-well" | ||
const output = getPrettyPathName(input, 17) | ||
expect(output).toEqual("../limit-and-th..") | ||
}) | ||
|
||
it("treats the tilde as the root directory", () => { | ||
const input = "~/just/over/the/limit" | ||
const output = getPrettyPathName(input, 18) | ||
expect(output).toEqual("~/../the/limit") | ||
}) | ||
}) | ||
|
||
describe("escapeMarkdownCharacters", () => { | ||
it("escapes filenames with '|,(,),[,],#,*,{,},-,+,_,!,\\,`>' characters", () => { | ||
const filename = `src/file-with-characters{[(|#*-+_!\`)]}.ts` | ||
const expectedFilename = `src/file\\-with\\-characters\\{\\[\\(\\|\\#\\*\\-\\+\\_\\!\\\`\\)\\]\\}.ts` | ||
const output = escapeMarkdownCharacters(filename) | ||
expect(output).toEqual(expectedFilename) | ||
}) | ||
}) |
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,53 @@ | ||
import * as _ from "lodash" | ||
import * as path from "path" | ||
|
||
/** | ||
* Shortens the length of a directory in a pretty way. | ||
* @param pathName The path to shorten | ||
* @param maxLength The maximum length of the path. Must be at least 4. | ||
* @returns The shortened directory name. | ||
*/ | ||
export function getPrettyPathName(pathName: string, maxLength: number): string { | ||
if (maxLength < 4) { | ||
throw Error("maxLength must be at least 4") | ||
} | ||
if (pathName.length <= maxLength) { | ||
return pathName | ||
} | ||
|
||
const parts = path.parse(pathName) | ||
|
||
const dirWithoutRoot = parts.dir.slice(parts.root.length, parts.dir.length - parts.root.length + 1) | ||
const dirs = dirWithoutRoot.split(path.sep) | ||
const root = `${parts.root}..${path.sep}` | ||
|
||
// Save the first directory, we want to try and prioritize keeping it in the path. | ||
let firstDir = dirs.shift() | ||
firstDir = firstDir ? firstDir : "" | ||
|
||
while (dirs.length > 0) { | ||
// Except for the first directory, start removing dirs from left to right. | ||
dirs.shift() | ||
const middle = ["..", ...dirs].join(path.sep) | ||
const newPath = `${parts.root}${firstDir}${path.sep}${middle}${path.sep}${parts.base}` | ||
if (newPath.length <= maxLength) { | ||
return newPath | ||
} | ||
} | ||
|
||
const rootAndName = `..${path.sep}${parts.base}` | ||
if (rootAndName.length <= maxLength) { | ||
return rootAndName | ||
} | ||
return `${rootAndName.slice(0, maxLength - 2)}..` | ||
} | ||
|
||
/** | ||
* Escapes the characters |()[]#*{}-+_!\,`> from a string. | ||
* @param source The source to escape | ||
* @returns An escaped version of the string | ||
*/ | ||
export function escapeMarkdownCharacters(source: string) { | ||
const escapedCharacters = ["|", "(", ")", "[", "]", "#", "*", "{", "}", "-", "+", "_", "!", "\\", "`"] | ||
return [...source].map(c => (_.includes(escapedCharacters, c) ? `\\${c}` : c)).join("") | ||
} |
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,3 @@ | ||
export function getGitRoot(): Promise<string> { | ||
throw Error("Unimplemented") | ||
} |
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
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