-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathtrimAndFormatPath.ts
More file actions
46 lines (40 loc) · 1.4 KB
/
trimAndFormatPath.ts
File metadata and controls
46 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as path from 'path';
import chalk from 'chalk';
import type {Config} from '@jest/types';
import {slash} from 'jest-util';
import relativePath from './relativePath';
export default function trimAndFormatPath(
pad: number,
config: Config.ProjectConfig | Config.GlobalConfig,
testPath: string,
columns: number,
): string {
const maxLength = columns - pad;
const relative = relativePath(config, testPath);
const {basename} = relative;
let {dirname} = relative;
// length is ok
if ((dirname + path.sep + basename).length <= maxLength) {
return slash(chalk.dim(dirname + path.sep) + chalk.bold(basename));
}
// we can fit trimmed dirname and full basename
const basenameLength = basename.length;
if (basenameLength + 4 < maxLength) {
const dirnameLength = maxLength - 4 - basenameLength;
dirname = `...${dirname.slice(dirname.length - dirnameLength)}`;
return slash(chalk.dim(dirname + path.sep) + chalk.bold(basename));
}
if (basenameLength + 4 === maxLength) {
return slash(chalk.dim(`...${path.sep}`) + chalk.bold(basename));
}
// can't fit dirname, but can fit trimmed basename
return slash(
chalk.bold(`...${basename.slice(basename.length - maxLength - 4)}`),
);
}