Skip to content

Instantly share code, notes, and snippets.

@BSFishy
Created June 14, 2023 20:38
Show Gist options
  • Save BSFishy/230450d7f49b449245c5b3573e499161 to your computer and use it in GitHub Desktop.
Save BSFishy/230450d7f49b449245c5b3573e499161 to your computer and use it in GitHub Desktop.
Search OSD for certain strings in certain files
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
const fs = require('fs');
const path = require('path');
const regex = /#[0-9a-fA-F]{3}(?:[0-9a-fA-F]{3})?|(?:rgb|rgba|hsl)\([^)]*\)/g;
const includePaths = ['packages', 'src'].map((filterPath) => path.join(__dirname, filterPath));
const includeExtensions = ['.scss', '.css'];
function getTargetPaths(rootPath) {
const dirs = fs.readdirSync(rootPath);
const targetPaths = [];
for (const dir of dirs) {
targetPaths.push(path.join(rootPath, dir, 'target'));
}
return targetPaths;
}
const ignorePaths = [
path.join(__dirname, 'packages/osd-ui-framework'),
...getTargetPaths(path.join(__dirname, 'src')),
...getTargetPaths(path.join(__dirname, 'src', 'plugins')),
...getTargetPaths(path.join(__dirname, 'packages')),
];
const ignoreExtensions = ['.map'];
function recursePath(currentDir, callback) {
const files = fs.readdirSync(currentDir);
for (const file of files) {
const filePath = path.join(currentDir, file);
const stat = fs.lstatSync(filePath);
if (includePaths.filter((includePath) => filePath.startsWith(includePath)) < 1) {
continue;
}
if (ignorePaths.filter((ignorePath) => filePath.startsWith(ignorePath)).length > 0) {
continue;
}
if (
ignoreExtensions.filter((ignoreExtension) => filePath.endsWith(ignoreExtension)).length > 0
) {
continue;
}
if (stat.isFile()) {
if (
includeExtensions.filter((includeExtension) => filePath.endsWith(includeExtension)).length <
1
) {
continue;
}
callback(filePath);
} else if (stat.isDirectory()) {
recursePath(filePath, callback);
}
}
}
const matches = {};
recursePath(__dirname, function search(searchPath) {
let contents;
try {
contents = fs.readFileSync(searchPath).toString();
} catch (e) {
console.error(`failed to read ${searchPath}`);
return;
}
let match;
while ((match = regex.exec(contents)) !== null) {
// Prevent infinite loops on zero-width matches
if (match.index === regex.lastIndex) {
regex.lastIndex++;
}
const lineNumber =
contents
.slice(0, match.index)
.split('')
.filter((c) => c === '\n').length + 1;
(matches[searchPath] || (matches[searchPath] = [])).push([match[0], lineNumber]);
}
});
const sortedMatches = Object.entries(matches).map(([matchPath, matchStrings]) => [
matchPath.slice(__dirname.length + 1),
matchStrings,
]);
for (const [, matches] of sortedMatches) {
matches.sort(([, aLineNumber], [, bLineNumber]) => {
return aLineNumber - bLineNumber;
});
}
sortedMatches.sort(([a], [b]) => {
const aSplit = a.split('.');
const aExt = aSplit[aSplit.length - 1];
const bSplit = b.split('.');
const bExt = bSplit[bSplit.length - 1];
return aExt.localeCompare(bExt);
});
console.log('| file name | matched strings |');
console.log('| --- | --- |');
for (const [matchPath, matchStrings] of sortedMatches) {
console.log(
`| [\`${matchPath}\`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/main/${matchPath}) | ${matchStrings
.map(
([match, ln]) =>
`[\`${match}\`](https://github.com/opensearch-project/OpenSearch-Dashboards/blob/2322c53d10664d68c7d2ee21a86a4e29d1919d9c/${matchPath}#L${ln})`
)
.join(', ')} |`
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment