Skip to content

Commit

Permalink
copy path util functions from legacy to harmony
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Dec 27, 2021
1 parent ffc4843 commit a6b12be
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .bitmap
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,12 @@
"mainFile": "index.ts",
"rootDir": "scopes/toolbox/time/time-format"
},
"path/to-linux": {
"scope": "",
"version": "",
"mainFile": "index.ts",
"rootDir": "scopes/toolbox/path/to-linux"
},
"ts-server": {
"scope": "teambit.typescript",
"version": "0.0.30",
Expand Down
2 changes: 1 addition & 1 deletion scopes/compilation/compiler/dist-artifact.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
import { pathNormalizeToLinux } from '@teambit/toolbox.path.to-linux';

export class DistArtifact {
constructor(private artifacts: AbstractVinyl[]) {}
Expand Down
2 changes: 1 addition & 1 deletion scopes/component/component/component.graphql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stripAnsi from 'strip-ansi';
import gql from 'graphql-tag';
import { GraphQLJSONObject } from 'graphql-type-json';
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
import { pathNormalizeToLinux } from '@teambit/toolbox.path.to-linux';

import { Component } from './component';
import { ComponentFactory } from './component-factory';
Expand Down
2 changes: 1 addition & 1 deletion scopes/preview/preview/preview-artifact.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
import { pathNormalizeToLinux } from '@teambit/toolbox.path.to-linux';

export class PreviewArtifact {
constructor(private artifacts: AbstractVinyl[]) {}
Expand Down
2 changes: 1 addition & 1 deletion scopes/react/react/react.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { ESLintMain, EslintConfigTransformer } from '@teambit/eslint';
import { PrettierConfigTransformer, PrettierMain } from '@teambit/prettier';
import { Linter, LinterContext } from '@teambit/linter';
import { Formatter, FormatterContext } from '@teambit/formatter';
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
import { pathNormalizeToLinux } from '@teambit/toolbox.path.to-linux';
import type { ComponentMeta } from '@teambit/react.ui.highlighter.component-metadata.bit-component-meta';
import { SchemaExtractor } from '@teambit/schema';
import { join, resolve } from 'path';
Expand Down
2 changes: 2 additions & 0 deletions scopes/toolbox/path/to-linux/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { normalizePath } from './normalize-path';
export { pathJoinLinux, pathNormalizeToLinux, pathRelativeLinux, pathResolveToLinux } from './to-linux';
31 changes: 31 additions & 0 deletions scopes/toolbox/path/to-linux/normalize-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* normalize-path <https://github.com/jonschlinkert/normalize-path>
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Released under the MIT License.
*/

export function normalizePath(path: string, stripTrailing?: boolean) {
if (path === '\\' || path === '/') return '/';

const len = path.length;
if (len <= 1) return path;

// ensure that win32 namespaces has two leading slashes, so that the path is
// handled properly by the win32 version of path.parse() after being normalized
// https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
let prefix = '';
if (len > 4 && path[3] === '\\') {
const ch = path[2];
if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
path = path.slice(2);
prefix = '//';
}
}

const segs = path.split(/[/\\]+/);
if (stripTrailing !== false && segs[segs.length - 1] === '') {
segs.pop();
}
return prefix + segs.join('/');
}
35 changes: 35 additions & 0 deletions scopes/toolbox/path/to-linux/to-linux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as path from 'path';
import { normalizePath } from './normalize-path';

export type PathLinux = string; // Linux format path (even when running on Windows)
export type PathLinuxRelative = string;
export type PathLinuxAbsolute = string;

// OS based format. On Windows it is Windows format, on Linux it is Linux format.
export type PathOsBased = string | PathOsBasedRelative | PathOsBasedAbsolute;
export type PathOsBasedRelative = string;
export type PathOsBasedAbsolute = string;

export function pathJoinLinux(...paths): PathLinux {
return normalizePath(path.join(...paths));
}
export function pathNormalizeToLinux(pathToNormalize?: PathOsBased): PathLinux | undefined {
return pathToNormalize ? normalizePath(pathToNormalize) : pathToNormalize;
}
export function pathRelativeLinux(from: PathOsBased, to: PathOsBased): PathLinux {
return normalizePath(path.relative(from, to));
}
export function pathResolveToLinux(arr: PathOsBased[]): PathLinux {
return normalizePath(path.resolve(arr.join(',')));
}

/**
* path.resolve uses current working dir.
* sometimes the cwd is not important. a user may run a Bit command from an inner dir.
*/
export function getPathRelativeRegardlessCWD(from: PathOsBasedRelative, to: PathOsBasedRelative): PathLinuxRelative {
const fromLinux = pathNormalizeToLinux(from);
const toLinux = pathNormalizeToLinux(to);
// change them to absolute so path.relative won't consider the cwd
return pathRelativeLinux(`/${fromLinux}`, `/${toLinux}`);
}
2 changes: 1 addition & 1 deletion scopes/ui-foundation/ui/webpack/webpack.dev.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ProvidePlugin } from 'webpack';
import * as stylesRegexps from '@teambit/webpack.modules.style-regexps';
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
import { pathNormalizeToLinux } from '@teambit/toolbox.path.to-linux';
import { WebpackConfigWithDevServer } from '@teambit/webpack';

import HtmlWebpackPlugin from 'html-webpack-plugin';
Expand Down
2 changes: 1 addition & 1 deletion scopes/web-components/elements/elements-artifact.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ArtifactVinyl } from '@teambit/legacy/dist/consumer/component/sources/artifact';
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
import { pathNormalizeToLinux } from '@teambit/toolbox.path.to-linux';

export class ElementsArtifact {
constructor(private artifacts: ArtifactVinyl[]) {}
Expand Down
2 changes: 1 addition & 1 deletion scopes/webpack/webpack/config/webpack.dev.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import noopServiceWorkerMiddleware from 'react-dev-utils/noopServiceWorkerMiddle
import redirectServedPath from 'react-dev-utils/redirectServedPathMiddleware';
import getPublicUrlOrPath from 'react-dev-utils/getPublicUrlOrPath';
import { PubsubMain } from '@teambit/pubsub';
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
import { pathNormalizeToLinux } from '@teambit/toolbox.path.to-linux';
import { WebpackConfigWithDevServer } from '../webpack.dev-server';
import { fallbacks } from './webpack-fallbacks';

Expand Down
2 changes: 1 addition & 1 deletion scopes/workspace/workspace/watch/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import loader from '@teambit/legacy/dist/cli/loader';
import { BIT_MAP, COMPONENT_ORIGINS } from '@teambit/legacy/dist/constants';
import { Consumer } from '@teambit/legacy/dist/consumer';
import logger from '@teambit/legacy/dist/logger/logger';
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
import { pathNormalizeToLinux } from '@teambit/toolbox.path.to-linux';
import mapSeries from 'p-map-series';
import chalk from 'chalk';
import { ChildProcess } from 'child_process';
Expand Down

0 comments on commit a6b12be

Please sign in to comment.