From cc0a0c9615115cc67222024bea5662d8e135cde9 Mon Sep 17 00:00:00 2001 From: Miki Date: Mon, 24 Oct 2022 12:13:43 -0700 Subject: [PATCH] Manually backports Windows changes from #2601 (#2647) * [Windows] Replaces `rm -rf` with `remove.js` * [dev/build] Facilitates using zipped archives of node releases * [div/build] Introduces Windows as a platform * [dev/build] Corrects cleaning of platform specific build artifacts * [dev/build] Enhances the cleanup of downloaded node binaries * [opensearch-dashboards-plugin] Removes prohibition on installing plugins on Windows * [@osd/utils] Adds a method to standardize path references across platforms * [dev/build] Standardize paths in tests * [@osd/telemetry-tools] Normalizes the collection paths * [plugins/url-forwarding] Fixes the usage of `normalizePath` across node and browser * [@osd/pm] Allows symlink created for tests without elevated privileges on Windows * [@osd/opensearch] Allows usage of Windows snapshots in integration tests * [@osd/opensearch] Employs absolute paths in tests * [@osd/apm-config-loader] Employs absolute paths in tests * [core/server] Employs absolute and posix references to paths * [@osd/optimizer] Standardize paths in tests * [@osd/tests] Employs absolute paths in tests * [@osd/pm] Standardize paths in project trees * [plugins/telemetry] Accommodates the inability of Windows to create unreadable files for testing * [@osd/config-schema] Normalize paths in tests * [@osd/plugin-helpers] Standardize paths in tests * [@osd/plugin-generator] Standardize paths in tests * [Windows] Update changelog backport PR: https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2601 Signed-off-by: Miki --- packages/osd-utils/src/path/index.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/osd-utils/src/path/index.ts b/packages/osd-utils/src/path/index.ts index ac4c40b0b606..3c10eff22d96 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 } from 'path'; +import { join, normalize } from 'path'; import { accessSync, constants } from 'fs'; import { TypeOf, schema } from '@osd/config-schema'; import { REPO_ROOT } from '@osd/cross-platform'; @@ -94,3 +94,27 @@ 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; +};