kibana/packages/kbn-repo-source-classifier-cli/index.ts
Spencer 1b85815402
[packages] migrate all plugins to packages (#148130)
Fixes https://github.com/elastic/kibana/issues/149344

This PR migrates all plugins to packages automatically. It does this
using `node scripts/lint_packages` to automatically migrate
`kibana.json` files to `kibana.jsonc` files. By doing this automatically
we can simplify many build and testing procedures to only support
packages, and not both "packages" and "synthetic packages" (basically
pointers to plugins).

The majority of changes are in operations related code, so we'll be
having operations review this before marking it ready for review. The
vast majority of the code owners are simply pinged because we deleted
all `kibana.json` files and replaced them with `kibana.jsonc` files, so
we plan on leaving the PR ready-for-review for about 24 hours before
merging (after feature freeze), assuming we don't have any blockers
(especially from @elastic/kibana-core since there are a few core
specific changes, though the majority were handled in #149370).

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2023-02-08 21:06:50 -06:00

82 lines
2.9 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import Path from 'path';
import { RepoSourceClassifier } from '@kbn/repo-source-classifier';
import { ImportResolver } from '@kbn/import-resolver';
import { REPO_ROOT } from '@kbn/repo-info';
import { getRepoFiles } from '@kbn/get-repo-files';
import { run } from '@kbn/dev-cli-runner';
import { TypeTree } from './src/type_tree';
run(
async ({ flagsReader }) => {
const resolver = ImportResolver.create(REPO_ROOT);
const classifier = new RepoSourceClassifier(resolver);
const include = flagsReader.getPositionals().length
? flagsReader.getPositionals().map((p) => Path.resolve(p))
: [process.cwd()];
const exclude = (flagsReader.arrayOfStrings('exclude') ?? []).map((p) => Path.resolve(p));
const typeFlags = String(flagsReader.string('types'))
.split(',')
.map((f) => f.trim())
.filter(Boolean);
const includeTypes: string[] = [];
const excludeTypes: string[] = [];
for (const type of typeFlags) {
if (type.startsWith('!')) {
excludeTypes.push(type.slice(1));
} else {
includeTypes.push(type);
}
}
const tree = new TypeTree();
const cwd = process.cwd();
for (const { abs } of await getRepoFiles(include, exclude)) {
const { type } = classifier.classify(abs);
if ((includeTypes.length && !includeTypes.includes(type)) || excludeTypes.includes(type)) {
continue;
}
tree.add(type, Path.relative(cwd, abs));
}
if (flagsReader.boolean('flat')) {
for (const file of tree.toList()) {
process.stdout.write(`${file}\n`);
}
} else {
process.stdout.write(tree.print({ expand: flagsReader.boolean('expand') }));
}
},
{
description: 'run the repo-source-classifier on the source files and produce a report',
usage: `node scripts/classify_source <...paths>`,
flags: {
string: ['exclude', 'types'],
boolean: ['expand', 'flat'],
help: `
<...paths> include paths to select specific files which should be reported
by default all files in the cwd are classified. Can be specified
multiple times
--exclude exclude specific paths from the classification. Can be specified
multiple times
--types limit the types reported to the types in this comma separated list
to exclude a type prefix it with !
--expand prevent collapsing entries that are of the same type
--flat just print file names
`,
},
}
);