Skip to content

Commit

Permalink
feat: update pathUpToWorkspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Sep 20, 2021
1 parent b1b0c39 commit a8577e6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
1 change: 0 additions & 1 deletion packages/@yarn-tool/dotenv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export function wsEnvConfig<E = typeof process.env>(cwd?: string, options?: IDot

for (current of pathUpToWorkspacesGenerator(cwd))
{

for (let file of files)
{
path = join(current, file);
Expand Down
7 changes: 4 additions & 3 deletions packages/@yarn-tool/path-parents/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export declare function pathUpToWorkspacesGenerator(cwd?: string, options?: {
export interface IOptions {
ignoreCurrentDirectory?: boolean;
}): Generator<string, void, unknown>;
export declare function pathUpToWorkspaces(cwd?: string): string[];
}
export declare function pathUpToWorkspacesGenerator(cwd?: string, options?: IOptions): Generator<string, void, unknown>;
export declare function pathUpToWorkspaces(cwd?: string, options?: IOptions): string[];
export default pathUpToWorkspaces;
17 changes: 8 additions & 9 deletions packages/@yarn-tool/path-parents/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 15 additions & 12 deletions packages/@yarn-tool/path-parents/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { handleOptions, pathParentsGenerator } from 'path-parents';
import { findRoot } from '@yarn-tool/find-root';
import { resolve } from 'upath2';
import pathIsSame from 'path-is-same';
import { pathIsSame } from 'path-is-same';

export function* pathUpToWorkspacesGenerator(cwd?: string, options?: {
ignoreCurrentDirectory?: boolean
})
export interface IOptions
{
cwd = resolve(cwd ?? process.cwd())
ignoreCurrentDirectory?: boolean;
}

if (!options?.ignoreCurrentDirectory)
{
yield cwd
}
export function* pathUpToWorkspacesGenerator(cwd?: string, options?: IOptions)
{
cwd = resolve(cwd ?? process.cwd())

let {
root,
Expand All @@ -22,7 +20,12 @@ export function* pathUpToWorkspacesGenerator(cwd?: string, options?: {
cwd,
});

if (hasWorkspace && !isWorkspace)
if (!options?.ignoreCurrentDirectory)
{
yield cwd
}

if (root.length && !pathIsSame(cwd, root))
{
for (let current of pathParentsGenerator(cwd))
{
Expand All @@ -39,9 +42,9 @@ export function* pathUpToWorkspacesGenerator(cwd?: string, options?: {
}
}

export function pathUpToWorkspaces(cwd?: string)
export function pathUpToWorkspaces(cwd?: string, options?: IOptions)
{
return [...pathUpToWorkspacesGenerator(cwd)]
return [...pathUpToWorkspacesGenerator(cwd, options)]
}

export default pathUpToWorkspaces
32 changes: 30 additions & 2 deletions packages/@yarn-tool/path-parents/test/lazy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pathUpToWorkspaces from '../index';
import __ from 'lodash/fp/__';
import { join } from 'path';
import { findRootLazy } from '@yarn-tool/find-root';

describe(`describe`, () =>
{
Expand All @@ -8,9 +10,35 @@ describe(`describe`, () =>
{

let actual = pathUpToWorkspaces(__dirname);
let expected;

expect(actual.length).toBeGreaterThan(0);
expect(actual.length).toBeGreaterThan(2);

actual = pathUpToWorkspaces(__dirname, {
ignoreCurrentDirectory: true,
});

expect(actual.length).toBeGreaterThan(2);

});

})

describe(`ws.root`, () =>
{
let rootData = findRootLazy();
let cwd = rootData.root;

test(`test`, () =>
{
let actual = pathUpToWorkspaces(cwd);

expect(actual.length).toStrictEqual(1);

actual = pathUpToWorkspaces(cwd, {
ignoreCurrentDirectory: true,
});

expect(actual.length).toStrictEqual(0);

});

Expand Down

0 comments on commit a8577e6

Please sign in to comment.