From b17f5c4074cdf4379b3bb81a685fc668159ccea8 Mon Sep 17 00:00:00 2001 From: Ashwin P Chandran Date: Thu, 27 Oct 2022 17:59:44 -0700 Subject: [PATCH] [Backport 2.x][@osd/cross-platform] Adds cross-platform helpers (#2681) (#2683) * [@osd/cross-platform] Adds cross-platform helpers (#2681) Signed-off-by: Miki Signed-off-by: Miki * fixes changelog formating Signed-off-by: Ashwin P Chandran Signed-off-by: Miki Signed-off-by: Ashwin P Chandran Co-authored-by: Miki --- CHANGELOG.md | 7 ++++- package.json | 1 + packages/osd-config-schema/package.json | 5 ++-- .../__snapshots__/schema_error.test.ts.snap | 2 +- .../src/errors/schema_error.test.ts | 6 ++-- packages/osd-cross-platform/README.md | 3 ++ packages/osd-cross-platform/package.json | 15 ++++++++++ packages/osd-cross-platform/src/index.ts | 7 +++++ packages/osd-cross-platform/src/path.ts | 30 +++++++++++++++++++ packages/osd-cross-platform/src/process.ts | 22 ++++++++++++++ packages/osd-cross-platform/tsconfig.json | 11 +++++++ packages/osd-optimizer/package.json | 1 + .../src/optimizer/get_changes.test.ts | 2 +- packages/osd-plugin-generator/package.json | 1 + .../integration_tests/generate_plugin.test.ts | 3 +- packages/osd-plugin-helpers/package.json | 1 + packages/osd-plugin-helpers/src/cli.ts | 17 ++++++----- .../src/integration_tests/build.test.ts | 8 ++--- packages/osd-pm/package.json | 1 + packages/osd-pm/src/utils/projects_tree.ts | 2 +- packages/osd-utils/package.json | 1 + packages/osd-utils/src/path/index.ts | 26 +--------------- src/dev/build/lib/config.test.ts | 3 +- 23 files changed, 125 insertions(+), 50 deletions(-) create mode 100644 packages/osd-cross-platform/README.md create mode 100644 packages/osd-cross-platform/package.json create mode 100644 packages/osd-cross-platform/src/index.ts create mode 100644 packages/osd-cross-platform/src/path.ts create mode 100644 packages/osd-cross-platform/src/process.ts create mode 100644 packages/osd-cross-platform/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index b968f9b54af..d41a5049059 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### 📈 Features/Enhancements * [MD] Support legacy client for data source ([#2204](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2204)) -* [MD] Add data source signing support ([#2510](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2510)) +* [MD] Add data source signing support ([#2510](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2510)) * [Plugin Helpers] Facilitate version changes ([#2398](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2398)) * [MD] Display error toast for create index pattern with data source ([#2506](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2506)) * [Multi DataSource] UX enhancement on index pattern management stack ([#2505](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2505)) @@ -23,8 +23,13 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) * [Multi DataSource] Add data source config to opensearch-dashboards-docker ([#2557](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2557)) * [Multi DataSource] Make text content dynamically translated & update unit tests ([#2570](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2570)) * [Vis Builder] Change classname prefix wiz to vb ([#2581](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2581/files)) +* [Vis Builder] Change wizard to vis_builder in file names and paths ([#2587](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2587)) * [Windows] Facilitate building and running OSD and plugins on Windows platforms ([#2601](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2601)) +* [Windows] Add helper functions to work around the differences of platforms ([#2681](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2681)) * [Multi DataSource] Address UX comments on Data source list and create page ([#2625](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2625)) +* [Vis Builder] Rename wizard to visBuilder in i18n id and formatted message id ([#2635](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2635)) +* [Vis Builder] Rename wizard to visBuilder in class name, type name and function name ([#2639](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2639)) +* [Vis Builder] Rename wizard on save modal and visualization table ([#2645](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2645)) ### 🐛 Bug Fixes * [Vis Builder] Fixes auto bounds for timeseries bar chart visualization ([2401](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2401)) diff --git a/package.json b/package.json index 367c0fd307f..4559d79ab87 100644 --- a/package.json +++ b/package.json @@ -135,6 +135,7 @@ "@osd/apm-config-loader": "1.0.0", "@osd/config": "1.0.0", "@osd/config-schema": "1.0.0", + "@osd/cross-platform": "1.0.0", "@osd/i18n": "1.0.0", "@osd/interpreter": "1.0.0", "@osd/logging": "1.0.0", diff --git a/packages/osd-config-schema/package.json b/packages/osd-config-schema/package.json index 1e07c42cba5..52471e29527 100644 --- a/packages/osd-config-schema/package.json +++ b/packages/osd-config-schema/package.json @@ -10,8 +10,9 @@ "osd:bootstrap": "yarn build" }, "devDependencies": { - "typescript": "4.0.2", - "tsd": "^0.21.0" + "@osd/cross-platform": "1.0.0", + "tsd": "^0.21.0", + "typescript": "4.0.2" }, "peerDependencies": { "lodash": "^4.17.21", diff --git a/packages/osd-config-schema/src/errors/__snapshots__/schema_error.test.ts.snap b/packages/osd-config-schema/src/errors/__snapshots__/schema_error.test.ts.snap index 38f81a0cc62..2acd5f14fb5 100644 --- a/packages/osd-config-schema/src/errors/__snapshots__/schema_error.test.ts.snap +++ b/packages/osd-config-schema/src/errors/__snapshots__/schema_error.test.ts.snap @@ -3,6 +3,6 @@ exports[`includes stack 1`] = ` "Error: test at new SchemaError (packages/osd-config-schema/src/errors/schema_error.ts:35:5) - at Object. (packages/osd-config-schema/src/errors/schema_error.test.ts:59:11) + at Object. (packages/osd-config-schema/src/errors/schema_error.test.ts:61:11) at new Promise ()" `; diff --git a/packages/osd-config-schema/src/errors/schema_error.test.ts b/packages/osd-config-schema/src/errors/schema_error.test.ts index 9e7b5a89708..345304c955e 100644 --- a/packages/osd-config-schema/src/errors/schema_error.test.ts +++ b/packages/osd-config-schema/src/errors/schema_error.test.ts @@ -31,6 +31,8 @@ import { relative, sep } from 'path'; import { SchemaError } from '.'; +import { standardize, PROCESS_WORKING_DIR } from '@osd/cross-platform'; + /** * Make all paths in stacktrace relative. */ @@ -46,9 +48,7 @@ export const cleanStack = (stack: string) => } const path = parts[1]; - // Cannot use `standardize` from `@osd/utils - let relativePath = relative(process.cwd(), path); - if (process.platform === 'win32') relativePath = relativePath.replace(/\\/g, '/'); + const relativePath = standardize(relative(PROCESS_WORKING_DIR, path)); return line.replace(path, relativePath); }) diff --git a/packages/osd-cross-platform/README.md b/packages/osd-cross-platform/README.md new file mode 100644 index 00000000000..a61a2f184f0 --- /dev/null +++ b/packages/osd-cross-platform/README.md @@ -0,0 +1,3 @@ +# `@osd/cross-platform` — OpenSearch Dashboards cross-platform helpers + +This package contains the helper functions to work around the differences of platforms diff --git a/packages/osd-cross-platform/package.json b/packages/osd-cross-platform/package.json new file mode 100644 index 00000000000..1af90b00a98 --- /dev/null +++ b/packages/osd-cross-platform/package.json @@ -0,0 +1,15 @@ +{ + "name": "@osd/cross-platform", + "main": "./target/index.js", + "version": "1.0.0", + "license": "Apache-2.0", + "private": true, + "scripts": { + "build": "tsc", + "osd:bootstrap": "yarn build" + }, + "devDependencies": { + "typescript": "4.0.2", + "tsd": "^0.21.0" + } +} diff --git a/packages/osd-cross-platform/src/index.ts b/packages/osd-cross-platform/src/index.ts new file mode 100644 index 00000000000..343d7e9257a --- /dev/null +++ b/packages/osd-cross-platform/src/index.ts @@ -0,0 +1,7 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export * from './path'; +export * from './process'; diff --git a/packages/osd-cross-platform/src/path.ts b/packages/osd-cross-platform/src/path.ts new file mode 100644 index 00000000000..7e37443f1bc --- /dev/null +++ b/packages/osd-cross-platform/src/path.ts @@ -0,0 +1,30 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { normalize } from 'path'; + +/** + * Get a standardized reference to a path + * @param {string} path - the path to standardize + * @param {boolean} [usePosix=true] - produce a posix reference + * @param {boolean} [escapedBackslashes=true] - on Windows, double-backslash the reference + * @internal + */ +export const standardize = ( + path: string, + usePosix: boolean = true, + escapedBackslashes: boolean = true +) => { + /* Force os-dependant separators + * path.posix.normalize doesn't convert backslashes to slashes on Windows so we manually force it afterwards + */ + const normal = normalize(path); + + // Filter out in-browser executions as well as non-windows ones + if (process?.platform !== 'win32') return normal; + + if (usePosix) return normal.replace(/\\/g, '/'); + return escapedBackslashes ? normal.replace(/\\/g, '\\\\') : normal; +}; diff --git a/packages/osd-cross-platform/src/process.ts b/packages/osd-cross-platform/src/process.ts new file mode 100644 index 00000000000..fa593c94368 --- /dev/null +++ b/packages/osd-cross-platform/src/process.ts @@ -0,0 +1,22 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { execSync } from 'child_process'; + +let workingDir = process.cwd(); + +if (process.platform === 'win32') { + try { + const pathFullName = execSync('powershell "(Get-Item -LiteralPath $pwd).FullName"', { + cwd: workingDir, + encoding: 'utf8', + })?.trim?.(); + if (pathFullName?.length > 2) workingDir = pathFullName; + } catch (ex) { + // Do nothing + } +} + +export const PROCESS_WORKING_DIR = workingDir; diff --git a/packages/osd-cross-platform/tsconfig.json b/packages/osd-cross-platform/tsconfig.json new file mode 100644 index 00000000000..e9dd6313e6f --- /dev/null +++ b/packages/osd-cross-platform/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "outDir": "target", + "declaration": true, + "declarationMap": true + }, + "include": [ + "src/**/*" + ] +} diff --git a/packages/osd-optimizer/package.json b/packages/osd-optimizer/package.json index 82e592b3414..9ecbea29800 100644 --- a/packages/osd-optimizer/package.json +++ b/packages/osd-optimizer/package.json @@ -13,6 +13,7 @@ "@babel/cli": "^7.16.0", "@babel/core": "^7.16.5", "@osd/babel-preset": "1.0.0", + "@osd/cross-platform": "1.0.0", "@osd/dev-utils": "1.0.0", "@osd/std": "1.0.0", "@osd/ui-shared-deps": "1.0.0", diff --git a/packages/osd-optimizer/src/optimizer/get_changes.test.ts b/packages/osd-optimizer/src/optimizer/get_changes.test.ts index 44e1637d543..6ccf686e43a 100644 --- a/packages/osd-optimizer/src/optimizer/get_changes.test.ts +++ b/packages/osd-optimizer/src/optimizer/get_changes.test.ts @@ -33,7 +33,7 @@ import path from 'path'; jest.mock('execa'); import { getChanges } from './get_changes'; -import { standardize } from '@osd/dev-utils'; +import { standardize } from '@osd/cross-platform'; const execa: jest.Mock = jest.requireMock('execa'); diff --git a/packages/osd-plugin-generator/package.json b/packages/osd-plugin-generator/package.json index 73aaeba4cb1..66028c53875 100644 --- a/packages/osd-plugin-generator/package.json +++ b/packages/osd-plugin-generator/package.json @@ -9,6 +9,7 @@ "osd:watch": "node scripts/build --watch" }, "dependencies": { + "@osd/cross-platform": "1.0.0", "@osd/dev-utils": "1.0.0", "ejs": "^3.1.7", "execa": "^4.0.2", diff --git a/packages/osd-plugin-generator/src/integration_tests/generate_plugin.test.ts b/packages/osd-plugin-generator/src/integration_tests/generate_plugin.test.ts index 45ec5a6986a..c81f248369e 100644 --- a/packages/osd-plugin-generator/src/integration_tests/generate_plugin.test.ts +++ b/packages/osd-plugin-generator/src/integration_tests/generate_plugin.test.ts @@ -32,7 +32,8 @@ import Path from 'path'; import del from 'del'; import execa from 'execa'; -import { REPO_ROOT, standardize, createAbsolutePathSerializer } from '@osd/dev-utils'; +import { standardize } from '@osd/cross-platform'; +import { REPO_ROOT, createAbsolutePathSerializer } from '@osd/dev-utils'; import globby from 'globby'; // Has to be a posix reference because it is used to generate glob patterns diff --git a/packages/osd-plugin-helpers/package.json b/packages/osd-plugin-helpers/package.json index 3738aae36b1..55e5e803fe4 100644 --- a/packages/osd-plugin-helpers/package.json +++ b/packages/osd-plugin-helpers/package.json @@ -16,6 +16,7 @@ "osd:watch": "tsc --watch" }, "dependencies": { + "@osd/cross-platform": "1.0.0", "@osd/dev-utils": "1.0.0", "@osd/optimizer": "1.0.0", "del": "^5.1.0", diff --git a/packages/osd-plugin-helpers/src/cli.ts b/packages/osd-plugin-helpers/src/cli.ts index 82bd9f5adbe..f108f9a2f6c 100644 --- a/packages/osd-plugin-helpers/src/cli.ts +++ b/packages/osd-plugin-helpers/src/cli.ts @@ -30,6 +30,7 @@ import Path from 'path'; +import { PROCESS_WORKING_DIR } from '@osd/cross-platform'; import { RunWithCommands, createFlagError, createFailError } from '@osd/dev-utils'; import { findOpenSearchDashboardsJson } from './find_opensearch_dashboards_json'; @@ -79,10 +80,10 @@ export function runCli() { throw createFlagError('expected a single --skip-archive flag'); } - const pluginDir = await findOpenSearchDashboardsJson(process.cwd()); + const pluginDir = await findOpenSearchDashboardsJson(PROCESS_WORKING_DIR); if (!pluginDir) { throw createFailError( - `Unable to find OpenSearch Dashboards Platform plugin in [${process.cwd()}] or any of its parent directories. Has it been migrated properly? Does it have a opensearch_dashboards.json file?` + `Unable to find OpenSearch Dashboards Platform plugin in [${PROCESS_WORKING_DIR}] or any of its parent directories. Has it been migrated properly? Does it have a opensearch_dashboards.json file?` ); } @@ -148,30 +149,30 @@ export function runCli() { allowUnexpected: true, }, async run({ log, flags }) { - const pluginDir = await findOpenSearchDashboardsJson(process.cwd()); + const pluginDir = await findOpenSearchDashboardsJson(PROCESS_WORKING_DIR); if (!pluginDir) { throw createFailError( - `Unable to find OpenSearch Dashboards Platform plugin in [${process.cwd()}] or any of its parent directories. Has it been migrated properly? Does it have a opensearch_dashboards.json file?` + `Unable to find OpenSearch Dashboards Platform plugin in [${PROCESS_WORKING_DIR}] or any of its parent directories. Has it been migrated properly? Does it have a opensearch_dashboards.json file?` ); } let dashboardsPackage; try { - dashboardsPackage = await import(Path.join(process.cwd(), '../../package.json')); + dashboardsPackage = await import(Path.join(PROCESS_WORKING_DIR, '../../package.json')); } catch (ex) { throw createFailError(`Unable to parse the OpenSearch Dashboards' package.json file`); } let pluginPackage; try { - pluginPackage = await import(Path.join(process.cwd(), 'package.json')); + pluginPackage = await import(Path.join(PROCESS_WORKING_DIR, 'package.json')); } catch (ex) { throw createFailError(`Unable to parse the plugin's package.json file`); } let manifestFile; try { - manifestFile = await import(Path.join(process.cwd(), 'opensearch_dashboards.json')); + manifestFile = await import(Path.join(PROCESS_WORKING_DIR, 'opensearch_dashboards.json')); } catch (ex) { throw createFailError(`Unable to parse the plugin's opensearch_dashboards.json file`); } @@ -240,7 +241,7 @@ export function runCli() { const context: VersionContext = { log, - sourceDir: process.cwd(), + sourceDir: PROCESS_WORKING_DIR, pluginVersion: updatedPluginVersion, compatibilityVersion: updatedCompatibilityVersion, }; diff --git a/packages/osd-plugin-helpers/src/integration_tests/build.test.ts b/packages/osd-plugin-helpers/src/integration_tests/build.test.ts index 35195f9bc16..12a0b52ace2 100644 --- a/packages/osd-plugin-helpers/src/integration_tests/build.test.ts +++ b/packages/osd-plugin-helpers/src/integration_tests/build.test.ts @@ -32,12 +32,8 @@ import Path from 'path'; import Fs from 'fs'; import execa from 'execa'; -import { - REPO_ROOT, - standardize, - createStripAnsiSerializer, - createReplaceSerializer, -} from '@osd/dev-utils'; +import { standardize } from '@osd/cross-platform'; +import { REPO_ROOT, createStripAnsiSerializer, createReplaceSerializer } from '@osd/dev-utils'; import extract from 'extract-zip'; import del from 'del'; import globby from 'globby'; diff --git a/packages/osd-pm/package.json b/packages/osd-pm/package.json index 3af720ce169..cda579e14fe 100644 --- a/packages/osd-pm/package.json +++ b/packages/osd-pm/package.json @@ -69,6 +69,7 @@ "write-pkg": "^4.0.0" }, "dependencies": { + "@osd/cross-platform": "1.0.0", "@osd/utils": "1.0.0", "tslib": "^2.0.0" } diff --git a/packages/osd-pm/src/utils/projects_tree.ts b/packages/osd-pm/src/utils/projects_tree.ts index 41f9e433109..3c14be94ca0 100644 --- a/packages/osd-pm/src/utils/projects_tree.ts +++ b/packages/osd-pm/src/utils/projects_tree.ts @@ -31,7 +31,7 @@ import chalk from 'chalk'; import path from 'path'; -import { standardize } from '@osd/utils'; +import { standardize } from '@osd/cross-platform'; import { Project } from './project'; const projectKey = Symbol('__project'); diff --git a/packages/osd-utils/package.json b/packages/osd-utils/package.json index 802b03a0adb..1d632cffbf6 100644 --- a/packages/osd-utils/package.json +++ b/packages/osd-utils/package.json @@ -11,6 +11,7 @@ }, "dependencies": { "@osd/config-schema": "1.0.0", + "@osd/cross-platform": "1.0.0", "load-json-file": "^6.2.0" }, "devDependencies": { diff --git a/packages/osd-utils/src/path/index.ts b/packages/osd-utils/src/path/index.ts index 263d2d39ac3..c661b04fb6a 100644 --- a/packages/osd-utils/src/path/index.ts +++ b/packages/osd-utils/src/path/index.ts @@ -28,7 +28,7 @@ * under the License. */ -import { join, normalize } from 'path'; +import { join } from 'path'; import { accessSync, constants } from 'fs'; import { TypeOf, schema } from '@osd/config-schema'; import { REPO_ROOT } from '../repo_root'; @@ -94,27 +94,3 @@ export const config = { data: schema.string({ defaultValue: () => getDataPath() }), }), }; - -/** - * Get a standardized reference to a path - * @param {string} path - the path to standardize - * @param {boolean} [usePosix=true] - produce a posix reference - * @param {boolean} [escapedBackslashes=true] - on Windows, double-backslash the reference - * @internal - */ -export const standardize = ( - path: string, - usePosix: boolean = true, - escapedBackslashes: boolean = true -) => { - /* Force os-dependant separators - * path.posix.normalize doesn't convert backslashes to slashes on Windows so we manually force it afterwards - */ - const normal = normalize(path); - - // Filter out in-browser executions as well as non-windows ones - if (process?.platform !== 'win32') return normal; - - if (usePosix) return normal.replace(/\\/g, '/'); - return escapedBackslashes ? normal.replace(/\\/g, '\\\\') : normal; -}; diff --git a/src/dev/build/lib/config.test.ts b/src/dev/build/lib/config.test.ts index db96a8c18dd..8005f2d952a 100644 --- a/src/dev/build/lib/config.test.ts +++ b/src/dev/build/lib/config.test.ts @@ -30,7 +30,8 @@ import { resolve } from 'path'; -import { REPO_ROOT, standardize } from '@osd/utils'; +import { standardize } from '@osd/cross-platform'; +import { REPO_ROOT } from '@osd/utils'; import { createAbsolutePathSerializer } from '@osd/dev-utils'; import pkg from '../../../../package.json';