UNPKG

3.15 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _chalk = _interopRequireDefault(require("chalk"));
9
10var _ansiEscapes = _interopRequireDefault(require("ansi-escapes"));
11
12var _stringLength = _interopRequireDefault(require("string-length"));
13
14var _jestWatcher = require("jest-watcher");
15
16var _jestRegexUtil = require("jest-regex-util");
17
18var _utils = require("../lib/utils");
19
20var _pattern_mode_helpers = require("../lib/pattern_mode_helpers");
21
22var _scroll = _interopRequireDefault(require("../lib/scroll"));
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26class FileNamePatternPrompt extends _jestWatcher.PatternPrompt {
27 constructor(pipe, prompt) {
28 super(pipe, prompt);
29 this._entityName = 'filenames';
30 this._searchSources = [];
31 }
32
33 _onChange(pattern, options) {
34 super._onChange(pattern, options);
35
36 this._printTypeahead(pattern, options);
37 }
38
39 _printTypeahead(pattern, options) {
40 const matchedTests = this._getMatchedTests(pattern);
41
42 const total = matchedTests.length;
43 const pipe = this._pipe;
44 const prompt = this._prompt;
45 (0, _jestWatcher.printPatternCaret)(pattern, pipe);
46 pipe.write(_ansiEscapes.default.cursorLeft);
47
48 if (pattern) {
49 (0, _pattern_mode_helpers.printPatternMatches)(total, 'file', pipe);
50 const prefix = ` ${_chalk.default.dim('\u203A')} `;
51 const padding = (0, _stringLength.default)(prefix) + 2;
52 const width = (0, _utils.getTerminalWidth)(pipe);
53 const {
54 start,
55 end,
56 index
57 } = (0, _scroll.default)(total, options);
58 prompt.setPromptLength(total);
59 matchedTests.slice(start, end).map(({
60 path,
61 context
62 }) => {
63 const filePath = (0, _utils.trimAndFormatPath)(padding, context.config, path, width);
64 return (0, _utils.highlight)(path, filePath, pattern);
65 }).map((item, i) => (0, _pattern_mode_helpers.formatTypeaheadSelection)(item, i, index, prompt)).forEach(item => (0, _pattern_mode_helpers.printTypeaheadItem)(item, pipe));
66
67 if (total > end) {
68 (0, _pattern_mode_helpers.printMore)('file', pipe, total - end);
69 }
70 } else {
71 (0, _pattern_mode_helpers.printStartTyping)('filename', pipe);
72 }
73
74 (0, _jestWatcher.printRestoredPatternCaret)(pattern, this._currentUsageRows, pipe);
75 }
76
77 _getMatchedTests(pattern) {
78 let regex;
79
80 try {
81 regex = new RegExp(pattern, 'i');
82 } catch (e) {
83 return [];
84 }
85
86 return this._searchSources.reduce((tests, {
87 testPaths,
88 config
89 }) => {
90 return tests.concat(testPaths.filter(testPath => regex.test(testPath)).map(path => ({
91 path,
92 context: {
93 config
94 }
95 })));
96 }, []);
97 }
98
99 updateSearchSources(searchSources) {
100 this._searchSources = searchSources;
101 }
102
103 run(onSuccess, onCancel, options) {
104 super.run(value => {
105 onSuccess((0, _utils.removeTrimmingDots)(value).split('/').map(_jestRegexUtil.escapeStrForRegex).join('/'));
106 }, onCancel, options);
107 }
108
109}
110
111exports.default = FileNamePatternPrompt;
\No newline at end of file