Skip to content

Commit

Permalink
[@osd/cross-platform] Adds cross-platform helpers (opensearch-project…
Browse files Browse the repository at this point in the history
…#2681)

cherry-pick from 0642974

Signed-off-by: Miki <[email protected]>
  • Loading branch information
AMoo-Miki committed Dec 1, 2022
1 parent 7518d2c commit 7ba9cf6
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 46 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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",
Expand Down
1 change: 1 addition & 0 deletions packages/osd-config-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"osd:bootstrap": "yarn build"
},
"devDependencies": {
"@osd/cross-platform": "1.0.0",
"typescript": "4.0.2",
"tsd": "^0.16.0"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/osd-config-schema/src/errors/schema_error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import { relative, sep } from 'path';
import { SchemaError } from '.';

import { standardize, PROCESS_WORKING_DIR } from '@osd/cross-platform';

/**
* Make all paths in stacktrace relative.
*/
Expand All @@ -48,9 +50,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);
})
Expand Down
3 changes: 3 additions & 0 deletions packages/osd-cross-platform/README.md
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions packages/osd-cross-platform/package.json
Original file line number Diff line number Diff line change
@@ -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.16.0"
}
}
7 changes: 7 additions & 0 deletions packages/osd-cross-platform/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export * from './path';
export * from './process';
30 changes: 30 additions & 0 deletions packages/osd-cross-platform/src/path.ts
Original file line number Diff line number Diff line change
@@ -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;
};
22 changes: 22 additions & 0 deletions packages/osd-cross-platform/src/process.ts
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions packages/osd-cross-platform/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target",
"declaration": true,
"declarationMap": true
},
"include": [
"src/**/*"
]
}
1 change: 1 addition & 0 deletions packages/osd-optimizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@babel/cli": "^7.14.5",
"@babel/core": "^7.11.6",
"@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",
Expand Down
2 changes: 1 addition & 1 deletion packages/osd-optimizer/src/optimizer/get_changes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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');

Expand Down
1 change: 1 addition & 0 deletions packages/osd-plugin-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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
Expand Down
1 change: 1 addition & 0 deletions packages/osd-plugin-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "^6.1.1",
Expand Down
17 changes: 9 additions & 8 deletions packages/osd-plugin-helpers/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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';
Expand Down Expand Up @@ -81,10 +82,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?`
);
}

Expand Down Expand Up @@ -150,30 +151,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`);
}
Expand Down Expand Up @@ -242,7 +243,7 @@ export function runCli() {

const context: VersionContext = {
log,
sourceDir: process.cwd(),
sourceDir: PROCESS_WORKING_DIR,
pluginVersion: updatedPluginVersion,
compatibilityVersion: updatedCompatibilityVersion,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,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';
Expand Down
1 change: 1 addition & 0 deletions packages/osd-pm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/osd-pm/src/utils/projects_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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');
Expand Down
1 change: 1 addition & 0 deletions packages/osd-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@osd/config-schema": "1.0.0",
"@osd/cross-platform": "1.0.0",
"load-json-file": "^6.2.0"
},
"devDependencies": {
Expand Down
26 changes: 1 addition & 25 deletions packages/osd-utils/src/path/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* GitHub history for details.
*/

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';
Expand Down Expand Up @@ -96,27 +96,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;
};
3 changes: 2 additions & 1 deletion src/dev/build/lib/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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';
Expand Down

0 comments on commit 7ba9cf6

Please sign in to comment.