Skip to content

Commit 887093d

Browse files
authored
[@osd/cross-platform] Adds cross-platform helpers (opensearch-project#2681)
Signed-off-by: Miki <[email protected]> Signed-off-by: Miki <[email protected]>
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
})

packages/osd-cross-platform/README.md

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;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "target",
5+
"declaration": true,
6+
"declarationMap": true
7+
},
8+
"include": [
9+
"src/**/*"
10+
]
11+
}

packages/osd-optimizer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@babel/cli": "^7.16.0",
1414
"@babel/core": "^7.16.5",
1515
"@osd/babel-preset": "1.0.0",
16+
"@osd/cross-platform": "1.0.0",
1617
"@osd/dev-utils": "1.0.0",
1718
"@osd/std": "1.0.0",
1819
"@osd/ui-shared-deps": "1.0.0",

packages/osd-optimizer/src/optimizer/get_changes.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import path from 'path';
3333
jest.mock('execa');
3434

3535
import { getChanges } from './get_changes';
36-
import { standardize } from '@osd/dev-utils';
36+
import { standardize } from '@osd/cross-platform';
3737

3838
const execa: jest.Mock = jest.requireMock('execa');
3939

packages/osd-plugin-generator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"osd:watch": "node scripts/build --watch"
1010
},
1111
"dependencies": {
12+
"@osd/cross-platform": "1.0.0",
1213
"@osd/dev-utils": "1.0.0",
1314
"ejs": "^3.1.7",
1415
"execa": "^4.0.2",

packages/osd-plugin-generator/src/integration_tests/generate_plugin.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import Path from 'path';
3232

3333
import del from 'del';
3434
import execa from 'execa';
35-
import { REPO_ROOT, standardize, createAbsolutePathSerializer } from '@osd/dev-utils';
35+
import { standardize } from '@osd/cross-platform';
36+
import { REPO_ROOT, createAbsolutePathSerializer } from '@osd/dev-utils';
3637
import globby from 'globby';
3738

3839
// Has to be a posix reference because it is used to generate glob patterns

packages/osd-plugin-helpers/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"osd:watch": "tsc --watch"
1717
},
1818
"dependencies": {
19+
"@osd/cross-platform": "1.0.0",
1920
"@osd/dev-utils": "1.0.0",
2021
"@osd/optimizer": "1.0.0",
2122
"del": "^5.1.0",

packages/osd-plugin-helpers/src/cli.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import Path from 'path';
3232

33+
import { PROCESS_WORKING_DIR } from '@osd/cross-platform';
3334
import { RunWithCommands, createFlagError, createFailError } from '@osd/dev-utils';
3435

3536
import { findOpenSearchDashboardsJson } from './find_opensearch_dashboards_json';
@@ -79,10 +80,10 @@ export function runCli() {
7980
throw createFlagError('expected a single --skip-archive flag');
8081
}
8182

82-
const pluginDir = await findOpenSearchDashboardsJson(process.cwd());
83+
const pluginDir = await findOpenSearchDashboardsJson(PROCESS_WORKING_DIR);
8384
if (!pluginDir) {
8485
throw createFailError(
85-
`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?`
86+
`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?`
8687
);
8788
}
8889

@@ -148,30 +149,30 @@ export function runCli() {
148149
allowUnexpected: true,
149150
},
150151
async run({ log, flags }) {
151-
const pluginDir = await findOpenSearchDashboardsJson(process.cwd());
152+
const pluginDir = await findOpenSearchDashboardsJson(PROCESS_WORKING_DIR);
152153
if (!pluginDir) {
153154
throw createFailError(
154-
`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?`
155+
`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?`
155156
);
156157
}
157158

158159
let dashboardsPackage;
159160
try {
160-
dashboardsPackage = await import(Path.join(process.cwd(), '../../package.json'));
161+
dashboardsPackage = await import(Path.join(PROCESS_WORKING_DIR, '../../package.json'));
161162
} catch (ex) {
162163
throw createFailError(`Unable to parse the OpenSearch Dashboards' package.json file`);
163164
}
164165

165166
let pluginPackage;
166167
try {
167-
pluginPackage = await import(Path.join(process.cwd(), 'package.json'));
168+
pluginPackage = await import(Path.join(PROCESS_WORKING_DIR, 'package.json'));
168169
} catch (ex) {
169170
throw createFailError(`Unable to parse the plugin's package.json file`);
170171
}
171172

172173
let manifestFile;
173174
try {
174-
manifestFile = await import(Path.join(process.cwd(), 'opensearch_dashboards.json'));
175+
manifestFile = await import(Path.join(PROCESS_WORKING_DIR, 'opensearch_dashboards.json'));
175176
} catch (ex) {
176177
throw createFailError(`Unable to parse the plugin's opensearch_dashboards.json file`);
177178
}
@@ -240,7 +241,7 @@ export function runCli() {
240241

241242
const context: VersionContext = {
242243
log,
243-
sourceDir: process.cwd(),
244+
sourceDir: PROCESS_WORKING_DIR,
244245
pluginVersion: updatedPluginVersion,
245246
compatibilityVersion: updatedCompatibilityVersion,
246247
};

packages/osd-plugin-helpers/src/integration_tests/build.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ import Path from 'path';
3232
import Fs from 'fs';
3333

3434
import execa from 'execa';
35-
import {
36-
REPO_ROOT,
37-
standardize,
38-
createStripAnsiSerializer,
39-
createReplaceSerializer,
40-
} from '@osd/dev-utils';
35+
import { standardize } from '@osd/cross-platform';
36+
import { REPO_ROOT, createStripAnsiSerializer, createReplaceSerializer } from '@osd/dev-utils';
4137
import extract from 'extract-zip';
4238
import del from 'del';
4339
import globby from 'globby';

packages/osd-pm/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"write-pkg": "^4.0.0"
7070
},
7171
"dependencies": {
72+
"@osd/cross-platform": "1.0.0",
7273
"@osd/utils": "1.0.0",
7374
"tslib": "^2.0.0"
7475
}

packages/osd-pm/src/utils/projects_tree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import chalk from 'chalk';
3232
import path from 'path';
3333

34-
import { standardize } from '@osd/utils';
34+
import { standardize } from '@osd/cross-platform';
3535
import { Project } from './project';
3636

3737
const projectKey = Symbol('__project');

packages/osd-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@osd/config-schema": "1.0.0",
14+
"@osd/cross-platform": "1.0.0",
1415
"load-json-file": "^6.2.0"
1516
},
1617
"devDependencies": {

packages/osd-utils/src/path/index.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* under the License.
2929
*/
3030

31-
import { join, normalize } from 'path';
31+
import { join } from 'path';
3232
import { accessSync, constants } from 'fs';
3333
import { TypeOf, schema } from '@osd/config-schema';
3434
import { REPO_ROOT } from '../repo_root';
@@ -94,27 +94,3 @@ export const config = {
9494
data: schema.string({ defaultValue: () => getDataPath() }),
9595
}),
9696
};
97-
98-
/**
99-
* Get a standardized reference to a path
100-
* @param {string} path - the path to standardize
101-
* @param {boolean} [usePosix=true] - produce a posix reference
102-
* @param {boolean} [escapedBackslashes=true] - on Windows, double-backslash the reference
103-
* @internal
104-
*/
105-
export const standardize = (
106-
path: string,
107-
usePosix: boolean = true,
108-
escapedBackslashes: boolean = true
109-
) => {
110-
/* Force os-dependant separators
111-
* path.posix.normalize doesn't convert backslashes to slashes on Windows so we manually force it afterwards
112-
*/
113-
const normal = normalize(path);
114-
115-
// Filter out in-browser executions as well as non-windows ones
116-
if (process?.platform !== 'win32') return normal;
117-
118-
if (usePosix) return normal.replace(/\\/g, '/');
119-
return escapedBackslashes ? normal.replace(/\\/g, '\\\\') : normal;
120-
};

src/dev/build/lib/config.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
import { resolve } from 'path';
3232

33-
import { REPO_ROOT, standardize } from '@osd/utils';
33+
import { standardize } from '@osd/cross-platform';
34+
import { REPO_ROOT } from '@osd/utils';
3435
import { createAbsolutePathSerializer } from '@osd/dev-utils';
3536

3637
import pkg from '../../../../package.json';

0 commit comments

Comments
 (0)