-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐛 add more meaningful log to importAny
- Loading branch information
1 parent
4c2d9d1
commit 2f7053e
Showing
17 changed files
with
74 additions
and
64 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
import { dirname } from 'path'; | ||
|
||
export async function importAny(...modules: string[]) { | ||
try { | ||
const mod = await modules.reduce( | ||
(acc, moduleName) => acc.catch(() => import(moduleName)), | ||
Promise.reject(), | ||
); | ||
|
||
return mod; | ||
} catch (e) { | ||
throw new Error(`Cannot find any of modules: ${modules}\n\n${e}`); | ||
} | ||
} | ||
|
||
export function concat(...arrs: any[]): any[] { | ||
return arrs.reduce((acc: [], a) => { | ||
if (a) { | ||
return acc.concat(a); | ||
} | ||
|
||
return acc; | ||
}, []); | ||
} | ||
|
||
/** Paths used by preprocessors to resolve @imports */ | ||
export function getIncludePaths(fromFilename: string, base: string[] = []) { | ||
return [ | ||
...new Set([...base, 'node_modules', process.cwd(), dirname(fromFilename)]), | ||
]; | ||
} | ||
|
||
const cachedResult: Record<string, boolean> = {}; | ||
|
||
/** | ||
* Checks if a package is installed. | ||
* | ||
* @export | ||
* @param {string} dep | ||
* @returns boolean | ||
*/ | ||
export async function hasDepInstalled(dep: string) { | ||
if (cachedResult[dep] != null) { | ||
return cachedResult[dep]; | ||
} | ||
|
||
let result = false; | ||
|
||
try { | ||
await import(dep); | ||
|
||
result = true; | ||
} catch (e) { | ||
result = false; | ||
} | ||
|
||
return (cachedResult[dep] = result); | ||
} |
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
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
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
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