kibana/packages/kbn-relocate/utils.logging.ts
Gerard Soldevila 0b9179acbd
Sustainable Kibana Architecture: Add scripts/relocate CLI (beta) (#203803)
## Summary

Adds the `node scripts/relocate` functionality, that helps moving
modules to their intended locations, according to the _Sustainable
Kibana Architecture_.

Please refer to the README.md for further details and usage.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2024-12-11 17:10:37 +01:00

107 lines
3.4 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import type { ToolingLog } from '@kbn/tooling-log';
import { appendFileSync, writeFileSync } from 'fs';
import dedent from 'dedent';
import type { Package } from './types';
import { calculateModuleTargetFolder } from './utils.relocate';
import {
BASE_FOLDER,
DESCRIPTION,
GLOBAL_DESCRIPTION,
SCRIPT_ERRORS,
UPDATED_REFERENCES,
UPDATED_RELATIVE_PATHS,
} from './constants';
export const relocatePlan = (modules: Package[], log: ToolingLog) => {
const plugins = modules.filter((module) => module.manifest.type === 'plugin');
const packages = modules.filter((module) => module.manifest.type !== 'plugin');
const target = (module: Package) => calculateModuleTargetFolder(module).replace(BASE_FOLDER, '');
writeFileSync(DESCRIPTION, GLOBAL_DESCRIPTION);
if (plugins.length) {
const pluginList = dedent`
\n\n#### ${plugins.length} plugin(s) are going to be relocated:\n
| Id | Target folder |
| -- | ------------- |
${plugins.map((plg) => `| \`${plg.id}\` | \`${target(plg)}\` |`).join('\n')}
\n\n`;
appendFileSync(DESCRIPTION, pluginList);
log.info(
`${plugins.length} plugin(s) are going to be relocated:\n${plugins
.map((plg) => `${plg.id} => ${target(plg)}`)
.join('\n')}`
);
}
if (packages.length) {
const packageList = dedent`
\n\n#### ${packages.length} packages(s) are going to be relocated:\n
| Id | Target folder |
| -- | ------------- |
${packages.map((pkg) => `| \`${pkg.id}\` | \`${target(pkg)}\` |`).join('\n')}
\n\n`;
appendFileSync(DESCRIPTION, packageList);
log.info(
`${packages.length} packages(s) are going to be relocated:\n${packages
.map((plg) => `${plg.id} => ${target(plg)}`)
.join('\n')}`
);
}
};
export const appendCollapsible = (
fileName: string,
title: string,
contents: string,
open = false
) => {
appendFileSync(
fileName,
dedent`
<details ${open ? 'open' : ''}>
<summary>${title}</summary>
\`\`\`
${contents}
\`\`\`
</details>`
);
};
export const relocateSummary = (log: ToolingLog) => {
if (SCRIPT_ERRORS.length > 0) {
const contents = SCRIPT_ERRORS.sort().join('\n');
appendCollapsible(DESCRIPTION, 'Script errors', contents, true);
log.warning(`Please address the following errors:\n${contents}`);
}
if (UPDATED_REFERENCES.size > 0) {
const contents = Array.from(UPDATED_REFERENCES).sort().join('\n');
appendCollapsible(DESCRIPTION, 'Updated references', contents);
log.info(
`The following files have been updated to replace references to modules:\n${contents}`
);
}
if (UPDATED_RELATIVE_PATHS.size > 0) {
const contents = Array.from(UPDATED_RELATIVE_PATHS)
.sort()
.map((ref) => ref.replace(BASE_FOLDER, ''))
.join('\n');
appendCollapsible(DESCRIPTION, 'Updated relative paths', contents);
log.info(`The following files contain relative paths that have been updated:\n${contents}`);
}
};