Skip to content

Commit 887093d

Browse files
author
Miki
authored
[@osd/cross-platform] Adds cross-platform helpers (opensearch-project#2681)
Signed-off-by: Miki <amoo_miki@yahoo.com> Signed-off-by: Miki <amoo_miki@yahoo.com>
1 parent ad0799d commit 887093d

File tree

23 files changed

+120
-49
lines changed

23 files changed

+120
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
2727
- [Vis Builder] Change classname prefix wiz to vb ([#2581](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2581/files))
2828
- [Vis Builder] Change wizard to vis_builder in file names and paths ([#2587](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2587))
2929
- [Windows] Facilitate building and running OSD and plugins on Windows platforms ([#2601](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2601))
30+
- [Windows] Add helper functions to work around the differences of platforms ([#2681](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2681))
3031
- [Multi DataSource] Address UX comments on Data source list and create page ([#2625](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2625))
3132
- [Vis Builder] Rename wizard to visBuilder in i18n id and formatted message id ([#2635](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2635))
3233
- [Vis Builder] Rename wizard to visBuilder in class name, type name and function name ([#2639](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2639))

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
"@osd/apm-config-loader": "1.0.0",
138138
"@osd/config": "1.0.0",
139139
"@osd/config-schema": "1.0.0",
140+
"@osd/cross-platform": "1.0.0",
140141
"@osd/i18n": "1.0.0",
141142
"@osd/interpreter": "1.0.0",
142143
"@osd/logging": "1.0.0",

packages/osd-config-schema/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
"osd:bootstrap": "yarn build"
1111
},
1212
"devDependencies": {
13-
"typescript": "4.0.2",
14-
"tsd": "^0.21.0"
13+
"@osd/cross-platform": "1.0.0",
14+
"tsd": "^0.21.0",
15+
"typescript": "4.0.2"
1516
},
1617
"peerDependencies": {
1718
"lodash": "^4.17.21",

packages/osd-config-schema/src/errors/__snapshots__/schema_error.test.ts.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/osd-config-schema/src/errors/schema_error.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import { relative, sep } from 'path';
3232
import { SchemaError } from '.';
3333

34+
import { standardize, PROCESS_WORKING_DIR } from '@osd/cross-platform';
35+
3436
/**
3537
* Make all paths in stacktrace relative.
3638
*/
@@ -46,9 +48,7 @@ export const cleanStack = (stack: string) =>
4648
}
4749

4850
const path = parts[1];
49-
// Cannot use `standardize` from `@osd/utils
50-
let relativePath = relative(process.cwd(), path);
51-
if (process.platform === 'win32') relativePath = relativePath.replace(/\\/g, '/');
51+
const relativePath = standardize(relative(PROCESS_WORKING_DIR, path));
5252

5353
return line.replace(path, relativePath);
5454
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `@osd/cross-platform` — OpenSearch Dashboards cross-platform helpers
2+
3+
This package contains the helper functions to work around the differences of platforms
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@osd/cross-platform",
3+
"main": "./target/index.js",
4+
"version": "1.0.0",
5+
"license": "Apache-2.0",
6+
"private": true,
7+
"scripts": {
8+
"build": "tsc",
9+
"osd:bootstrap": "yarn build"
10+
},
11+
"devDependencies": {
12+
"typescript": "4.0.2",
13+
"tsd": "^0.21.0"
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
export * from './path';
7+
export * from './process';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { normalize } from 'path';
7+
8+
/**
9+
* Get a standardized reference to a path
10+
* @param {string} path - the path to standardize
11+
* @param {boolean} [usePosix=true] - produce a posix reference
12+
* @param {boolean} [escapedBackslashes=true] - on Windows, double-backslash the reference
13+
* @internal
14+
*/
15+
export const standardize = (
16+
path: string,
17+
usePosix: boolean = true,
18+
escapedBackslashes: boolean = true
19+
) => {
20+
/* Force os-dependant separators
21+
* path.posix.normalize doesn't convert backslashes to slashes on Windows so we manually force it afterwards
22+
*/
23+
const normal = normalize(path);
24+
25+
// Filter out in-browser executions as well as non-windows ones
26+
if (process?.platform !== 'win32') return normal;
27+
28+
if (usePosix) return normal.replace(/\\/g, '/');
29+
return escapedBackslashes ? normal.replace(/\\/g, '\\\\') : normal;
30+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { execSync } from 'child_process';
7+
8+
let workingDir = process.cwd();
9+
10+
if (process.platform === 'win32') {
11+
try {
12+
const pathFullName = execSync('powershell "(Get-Item -LiteralPath $pwd).FullName"', {
13+
cwd: workingDir,
14+
encoding: 'utf8',
15+
})?.trim?.();
16+
if (pathFullName?.length > 2) workingDir = pathFullName;
17+
} catch (ex) {
18+
// Do nothing
19+
}
20+
}
21+
22+
export const PROCESS_WORKING_DIR = workingDir;

0 commit comments

Comments
 (0)