-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathsearch-trace-files.js
66 lines (55 loc) · 2.35 KB
/
search-trace-files.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2023 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Finds trace files in fixtures/traces with references to the provided string.
*
* Usage:
* node scripts/search-trace-files.js "v8.parse"
*/
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const TRACE_FILES_DIR = path.join(__dirname, '..', 'front_end', 'panels', 'timeline', 'fixtures', 'traces');
// Get a list of all trace files (gzipped or otherwise)
const filesInDir = fs.readdirSync(TRACE_FILES_DIR, 'utf8').filter(file => file.includes('.json'));
// A set of all the *.json files that we find.
const extractedFilePaths = new Set();
for (const fileNameWithExt of filesInDir) {
// Get the name of the file with no extensions. /foo/bar/baz.json.gz => "baz"
const fileName = path.basename(fileNameWithExt).split('.')[0];
const fullPath = path.join(TRACE_FILES_DIR, `${fileName}.json.gz`);
// If we found a .json.gz without a matching .json, extract the file.
// traces/*.json are gitignored, so we can safely do this without risking
// them being committed.
if (!fs.existsSync(path.join(TRACE_FILES_DIR, `${fileName}.json`))) {
console.log('[One time operation] Extracting gzip:', fileName);
const result = child_process.spawnSync('gzip', ['--keep', '-d', `${fullPath}`]);
if (result.status !== 0) {
console.error(`Error extracting ${path.basename(fullPath)}: ${result.output}`);
}
}
extractedFilePaths.add(path.join(TRACE_FILES_DIR, `${fileName}.json`));
}
const searchTerm = process.argv[2];
console.log('Searching for', `"${searchTerm}"`);
const matches = new Set();
Array.from(extractedFilePaths).forEach(fileToCheck => {
if (!fs.existsSync(fileToCheck)) {
return null;
}
// Yes, this is pretty inefficient to read the entire file sync, but this
// is only a hacky helper script so it will do :)
const contents = fs.readFileSync(fileToCheck, 'utf8');
const hasMatch = contents.includes(searchTerm);
if (hasMatch) {
matches.add(fileToCheck);
}
});
if (matches.size) {
const matchesSorted = Array.from(matches).map(fullPath => path.relative(TRACE_FILES_DIR, fullPath)).sort();
console.log('\n=> ' + matchesSorted.join('\n=> '));
} else {
console.log('No matches found :(');
}