Skip to content

Commit

Permalink
Adds @kbn/utils package (elastic#76518)
Browse files Browse the repository at this point in the history
Moves common utility functions to obtain the repository root,
paths (config/data), and Kibana package.json to a @kbn/utils package.
Moving these existing functions allows them to be used in production, in
other packages because of Kibana.

Signed-off-by: Tyler Smalley <[email protected]>
  • Loading branch information
Tyler Smalley committed Sep 15, 2020
1 parent 585ae33 commit 661d410
Show file tree
Hide file tree
Showing 103 changed files with 302 additions and 115 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
/packages/kbn-test/ @elastic/kibana-operations
/packages/kbn-ui-shared-deps/ @elastic/kibana-operations
/packages/kbn-es-archiver/ @elastic/kibana-operations
/packages/kbn-utils/ @elastic/kibana-operations
/src/legacy/server/keystore/ @elastic/kibana-operations
/src/legacy/server/pid/ @elastic/kibana-operations
/src/legacy/server/sass/ @elastic/kibana-operations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To get started copy and paste this example to `test/functional/config.js`:
["source","js"]
-----------
import { resolve } from 'path';
import { REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { MyServiceProvider } from './services/my_service';
import { MyAppPageProvider } from './services/my_app_page';
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@
"@types/hoek": "^4.1.3",
"@types/inert": "^5.1.2",
"@types/jest": "^25.2.3",
"@types/jest-when": "^2.7.1",
"@types/joi": "^13.4.2",
"@types/jquery": "^3.3.31",
"@types/js-yaml": "^3.11.1",
Expand Down Expand Up @@ -411,6 +412,7 @@
"jest-cli": "^25.5.4",
"jest-environment-jsdom-thirteen": "^1.0.1",
"jest-raw-loader": "^1.0.1",
"jest-when": "^2.7.2",
"jimp": "^0.14.0",
"jquery": "^3.5.0",
"js-levenshtein": "^1.1.6",
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-dev-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@babel/core": "^7.11.1",
"@kbn/utils": "1.0.0",
"axios": "^0.19.0",
"chalk": "^4.1.0",
"cheerio": "0.22.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-dev-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

export { REPO_ROOT } from '@kbn/utils';
export { withProcRunner, ProcRunner } from './proc_runner';
export * from './tooling_log';
export * from './serializers';
Expand All @@ -33,7 +34,6 @@ export {
KBN_P12_PATH,
KBN_P12_PASSWORD,
} from './certs';
export { REPO_ROOT } from './repo_root';
export { KbnClient } from './kbn_client';
export * from './run';
export * from './axios';
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-dev-utils/src/plugin_list/discover_plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import Fs from 'fs';

import MarkdownIt from 'markdown-it';
import cheerio from 'cheerio';
import { REPO_ROOT } from '@kbn/utils';

import { REPO_ROOT } from '../repo_root';
import { simpleKibanaPlatformPluginDiscovery } from '../simple_kibana_platform_plugin_discovery';
import { extractAsciidocInfo } from './extract_asciidoc_info';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import Path from 'path';

import normalizePath from 'normalize-path';
import { REPO_ROOT } from '@kbn/utils';

import { REPO_ROOT } from '../repo_root';
import { Plugins } from './discover_plugins';

function* printPlugins(plugins: Plugins, includes: string[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

import Path from 'path';
import Fs from 'fs';
import { REPO_ROOT } from '@kbn/utils';

import { run } from '../run';
import { REPO_ROOT } from '../repo_root';

import { discoverPlugins } from './discover_plugins';
import { generatePluginList } from './generate_plugin_list';

Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-dev-utils/src/precommit_hook/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import Path from 'path';
import { chmod, writeFile } from 'fs';
import { promisify } from 'util';
import { REPO_ROOT } from '@kbn/utils';

import { run } from '../run';
import { REPO_ROOT } from '../repo_root';
import { SCRIPT_SOURCE } from './script_source';
import { getGitDir } from './get_git_dir';

Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-dev-utils/src/precommit_hook/get_git_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import execa from 'execa';

import { REPO_ROOT } from '../repo_root';
import { REPO_ROOT } from '@kbn/utils';

// Retrieves the correct location for the .git dir for
// every git setup (including git worktree)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { REPO_ROOT } from '../repo_root';
import { REPO_ROOT } from '@kbn/utils';

export function createAbsolutePathSerializer(
rootPath: string = REPO_ROOT,
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-optimizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ To run the optimizer from code, you can import the [`OptimizerConfig`][Optimizer
Example:
```ts
import { runOptimizer, OptimizerConfig, logOptimizerState } from '@kbn/optimizer';
import { REPO_ROOT, ToolingLog } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { ToolingLog } from '@kbn/dev-utils';

const log = new ToolingLog({
level: 'verbose',
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-optimizer/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import 'source-map-support/register';

import Path from 'path';

import { run, REPO_ROOT, createFlagError, CiStatsReporter } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { run, createFlagError, CiStatsReporter } from '@kbn/dev-utils';

import { logOptimizerState } from './log_optimizer_state';
import { OptimizerConfig } from './optimizer';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import { inspect } from 'util';
import cpy from 'cpy';
import del from 'del';
import { toArray, tap, filter } from 'rxjs/operators';
import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { ToolingLog } from '@kbn/dev-utils';
import { runOptimizer, OptimizerConfig, OptimizerUpdate, logOptimizerState } from '@kbn/optimizer';

const TMP_DIR = Path.resolve(__dirname, '../__fixtures__/__tmp__');
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-optimizer/src/optimizer/cache_keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import Path from 'path';

import jestDiff from 'jest-diff';
import { REPO_ROOT, createAbsolutePathSerializer } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { createAbsolutePathSerializer } from '@kbn/dev-utils';

import { reformatJestDiff, getOptimizerCacheKey, diffCacheKey } from './cache_keys';
import { OptimizerConfig } from './optimizer_config';
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-optimizer/src/optimizer/cache_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { promisify } from 'util';

import Chalk from 'chalk';
import execa from 'execa';
import { REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import stripAnsi from 'strip-ansi';

import jestDiff from 'jest-diff';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import * as Rx from 'rxjs';
import { REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';

import { Update } from '../common';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ jest.mock('os', () => {
});

import Path from 'path';
import { REPO_ROOT, createAbsolutePathSerializer } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { createAbsolutePathSerializer } from '@kbn/dev-utils';

import { OptimizerConfig, ParsedOptions } from './optimizer_config';
import { parseThemeTags } from '../common';
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-plugin-generator/src/ask_questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import Path from 'path';

import { REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import inquirer from 'inquirer';

export interface Answers {
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-plugin-generator/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import Path from 'path';
import Fs from 'fs';

import execa from 'execa';
import { REPO_ROOT, run, createFailError, createFlagError } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { run, createFailError, createFlagError } from '@kbn/dev-utils';

import { snakeCase } from './casing';
import { askQuestions, getDefaultAnswers } from './ask_questions';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import Path from 'path';

import del from 'del';
import execa from 'execa';
import { REPO_ROOT, createAbsolutePathSerializer } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { createAbsolutePathSerializer } from '@kbn/dev-utils';
import globby from 'globby';

const GENERATED_DIR = Path.resolve(REPO_ROOT, `plugins`);
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-plugin-generator/src/plugin_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import Path from 'path';

import { REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';

export interface PluginType {
thirdParty: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-plugin-generator/src/render_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import { promisify } from 'util';

import vfs from 'vinyl-fs';
import prettier from 'prettier';
import { REPO_ROOT, transformFileStream } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { transformFileStream } from '@kbn/dev-utils';
import ejs from 'ejs';
import { Minimatch } from 'minimatch';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import Path from 'path';
import Fs from 'fs';

import execa from 'execa';
import { createStripAnsiSerializer, REPO_ROOT, createReplaceSerializer } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { createStripAnsiSerializer, createReplaceSerializer } from '@kbn/dev-utils';
import extract from 'extract-zip';
import del from 'del';
import globby from 'globby';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@

import Path from 'path';

import {
REPO_ROOT,
parseKibanaPlatformPlugin,
KibanaPlatformPlugin,
createFailError,
} from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { parseKibanaPlatformPlugin, KibanaPlatformPlugin, createFailError } from '@kbn/dev-utils';

export type Plugin = KibanaPlatformPlugin;

Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-plugin-helpers/src/tasks/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Fs from 'fs';
import Path from 'path';
import { promisify } from 'util';

import { REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { OptimizerConfig, runOptimizer, logOptimizerState } from '@kbn/optimizer';

import { BuildContext } from '../build_context';
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-pm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"write-pkg": "^4.0.0"
},
"dependencies": {
"@kbn/utils": "1.0.0",
"tslib": "^2.0.0"
}
}
1 change: 1 addition & 0 deletions packages/kbn-release-notes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"kbn:watch": "tsc --watch"
},
"dependencies": {
"@kbn/utils": "1.0.0",
"@kbn/dev-utils": "1.0.0",
"axios": "^0.19.2",
"cheerio": "0.22.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-release-notes/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import Fs from 'fs';
import Path from 'path';
import { inspect } from 'util';

import { run, createFlagError, createFailError, REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { run, createFlagError, createFailError } from '@kbn/dev-utils';

import { FORMATS, SomeFormat } from './formats';
import {
Expand Down
6 changes: 3 additions & 3 deletions packages/kbn-storybook/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
* under the License.
*/

const { resolve, dirname } = require('path');
const { resolve } = require('path');
const { REPO_ROOT } = require('@kbn/utils');

exports.REPO_ROOT = dirname(require.resolve('../../../package.json'));
exports.ASSET_DIR = resolve(exports.REPO_ROOT, 'built_assets/storybook');
exports.ASSET_DIR = resolve(REPO_ROOT, 'built_assets/storybook');
exports.CURRENT_CONFIG = resolve(exports.ASSET_DIR, 'current.config.js');
exports.STORY_ENTRY_PATH = resolve(exports.ASSET_DIR, 'stories.entry.js');
exports.DLL_DIST_DIR = resolve(exports.ASSET_DIR, 'dll');
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-storybook/lib/dll.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
const { resolve } = require('path');
const { existsSync } = require('fs');

const { REPO_ROOT, DLL_DIST_DIR } = require('./constants');
const { REPO_ROOT } = require('@kbn/utils');
const { DLL_DIST_DIR } = require('./constants');

exports.buildDll = async ({ rebuildDll, log, procRunner }) => {
if (rebuildDll) {
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-storybook/lib/storybook_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ const { promisify } = require('util');
const watch = require('glob-watcher');
const mkdirp = require('mkdirp'); // eslint-disable-line
const glob = require('fast-glob');
const { REPO_ROOT } = require('@kbn/utils');

const mkdirpAsync = promisify(mkdirp);
const writeFileAsync = promisify(Fs.writeFile);

const { REPO_ROOT, STORY_ENTRY_PATH } = require('./constants');
const { STORY_ENTRY_PATH } = require('./constants');
const STORE_ENTRY_DIR = dirname(STORY_ENTRY_PATH);

exports.generateStorybookEntry = ({ log, storyGlobs }) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-storybook/lib/webpack.dll.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
const webpack = require('webpack');
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { REPO_ROOT } = require('@kbn/utils');

const { DLL_NAME, REPO_ROOT, DLL_DIST_DIR } = require('./constants');
const { DLL_NAME, DLL_DIST_DIR } = require('./constants');

// This is the Webpack config for the DLL of CSS and JS assets that are
// not expected to change during development. This saves compile and run
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dependencies": {
"@kbn/babel-preset": "1.0.0",
"@kbn/dev-utils": "1.0.0",
"@kbn/utils": "1.0.0",
"@storybook/addon-actions": "^5.3.19",
"@storybook/addon-console": "^1.2.1",
"@storybook/addon-info": "^5.3.19",
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-storybook/storybook_config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const { stringifyRequest } = require('loader-utils');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { REPO_ROOT, DLL_DIST_DIR } = require('../lib/constants');
const { REPO_ROOT } = require('@kbn/utils');
const { DLL_DIST_DIR } = require('../lib/constants');
// eslint-disable-next-line import/no-unresolved
const { currentConfig } = require('../../../built_assets/storybook/current.config');

Expand Down
1 change: 1 addition & 0 deletions packages/kbn-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@babel/cli": "^7.10.5",
"@kbn/babel-preset": "1.0.0",
"@kbn/dev-utils": "1.0.0",
"@kbn/utils": "1.0.0",
"@types/joi": "^13.4.2",
"@types/lodash": "^4.14.159",
"@types/parse-link-header": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import Path from 'path';

import { REPO_ROOT, run, createFailError, createFlagError } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';
import { run, createFailError, createFlagError } from '@kbn/dev-utils';
import globby from 'globby';

import { getFailures, TestFailure } from './get_failures';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { spawnSync } from 'child_process';
import { resolve } from 'path';

import expect from '@kbn/expect';
import { REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';

const SCRIPT = resolve(REPO_ROOT, 'scripts/functional_test_runner.js');
const BASIC_CONFIG = require.resolve('../fixtures/simple_project/config.js');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { resolve } from 'path';

import stripAnsi from 'strip-ansi';
import expect from '@kbn/expect';
import { REPO_ROOT } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';

const SCRIPT = resolve(REPO_ROOT, 'scripts/functional_test_runner.js');
const FAILURE_HOOKS_CONFIG = require.resolve('../fixtures/failure_hooks/config.js');
Expand Down
Loading

0 comments on commit 661d410

Please sign in to comment.