-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move watch utils into separate module
- Loading branch information
1 parent
5c05672
commit 4d52e2e
Showing
3 changed files
with
62 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |