Skip to content

Commit

Permalink
Move watch utils into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
simonihmig committed Sep 10, 2024
1 parent 5c05672 commit 4d52e2e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
commonAncestorDirectories,
getImportableModules,
getWatchedDirectories,
} from '../util';
} from '../watch-utils';
import { Project } from 'scenario-tester';

const { module: Qmodule, test } = QUnit;
Expand Down
62 changes: 0 additions & 62 deletions packages/ember-auto-import/ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
import isSubdir from 'is-subdir';
import { dirname } from 'path';
// @ts-expect-error types don't resolve as this package exposes them only via package.json exports, which our old TS version does not support
import { getPackageEntryPoints } from 'pkg-entry-points';

// copied from pkg-entry-points, as we cannot use their types, see comment above
type ConditionToPath = [conditions: string[], internalPath: string];
type PackageEntryPoints = {
[subpath: string]: ConditionToPath[];
};

export function shallowEqual(a: any[], b: any[]) {
return (
a &&
Expand All @@ -21,54 +10,3 @@ export function shallowEqual(a: any[], b: any[]) {
export function stripQuery(path: string) {
return path.split('?')[0];
}

export function commonAncestorDirectories(dirs: string[]): string[] {
return dirs.reduce((results, fileOrDir) => {
let dir = dirname(fileOrDir);

if (results.length === 0) {
return [dir];
}

let newResults = results.filter(
(existingDir) => !isSubdir(dir, existingDir)
);

if (!newResults.some((existingDir) => isSubdir(existingDir, dir))) {
newResults.push(dir);
}

return newResults;
}, [] as string[]);
}

export async function getImportableModules(
packagePath: string
): Promise<string[]> {
const entryPoints: PackageEntryPoints = await getPackageEntryPoints(
packagePath
);

return Object.values(entryPoints)
.map(
(alternatives) =>
alternatives.find(
([conditions]) =>
(conditions.includes('import') || conditions.includes('default')) &&
!conditions.includes('types') &&
!conditions.includes('require') &&
!conditions.includes('node')
)?.[1]
)
.filter((item): item is string => !!item)
.filter((item, index, array) => array.indexOf(item) === index);
}

export async function getWatchedDirectories(
packagePath: string
): Promise<string[]> {
const modules = (await getImportableModules(packagePath)).filter(
(module) => module !== './addon-main.cjs'
);
return commonAncestorDirectories(modules);
}
61 changes: 61 additions & 0 deletions packages/ember-auto-import/ts/watch-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import isSubdir from 'is-subdir';
import { dirname } from 'path';
// @ts-expect-error types don't resolve as this package exposes them only via package.json exports, which our old TS version does not support
import { getPackageEntryPoints } from 'pkg-entry-points';

// copied from pkg-entry-points, as we cannot use their types, see comment above
type ConditionToPath = [conditions: string[], internalPath: string];
type PackageEntryPoints = {
[subpath: string]: ConditionToPath[];
};

export function commonAncestorDirectories(dirs: string[]): string[] {
return dirs.reduce((results, fileOrDir) => {
let dir = dirname(fileOrDir);

if (results.length === 0) {
return [dir];
}

let newResults = results.filter(
(existingDir) => !isSubdir(dir, existingDir)
);

if (!newResults.some((existingDir) => isSubdir(existingDir, dir))) {
newResults.push(dir);
}

return newResults;
}, [] as string[]);
}

export async function getImportableModules(
packagePath: string
): Promise<string[]> {
const entryPoints: PackageEntryPoints = await getPackageEntryPoints(
packagePath
);

return Object.values(entryPoints)
.map(
(alternatives) =>
alternatives.find(
([conditions]) =>
(conditions.includes('import') || conditions.includes('default')) &&
!conditions.includes('types') &&
!conditions.includes('require') &&
!conditions.includes('node')
)?.[1]
)
.filter((item): item is string => !!item)
.filter((item, index, array) => array.indexOf(item) === index);
}

export async function getWatchedDirectories(
packagePath: string
): Promise<string[]> {
const modules = (await getImportableModules(packagePath)).filter(
(module) => module !== './addon-main.cjs'
);
return commonAncestorDirectories(modules);
}

0 comments on commit 4d52e2e

Please sign in to comment.